2011-11-10 16 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グラフィックストレイル](http://download.oracle.com/javase/tutorial/2d/index)を実行する必要があります。 html)のJavaチュートリアルです。 –

答えて

1

List<Point> listImagePanelを追加します。

paintComponent(g)ImagePanelに上書きします。描画するには、属性listのデータを使用します。

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

    g.drawRect(...); 
} 

JFrameからImagePanelにリストを送信します。

フレームを作成した後にframe.setLayout(new FlowLayout())に電話する必要があります。

+0

美しくありがとう! – 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クラスに座標を渡す必要があります。

関連する問題