Java如何一次有多个矩形

时间:2013-10-16 16:32:20

标签: java swing

我想在鼠标x和y处绘制一个矩形。我希望有很多矩形,所以如果我点击JFrame上的50,50坐标,它会绘制一个矩形,然后如果我点击其他地方,它会在那里绘制另一个矩形,但不删除另一个,所以我可以点击5次(< - 示例)然后我将同时有五个矩形。

矩形也应该有一个固定的高度和宽度,所以当你点击一个特定的区域时,它会绘制一个10 x 10的矩形,它将记住已经绘制的所有其他矩形并保持它们被绘制地方以及我如何将它画成arraylist(如果有的话)

我的代码:

public class Game extends Canvas实现Runnable {     private static final long serialVersionUID = 1L;

public boolean running = false;
public static final String title = "tilebased game!";

private Thread thread;
public int height = 600;
public int width = 800;
private Dimension d = new Dimension(width, height);
public static Rectangle block;
public static Rectangle[] blocks = new Rectangle[2];
public static int blocknum = 0;
public static int xCreate;
public static int yCreate;
public static int xcoord;
public static int ycoord;
 public static ArrayList<Rectangle> rects = new ArrayList<Rectangle>();


public static boolean islicked = false;

public Game() {
    setPreferredSize(d);
    setMinimumSize(d);
    setMaximumSize(d);
    addMouseListener(new tile());
    addMouseMotionListener(new tile());

}



public void start() {


    running = true;
    new Thread(this).start();

}

public void stop() {

    running = false;

}

public static void main(String[] args) {
    Game g = new Game();
    JFrame f = new JFrame();
    f.add(g);
    f.pack();
    f.setTitle(title);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    g.start();

}

public void run() {
    while(running){
        tick();
        render();
    }

    try{
        Thread.sleep(5);
    }catch(Exception e){

    }
}


public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(2);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.white);


    for (int i = 0; i < rects.size(); i++) {
        Rectangle rect = rects.get(i);

    }


    g.dispose();
    bs.show();
}

public  void tick() {

}

}

和另一个班级。

    public class tile implements MouseListener, MouseMotionListener {


    public static Game game;
public Image img;

public static boolean clicked = false;
public tile tile;



@Override
public void mouseDragged(MouseEvent arg0) {

}

@Override
public void mouseMoved(MouseEvent e) {
    Game.xcoord = e.getX();
    Game.ycoord = e.getY();
}

@Override
public void mouseClicked(MouseEvent e) {
    Game.rects.add(new Rectangle(Game.xcoord,Game.ycoord,10,10));
    System.out.println("hi mayte");


}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent e) {
    if(e.getButton()== MouseEvent.BUTTON1){
        clicked = true;


    Game.xcoord = e.getX();
    Game.ycoord = e.getY();
    clicked = true;
    }

}

public void mouseReleased(MouseEvent e) {
    if(e.getButton()== MouseEvent.BUTTON1){
        clicked = true;
        System.out.println("hi mayte");


    Game.xcoord = e.getX();
    Game.ycoord = e.getY();
    clicked = false;
    }
}

}

1 个答案:

答案 0 :(得分:-1)

你可以制作一个矩形的ArrayList:

ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();

然后添加矩形(填充并将其放入mouseClicked中):

rectangles.add(new Rectangle(..));

之后,您可以遍历此数组:

for (int i = 0; i < rectangles.size(); i++) {
    Rectangle rect = rectangles.get(i);
    // paint methods here
}

并从集合中删除任何矩形:

rectangles.remove(5); // removes fifth element

ArrayList是一个负责存储动态数量的对象的类。工作期间尺寸发生了变化。