drawingPanel颜色随位移而变化

我正在使用JGrasp,在drawingPanel ,我正在尝试创建一个在屏幕上移动时改变颜色的球。 现在,我有:

 for (int i = 10; i<=i; i++) { Color c = new Color(i*i, 0, 0); pen.setColor(c); 

我的完整简化代码是:

 import java.awt.*; import java.util.*; import java.awt.Color; public class BallSample { public static final int SIZE = 30; public static final int HALF = SIZE / 2; public static void main(String[] args) { Scanner console = new Scanner(System.in); DrawingPanel panel = new DrawingPanel(1000, 1000); panel.setBackground(Color.CYAN); Graphics pen = panel.getGraphics(); for (int i = 10; i<=i; i++) { Color c = new Color(i*i, 0, 0); pen.setColor(c); pen.fillOval(500 - HALF, 500 - HALF, SIZE, SIZE); double xDisplacement = 30 * Math.cos(30); double yDisplacement = 30 * Math.sin(30) * -1; double x = 500.0; double y = 500.0; for (int j = 1; j <= 100; j++) { x = x + xDisplacement; y = y + yDisplacement; if (x = 1000) { xDisplacement = xDisplacement * -1; } if (y = 1000) { yDisplacement = yDisplacement * -1; } pen.fillOval((int) x - HALF, (int) y - HALF, SIZE, SIZE); panel.sleep(50); } } } } 

我希望这足够简化。

在以下代码中遇到一些问题后,我能够创建一个变色球示例。

首先,有panel.sleep(50); 让我思考(我在评论中发布的DrawingPanel类链接确认)可能会变得危险,最好使用Swing Timer ,但是我不会假装使用或读取所有类,因为它太长了,所以在我发布的例子中,我将使用Swing Timer方法。

另外,我不确定为什么用随机int (0到255之间new Color(int rgb)调用new Color(int rgb)构造函数不会更新或创建具有这些设置的新Color ,然后在阅读此答案和另一个(我丢失了)之后它的链接,对不起),你可以使用new Color(float r, float g, float b) ,因此你可以使用最多256的随机数。

你也可以将你的方法建立在MVC模式的基础上,有一个Model Ball类,它包含coords和它将拥有的Color ,一个随时间抽出球的View JPanel和一个可以随时改变球的坐标和颜色的Controller。好。

免责声明:此示例只是水平移动球并且不validation它是否在屏幕上,您需要根据自己的需要调整此示例。

 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.Timer; public class ChangingColorBall { private JFrame frame; private Timer timer; private BallPane ballPane; private Ball ball; public static void main(String[] args) { SwingUtilities.invokeLater(new ChangingColorBall()::createAndShowGui); //We place our program on the EDT } private void createAndShowGui() { frame = new JFrame(getClass().getSimpleName()); ball = new Ball(); ballPane = new BallPane(ball); timer = new Timer(100, e -> { //This Timer will execute every 100ms and will increase ball's X coord and paint it again. ball.increaseX(); //This method increases X coord and changes ball's color on the model ballPane.revalidate(); ballPane.repaint(); }); frame.add(ballPane); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); timer.start(); //We start the timer } class Ball { private int xCoord; private static final int Y = 50; private static final int SIZE = 20; private Color color; private Random r; public void increaseX() { xCoord += 10; //Increases X coord r = new Random(); color = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); //Generates a new random color } public int getXCoord() { return xCoord; } public void setXCoord(int xCoord) { this.xCoord = xCoord; } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } } @SuppressWarnings("serial") class BallPane extends JPanel { private Ball ball; public BallPane(Ball ball) { this.ball = ball; } @Override protected void paintComponent(Graphics g) { //This method paints the ball according to the color it has and the actual coords it has super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(ball.getColor()); g2d.fillOval(ball.getXCoord(), Ball.Y, Ball.SIZE, Ball.SIZE); } @Override public Dimension getPreferredSize() { //We should not call `.setSize(...)` method, so we override this one and call `.pack();` return new Dimension(200, 100); } } } 

注意

请注意不要将您的coords称为xy或者至少不要将其getter和setter称为getX()getY()否则您可能会遇到我在本答案开头处链接的问题。

以下是GUI的示例,我无法在计算机中执行GIF,但如果您复制粘贴代码并查看其工作原理,则会更好。

在此处输入图像描述

要更深入地了解自定义绘画如何在Swing中工作,请查看Oracle的课程: 在AWT和Swing教程中执行自定义绘画和绘画 。