我想用Java制作一个程序。我想做的是,当用户单击屏幕时,会绘制一个小方块。一个又一个的十个方格以前一个方格为中心显示。绘制第六个方块时,第一个方块消失,绘制第七个方块时,第二个方块消失,依此类推,直到所有方块都消失。
这是主活动:
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainActivity {
public static int width = 900;
public static int height = width / 16 * 9;
static HandlerClass handler = new HandlerClass();
static JFrame window;
static JPanel windowInner;
public static void main(String[] args) {
window = new JFrame("Squarmony 1.0");
window.setSize(width, height);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
windowInner = new JPanel();
windowInner.setSize(width, height);
windowInner.setBackground(Color.BLACK);
window.add(windowInner);
windowInner.addMouseListener(handler);
}
private static class HandlerClass implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
GraphicsActivity g = new GraphicsActivity();
g.drawRectRipple(window.getGraphics(), e);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
这是GraphicsActivity:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
public class GraphicsActivity {
public void drawRectRipple(Graphics g, MouseEvent e) {
g.setColor(Color.WHITE);
for (int i = 0; i <= 10; i++) {
for (int j = 0; j <= 250; j += 10) {
g.drawRect(e.getX() + j / 2, e.getY() - j / 2, j - j / 2, j + j / 2);
try {
Thread.sleep(250);
} catch (InterruptedException e1) {
}
}
}
}
}
我怎样才能将矩形(如波纹)一张一张地绘制到屏幕上?
谢谢,
约翰