2013-10-16 54 views
-3

我想繪製一個矩形在鼠標x和y。我想在那裏有很多矩形,所以如果我點擊JFrame上的50,50座標,它會繪製一個矩形,然後如果我點擊了其他地方,它會在那裏繪製另一個矩形,但不會刪除另一個矩形,所以我可以點擊5次(< - 例子),那麼我會一次有五個矩形。Java我怎樣才能一次有多個矩形

矩形應該有一個固定的高度和寬度,所以當你點擊一個特定的區域時,它將繪製一個10×10的矩形,它會記住所有已繪製的其他矩形,並將它們繪製在同一個地方,還我如何畫它的數組列表(如果有的話)

我的代碼:

公共類遊戲擴展畫布實現Runnable { 私有靜態最後的serialVersionUID長= 1升;

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; 
    } 
} 

}

+2

這個問題如何不同於[java:多個矩形一次](http://stackoverflow.com/questions/19386128/java-multiple-rectangles-at-once) – trashgod

+0

我建議將元素添加到LinkedList ,在渲染方法中遍歷它並將它們全部繪製。您可能還必須定義某些內容,例如:如果鼠標按下和鼠標釋放的位移大於0,則只會添加矩形。假設您需要動態矩形大小。 – Obicere

回答

-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的是負責存儲對象的動態量的類。它在工作中的尺寸變化。

+0

thankx男人很開心 – user2279603

+0

@ user2279603,你爲什麼要浪費人們的時間?你昨天給了這個問題的答案。提出重複問題和忽略建議會導致人們在將來忽略你的問題。 – camickr

+0

@Adrian,在上面的評論中已經提到了垃圾內容,這是一個重複的問題。通過回答你是鼓勵這種行爲。 – camickr