2012-11-25 1 views
1

Jpanel에서 클릭 할 수있는 곳으로 함수를 만들었고 마우스를 클릭 한 위치에 모양을 그립니다. 내가 겪고있는 문제는 새로운 위치를 클릭 할 때 모양을 이동하고 다시 그립니다. 이전의 모양을 화면에 "굽고"거기에 머물고 싶습니다. 그것은 어떤 데이터를 묶을 필요는 없으며 단지 모양의 이미지가 매번 어디에 있었는지를 보여 주기만하면됩니다. 나는 여러 가지 일을 시도했지만 성공하지 못했습니다.모양을 그리지 만 자바에서 화면에 가장 앞선 모양을 유지

public void paintComponent(Graphics g) { 

    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D) g; 
    g2.setColor(Color.BLUE); 
    g2.fillRect(n, m, 32, 32); //I want each one of these shapes to be new, not 
             //moving, but redrawn  

    //////////////////////////////////////////////// 
    //This is just drawing a grid and doing other things(irrelevant) 
    g2.fill(new Ellipse2D.Double(x, y, 32, 32)); 
    for (int i = 0; i < 500; i += 32) { 
     g2.drawRect(i, j, 32, 32); 
     for (int j = 0; j < 500; j += 32) { 
      g2.drawRect(i, j, 32, 32); 
     } 
    } 


    if (paintColBlock){ 
     System.out.println("Drawing Block at " + n +"," + m); 
     paintColBlock = false; 
    } 
    /////////////////////////////////////////////////////////////////////  

} 
+1

이전 도형을 직접 기억해야합니다. 아이디어 :'BufferedImage' –

답변

3

이 같은 ArrayListPoint의의 유지 : 여기 무슨 뜻인지 각 마우스 클릭에서 배열에 새 Point 추가

public void paintComponent(Graphics g) { 

super.paintComponent(g); 
Graphics2D g2 = (Graphics2D) g; 
g2.setColor(Color.BLUE); 
for(Point p : points) 
    g2.fillRect(p.x, p.y, 32, 32); 

을하고, 전화 repaint() :

public void mousePressed(MouseEvent evt){ 
    points.add(new Point(evt.getX(),evt.getY()); 
    repaint(); 
}