只是为了您的一点帮助,我发布了这段代码。尽管请尝试从中学习并提出可能会出现的有效问题,以了解整个事情。毫无疑问,如果您稍微搜索一下,您可能会找到这个精彩的文档,它在这个示例中准确地解释了您的需求。
每当用户单击或拖动鼠标时以编程方式重新绘制组件的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MovingSquare
{
private int x = 0;
private int y = 100;
private final int WIDTH = 100;
private CustomPanel canvas;
private Timer drawingTimer;
private ActionListener timerAction =
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
if ((x + WIDTH == 500))
{
x = 0;
canvas.setValues(x, y, Color.BLUE);
}
else
{
x += WIDTH;
canvas.setValues(x, y, Color.BLUE);
}
}
};
private void displayGUI()
{
JFrame frame = new JFrame("Moving Sqaure");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
canvas = new CustomPanel();
frame.setContentPane(canvas);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
drawingTimer = new Timer(1000, timerAction);
drawingTimer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new MovingSquare().displayGUI();
}
});
}
}
class CustomPanel extends JPanel
{
private final int WIDTH = 500;
private final int HEIGHT = 500;
private final int WSQUARE = 100;
private final int HSQUARE = 50;
private int x = 0;
private int y = 100;
private Color cSquare = Color.BLUE;
/*
* This is where we updating the state
* of different variables needed, and
* thus calling repaint.
*/
public void setValues(int x, int y, Color color)
{
cSquare = color;
repaint(this.x, this.y, WSQUARE, HSQUARE);
this.x = x;
this.y = y;
repaint(x, y, WSQUARE, HSQUARE);
}
/*
* Always make this one customary
* habbit, to override this method
* when you extending a JComponent.
*/
@Override
public Dimension getPreferredSize()
{
return (new Dimension(WIDTH, HEIGHT));
}
/*
* This is where the actual Painting
* Portion of the whole thingy will
* reside. Better is, not to put any
* calculations in this part, just
* update the state at some another
* location and convey it to repaint
* as needed.
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(cSquare);
g.fillRect(x, y, WSQUARE, HSQUARE);
}
}
最新编辑:
请试试这个修改后的代码,CustomPanel 类和以前一样:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class MovingSquare
{
private int x = 0;
private int y = 100;
private final int WIDTH = 100;
private final int HEIGHT = 100;
private Random random;
private CustomPanel canvas;
private Timer drawingTimer;
private ActionListener timerAction =
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
/*if ((x + WIDTH > 500) && (y + HEIGHT > 500))
{
x = random.nextInt(500 - WIDTH);
canvas.setValues(x, y, Color.BLUE);
}
else
{
x += WIDTH;
canvas.setValues(x, y, Color.BLUE);
}*/
x = random.nextInt(500 - WIDTH);
y = random.nextInt(500 - HEIGHT);
canvas.setValues(x, y, new Color(
random.nextFloat(), random.nextFloat()
, random.nextFloat(), random.nextFloat()));
}
};
public MovingSquare()
{
random = new Random();
}
private void displayGUI()
{
JFrame frame = new JFrame("Moving Sqaure");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
canvas = new CustomPanel();
frame.setContentPane(canvas);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
drawingTimer = new Timer(1000, timerAction);
drawingTimer.start();
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new MovingSquare().displayGUI();
}
});
}
}