【问题标题】:Java How can i have multiple rectangles at once [duplicate]Java如何一次拥有多个矩形[重复]
【发布时间】:2013-10-16 16:32:19
【问题描述】:

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

矩形也应该有固定的高度和宽度,所以当你点击一个特定的区域时,它会绘制一个 10 x 10 的矩形,它会记住所有其他已经绘制的矩形并保持它们的绘制相同位置以及如何将其绘制为数组列表(如果有的话)

我的代码:

公共类游戏扩展画布实现可运行{ 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;
    }
}

}

【问题讨论】:

  • 这个问题与java : multiple rectangles at once有何不同
  • 我建议将元素添加到 LinkedList 中,在渲染方法中对其进行迭代并全部绘制。您可能还必须定义某些内容,例如:仅当鼠标按下和鼠标释放的位移大于 0 时才会添加矩形。假设您需要动态矩形大小。

标签: java swing


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

【讨论】:

  • thankx man 很开心
  • @user2279603,你为什么要浪费人们的时间?你昨天在哪里给出了这个问题的答案。提出重复的问题并忽略建议会导致人们在未来忽略您的问题。
  • @Adrian,trashgod 已经在上面的评论中指出这是一个重复的问题。通过回答您是在鼓励这种行为。
  • @camickr,我明白了,我确实在 1-2 天前看到了这个问题,但我不知道为什么又被问到了?我以为人们忽略了他。
  • @Sage,你是什么意思人们忽略了他?那里给出了 3 个答案。
猜你喜欢
  • 1970-01-01
  • 2017-07-22
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多