从画面中心开始画点,这个点随机上下左右移动,形成如图轨迹,直到离开画面。用Java到底该怎么做? 20
1个回答
展开全部
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
/**
* 面板上的光标改变成为手型
* @author hardneedl
*/
final class RandomPointDemo extends JFrame{
private static final Dimension SIZE = new Dimension(600,400);
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getPreferredSize() {return SIZE;}
public String getTitle() {return "RandomPointDemo";}
/*画笔*/
private interface Brush extends Observer {
void paint(Graphics g);
Component getComponent();
}
abstract private class PointBrush implements Brush {
private static final int OFFSET = 2;
private BufferedImage getImage() {
return image;
}
private BufferedImage image;
//上一次点的位置
private int lastX, lastY;private Point newPoint;
PointBrush(int x,int y) {
lastX = x;lastY = y;
image = new BufferedImage(2000,2000,BufferedImage.TYPE_INT_RGB);
}
public void paint(Graphics g2) {
if (newPoint != null&&image != null) {
g2.setColor(Color.ORANGE);
g2.drawLine(lastX,lastY,newPoint.x,newPoint.y);
g2.setColor(Color.MAGENTA);
g2.fillRect(newPoint.x - OFFSET, newPoint.y - OFFSET, OFFSET<<1, OFFSET<<1);
g2.dispose();
lastY = newPoint.y;
lastX = newPoint.x;
}
}
public void update(Observable o, Object arg) {
if(arg instanceof Point) {
newPoint = (Point) arg;
paint(image.createGraphics());
}
}
}
/*画布*/
private class Canvas extends JComponent {
private void setBrush(PointBrush brush) {
this.brush = brush;
}
private PointBrush brush;
private Canvas(PointBrush brush) {
super();
this.brush = brush;
}
private Canvas() {
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isVisible()&&null!=brush) {
Graphics2D g2 = brush.getImage().createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
brush.paint(g2);
g.drawImage(brush.getImage(),0,0,this);
}
}
}
private Timer timer;
private Canvas canvas;
private RandomPointDemo() {
super();
init();
addListeners();
doLay();
}
private void init(){
canvas = new Canvas();
PointBrush brush = new PointBrush(canvas.getWidth()>>1, canvas.getHeight()>>1) {
public Component getComponent() {
return canvas;
}
};
canvas.setBrush(brush);
timer = new Timer(500, e -> {
Random random = new Random(System.currentTimeMillis());
Point newPoint = new Point(
random.nextInt(canvas.getWidth()),
random.nextInt(canvas.getHeight())
);
brush.update(null, newPoint);
canvas.paintImmediately(canvas.getBounds());
});
timer.start();
}
private void addListeners(){
}
private void doLay(){
Container container = getContentPane();
container.add(canvas, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(RandomPointDemo::new);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询