-3

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

矩形也应该具有固定的高度和宽度,因此当您单击特定区域时,它将绘制一个 10 x 10 的矩形,它会记住所有其他已绘制的矩形并将它们绘制在同一个位置,并且我如何将它绘制为数组列表(如果有的话)

我的代码:

公共类 Game 扩展 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;
    }
}

}

4

1 回答 1

-1

您可以制作一个 ArrayList 的 Rectangles:

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 是一个负责存储动态对象数量的类。它的大小在工作期间会发生变化。

于 2013-10-16T16:41:13.230 回答