2011-11-10 3 views
1

행운을 빌어 내 문제에 대한 해결책을 찾기 위해 꽤 많이 검색했습니다. 나는 거기에 간단한 해결책이 있다고 생각했을 것이다. 나는 ArrayList에 저장된 여러 점을 찾고 ArrayList에 주어진 각 점에서 셰이프를 드레이프 (이 단계에서 어떤 점이 상관 없으며 사각형이 수행합니다.)하고 싶습니다. 다음 코드를 사용합니다. 코드는 다음과 같습니다 :JPanel의 알려진 좌표에서 모양을 그립니다.

public static void main(String[] args){ 
     image = null; 
     try { 
      // Read from a file 
      File file = new File("box.jpg"); 
      image = ImageIO.read(file); 
     } catch (IOException e) { 
      System.out.print("LAAAAMMME!!!!"); 
     } 

     ImagePanel panel = new ImagePanel(image); //Custom JPanel to show a background image 
     panel.setPreferredSize(new Dimension(500, 500)); 

     JFrame frame = new JFrame("Find the Squares"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(500, 500); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.getContentPane().add(panel); 

     frame.validate(); 
     frame.setVisible(true); 
     frame.pack(); 


     ArrayList<Point> points = getCenterPoint(floatarray); 
     for(int x = 0 ; x < points.size(); x++){ 
      //Here I guess is where each point is created and drawn. 
     } 
    } 

어떤 제안이 있습니까?

+0

* "어떤 제안이 있습니까?"* 더 빨리 도움을 받으려면 해당 코드를 [SSCCE] (http://sscce.org/)로 업그레이드하십시오. –

+0

* // 여기서 각 지점이 작성되고 그려지는 곳입니다. * 여기에서 [2D Graphics Trail] (http://download.oracle.com/javase/tutorial/2d/index)을 수행해야하는 곳이 여기 있습니다. html). –

답변

1

List<Point> list부터 ImagePanel까지의 특성을 추가하십시오.

paintComponent(g)ImagePanel으로 바꿉니다. 그릴 때는 list 속성의 데이터를 사용하십시오.

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    g.drawRect(...); 
} 

JFrame에서 ImagePanel에 목록을 보냅니다.

프레임을 생성 한 후 frame.setLayout(new FlowLayout())으로 전화해야 할 수도 있습니다.

+0

Beautiful 매우 고마워요! – SamRowley

1
class MyTest{ 
public static void main(String[] args){ 
    image = null; 
    try { 
     // Read from a file 
     File file = new File("box.jpg"); 
     image = ImageIO.read(file); 
    } catch (IOException e) { 
     System.out.print("LAAAAMMME!!!!"); 
    } 

    ImagePanel panel = new ImagePanel(image); //Custom JPanel to show a background image 
    panel.setPreferredSize(new Dimension(500, 500)); 

    JFrame frame = new JFrame("Find the Squares"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(500, 500); 
    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 
    frame.getContentPane().add(panel); 
    frame.add(new MainPanel(Color.Red)); 
    frame.validate(); 
    frame.setVisible(true); 
    frame.pack(); 
    /*ArrayList<Point> points = getCenterPoint(floatarray); 
    for(int x = 0 ; x < points.size(); x++){ 
     //Here I guess is where each point is created and drawn. 
    }*/ 
} 
private class MainPanel extends JPanel { 
    Color color; 

    public MainPanel(Color color) { 
     this.color = color; 
    } 

    public void paintComponent(Graphics g) { 
     int width = getWidth(); 
     int height = getHeight(); 
     g.setColor(color); 
     //g.drawRect(startx,starty,endx,endy); // here you can drawRect as per your value 
    } 
} 
} 

체크 this 사이트

당신은 당신이 패널에 그릴 원하는 모양을 그릴 수 있도록 MainPanel 클래스에 좌표를 전달해야합니다.