2012-12-22 3 views
2

JPanel을 처음으로 사용하고 JPanel에서 기본 모양을 그립니다.Java JPanel 드로잉 모양

이 같은 형태에 대한 코드를 작성했습니다 :이 형태를 사용하게 될 경우

public class Shape extends JPanel{ 

int x,y; 

public Shape(int x, int y){ 
    this.x = x; 
    this.y = y; 
} 

public void paint(Graphics g){ 
    super.paint(g); 
    g.setColor(Color.black); 
    g.drawRect(x, y, 20, 20); 
    } 
} 

내가 다른 클래스가 있습니다. JFrame를 확장 해, MouseListener를 구현합니다. 이 JFrame에서는 JPanel을 단순히 "패널"이라고 부릅니다.

마우스 위치를 읽고 "패널"에 모양을 그리는 방법이 있습니다.

public void mouseClicked(MouseEvent e){ 
    Shape shape = new Shape(e.getX(),e.getY()); 
    panel.add(shape); 
    panel.revalidate(); 
    panel.repaint(); 
} 

문제는 마우스가있는 좌표에 모양을 그리지 않는다는 것입니다. 그것은 단지 윗면의 패널을 그려서 한 줄에 그립니다.

답장을 보내 주셔서 감사합니다.

답변

0
public class ShapesPanel extends JPanel { 

    private java.util.List shapesList ; 

    /** 
    * Constructs <code>ShapesPanel</code> 
    */ 
    public ShapesPanel() { 
     shapesList = new java.util.ArrayList() ; 
     this.addMouseListener(new MouseClickListener()) ; 
    } 


    /** 
    * Creates a shape 
    * @param bounds 
    * @return 
    */ 
    private Shape createShape(Rectangle bounds) { 
     Shape shape = new Ellipse2D.Double(bounds.x, bounds.y, bounds.width, bounds.height); 

/* 
     To use the following shapes, you need to have java shapes library, which can 
     be downloaded from <a href="http://wwww.jshapes.com">Java Shapes Library</a> 
*/ 
/* 
     // To create star shape 
     Shape shape = new Star(bounds, 50, Star.STAR_8_POINTS); 
     // To create triangle shape 
     Shape shape = new Triangle(bounds, Triangle.UP); 
     // To create diamond shape 
     Shape shape = new Diamond(bounds); 
     // To create Parallelogram shape 
     Shape shape = new Parallelogram(bounds ); 
*/ 

     return shape ; 
    } 

    /** 
    * MouseClickListener 
    */ 
    private class MouseClickListener extends MouseAdapter { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      Point pt = e.getPoint(); 
      Dimension size = new Dimension(100, 100); 
      Rectangle bounds = new Rectangle(pt.x, pt.y, size.width, size.height); 
      Shape shape = createShape(bounds); 
      shapesList.add(shape); 
      repaint(); 
     } 
    } 

    /** 
    * Paints the component 
    * @param g 
    */ 
    protected void paintComponent(Graphics g) { 

     Graphics2D g2 = (Graphics2D) g; 
     Rectangle bounds = new Rectangle(0,0,getWidth(), getHeight()); 
     g2.setPaint(Color.white); 
     g2.fill(bounds); 

     for (Iterator iterator = shapesList.iterator(); iterator.hasNext();) { 
      Shape shape = (Shape) iterator.next(); 
      g2.setPaint(Color.cyan); 
      g2.fill(shape); 
      g2.setPaint(Color.black); 
      g2.draw(shape); 
     } 
    } 

    /** 
    * Driver 
    * @param args 
    */ 
    public static void main(String[] args) { 
     JFrame frame = new JFrame("Draw Shapes") ; 
     frame.getContentPane().add(new ShapesPanel()); 
     frame.setSize(600, 600); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

    } 
} 

희망 사항은 패널에 자바 모양을 그려야합니다.