2015-12-03 37 views
0

我一直在嘗試解決這個錯誤,但我似乎無法弄清楚最新錯誤。我想編寫一個程序,提示用戶輸入中心和半徑的y座標和x座標和 。當用戶單擊「繪圖」按鈕時,在組件中繪製一個帶有該中心和半徑的圓。問題在理解什麼是錯我的代碼

import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class CircleMakerVeiwer { 

public static void main(String[] args) { 
    // stuff that displays that will help display the circle 
    JPanel panel = new JPanel(); 
    JPanel pane2 = new JPanel(); 
    JFrame frame = new JFrame(); 
    JButton button = new JButton("Draw Circle"); 

    // the obj that will be drawn 
    CircleMaker circle = new CircleMaker(); 

    // panel is set to a flowLayout 
    panel.setLayout(new FlowLayout()); 
    // panel's size is selected 
    panel.setPreferredSize(new Dimension(800, 800)); 

    // now here's where the fun begins 

    // labels 
    JLabel x_cor = new JLabel("X Cordinate"); 
    JLabel y_cor = new JLabel("Y Cordinate"); 
    JLabel rad = new JLabel("Radius"); 

    // JTextFeilds 
    JTextField x_input = new JTextField(3); 
    JTextField y_input = new JTextField(3); 
    // [in surfer dude voice] It's so RADDDD bROOOo 
    JTextField rad_input = new JTextField(3); 

    // all de panel adding 
    panel.add(x_cor); 
    panel.add(x_input); 
    panel.add(y_cor); 
    panel.add(y_input); 
    panel.add(rad); 
    panel.add(rad_input); 


    // time for the action listener 
    class DrawCircleListener implements ActionListener { 

     public void actionPerformed(ActionEvent arg0) { 
      int x, y, r; 
      System.out.println("action preformed (sort of)"); 
      x = Integer.parseInt(x_input.getText()); 
      y = Integer.parseInt(y_input.getText()); 
      r = Integer.parseInt(rad_input.getText()); 

      circle.setCircle(r, x, y); 

     }// end of actionPerformed 

    }// end of DrawCircleListener 
    DrawCircleListener listener = new DrawCircleListener(); 



    button.addActionListener(listener); 
    panel.add(button); 

    panel.add(circle); 

    frame.setSize(800, 800); 
    frame.setTitle("Circle Button"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(panel); 

    frame.setVisible(true); 

    }// end of main 

}// end of CircleMakerVeiwer.java 

我打算給CircleMaker對象以查看是否有任何事情以防萬一。

import java.awt.Graphics; 
    import java.awt.Graphics2D; 
    import java.awt.geom.Ellipse2D; 

    import javax.swing.JComponent; 

    /* 
    * This program will construct a circle it will also 
    * paint and set the parameters of the circle outside the constructor 
    */ 

public class CircleMaker extends JComponent { 

private Ellipse2D.Double c; 
    private int radius, x_cor, y_cor; 

public CircleMaker() { 
    c= new Ellipse2D.Double(); 
} 

public void setCircle(int r, int x_cordinate, int y_cordinate) { 
    //Graphics g; 
    c = new Ellipse2D.Double(x_cordinate - r, y_cordinate - r, r * 2, r * 2); 
    System.out.println("set circle was called"); 
    //repaint(); 
} 

public void paintComponent(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 
    g2.draw(c); 

} 


} 
+1

你能更具體的關於你的錯誤和預期的行爲?另請參閱[最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve) – zeeMonkeez

回答

3
  1. 您添加CircleMakerJPanel這是使用FlowLayout,裏面供奉組件的preferredSize,但CircleMaker的默認preferredSize0x0,所以該組件是有效的無形
  2. 你不當調用setCircle時,不會調用repaint,所以組件不知道它應該被更新。
  3. 您違反了paint方法鏈接合同而不是callin摹super.paintComponent做你的風俗畫通過重寫的CircleMakergetPreferredSize方法

開始......

public static class CircleMaker extends JComponent { 
    //... 
    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(400, 400); 
    } 

這將提供多大的一些想法之前,你想你組件是正常的佈局管理器

更新setCircle方法來調用repaint並添加super.paintComponentpaintComponent方法

public void setCircle(int r, int x_cordinate, int y_cordinate) { 
     //Graphics g; 
     c = new Ellipse2D.Double(x_cordinate - r, y_cordinate - r, r * 2, r * 2); 
     System.out.println("set circle was called"); 
     repaint(); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     g2.draw(c); 

    } 

最後,添加實例的CircleMaker到幀的CENTER位置,它採用了BorderLayout默認...

//panel.add(circle); 

    frame.setTitle("Circle Button"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(panel, BorderLayout.NORTH); 
    frame.add(circle); 

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

哦,擺脫panel.setPreferredSize(new Dimension(800, 800));

+0

謝謝,這幫助我瞭解我做錯了什麼,並幫助我修復它。 –

+0

很高興它可以幫助;) – MadProgrammer