2017-11-15 5 views
2

2つのJFramesを使用するJComponentアプリケーションを作成しようとしていますが、1つのフレームに変更可能なスライダとテキストフィールドがあり、 「火」ボタンを押すと、花火のレンダリングが表示されます。しかし、戦略的な印刷ステートメントを配置することによって、コードをラッピングする条件ステートメントが満たされていてもpaintComponent()メソッドが実行されないことがわかりました。私は正しい値が正しい時に生成されることを確実にするために、私の他の方法をすべて二重にチェックしました。 JComponentのすべての文献と質問を調べたところ、私はそれをうまく動作させることができないのではないかと心配しています。この問題は、私がライブラリに精通していないことが原因です。それは言われても、どんなアドバイスであれ、どんなアドバイスでも大いに感謝されます。要約版コードは以下の通りです:私はそれを使用している場合ではないと確信しているためJFrameキャンバス上のpaintComponent()を別のGUIで変更しています

*スイングタイマーも問題になることがあり、正しく

[fireworksCanvas.java]

public class fireworkCanvas extends JComponent implements ActionListener{ 

private static final long serialVersionUID = 1L; 
private ArrayList<Ellipse2D> nodes = new ArrayList<Ellipse2D>(); 
private ArrayList<Line2D> cNodes = new ArrayList<Line2D>(); 
private ArrayList<QuadCurve2D> bCurves = new ArrayList<QuadCurve2D>(); 
private int[] arcX; 
private int[] arcY; 
private Color userColor; 
private Random rand = new Random(); 
private int shellX, shellY, fType, theta, velocity; 
private Timer timer; 
private int time; 
private double g = -9.8; //gravity in m/s 
public boolean explosivesSet; 

public fireworkCanvas() { 
    time = rand.nextInt(3000) + 2000; 
    timer = new Timer(time, this); // 5 seconds 
    timer.start(); 
    fType = 0; 
} 

@Override 
public void paintComponent(Graphics g){ 

    if (explosivesSet) { 
     System.out.println("fType" + fType); 
     super.paintComponent(g); 
     Graphics2D g2D = (Graphics2D) g; 

     g.setColor(Color.BLACK); 
     g.drawPolyline(arcX, arcY, arcX.length); 

     for (Ellipse2D e : nodes) { 
      System.out.println("painting nodes"); // NEVER PRINTS 
      g.setColor(userColor); 
      g.fillOval(shellX + (int) e.getX(), shellY + (int) e.getY(), (int) e.getWidth(), (int) e.getHeight()); 
     } 
     for (Line2D l: cNodes) { 
      System.out.println("painting cNodes"); // NEVER PRINTS 
      g.setColor(determineColor("l")); 
      g.drawLine(shellX + (int) l.getX1(), shellY + (int) l.getY1(), shellX + (int) l.getX2(), shellY + (int) l.getY2()); 
     } 
     for (QuadCurve2D c: bCurves) { 
      System.out.println("painting curves"); // NEVER PRINTS 
      g.setColor(determineColor("c")); 
      g2D.draw(c); 
     } 
    } 
} 

public Color determineColor(String type) { 

    // returns color 
} 

public void setExplosives() { 

    if (fType != 5 && fType != 0) { 

     nodes.clear(); // clears three array lists with FW components 
     cNodes.clear(); // these are the components to paint for the 
     bCurves.clear(); // firework explosion graphic 
     setArc(); // stores path of shell for a polyLine to be drawn 

     // builds and generates components for FW based on type chosen (fType) 
     setExplosivesSet(true); 
     repaint(); 
    } 
} 

public void setArc() { 
    // builds int[] for shellX, shellY 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    // nothing is here?? 
    // should I use the action performed in some way? 
} 

[GUI.java]すべての

public class GUI extends JFrame implements ActionListener, ChangeListener, ItemListener, MouseListener{ 

private static JFrame canvasFrame = new JFrame("Canvas"); 

private fireworkCanvas canvas = new fireworkCanvas(); 
private Choice fireworkChooser = new Choice(); 
private JSlider launchAngle = new JSlider(); 
private JSlider velocity = new JSlider(); 
private JSlider r = new JSlider(); 
private JSlider g = new JSlider(); 
private JSlider b = new JSlider(); 
private JPanel panel = new JPanel(); 
private JButton button = new JButton("Fire!"); 
private JLabel launchLabel = new JLabel("Launch Angle "); 
private JLabel velocityLabel = new JLabel("Velocity "); 
private JLabel rLabel = new JLabel("Red "); 
private JLabel gLabel = new JLabel("Green "); 
private JLabel bLabel = new JLabel("Blue "); 
public static int fHeight = 500; 
public static int fWidth = 500; 

public GUI() { 

    this.add(panel); 
    panel.add(button); 
    panel.add(fireworkChooser); 
    panel.add(launchAngle); 
    panel.add(launchLabel); 
    panel.add(velocity); 
    panel.add(velocityLabel); 
    panel.add(r); 
    panel.add(rLabel); 
    panel.add(g); 
    panel.add(gLabel); 
    panel.add(b); 
    panel.add(bLabel); 

    addActionListener(this); 
    BoxLayout bl = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS); 
    setLayout(bl); 

    fireworkChooser.addItemListener(this); 
    launchAngle.addChangeListener(this); 
    velocity.addChangeListener(this); 
    r.addChangeListener(this); 
    g.addChangeListener(this); 
    b.addChangeListener(this); 
    button.addActionListener(this); 

    fireworkChooser.add("Firework 1"); 
    fireworkChooser.add("Firework 2"); 
    fireworkChooser.add("Firework 3"); 
    fireworkChooser.add("Firework 4"); 
    fireworkChooser.add("Super Firework"); 

    launchAngle.setMinimum(1); 
    launchAngle.setMaximum(90); 
    velocity.setMinimum(1); 
    velocity.setMaximum(50); 
    r.setMinimum(0); 
    r.setMaximum(255); 
    g.setMinimum(0); 
    g.setMaximum(255); 
    b.setMinimum(0); 
    b.setMaximum(255); 

} 

@Override 
public Dimension getPreferredSize() { 
    return new Dimension(600, 200); 
} 

@Override 
public void stateChanged(ChangeEvent e) { 

    // sets FW variables 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == button) { 
     canvas.setfType(fireworkChooser.getSelectedIndex()+1); 
     canvas.setExplosives(); 
     canvas.repaint(); 
     canvas.setExplosivesSet(false); 
     System.out.println("button fired"); 
    } 
} 

public static void createAndShowGUI() { 
    GUI gui = new GUI(); 
    gui.pack(); 
    gui.setLocationRelativeTo(null); 
    gui.setVisible(true); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    fireworkCanvas canvas = new fireworkCanvas(); 
    canvasFrame.pack(); 
    canvasFrame.add(canvas); 
    canvasFrame.setLocationRelativeTo(null); 
    canvasFrame.setVisible(true); 
    canvasFrame.setSize(fWidth, fHeight); 
    canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 


} 
+1

paintComponentメソッドから 'setExplosivesSet(false);'を削除します。 paintComponentが呼び出される頻度を制御することはできません。さまざまなオペレーティングシステムのイベントに応答して、毎秒何回も呼び出すことができます。ペインティングメソッドは決して状態を変更してはなりません。 – VGR

+0

それはかなり意味があります。私は 'setExplosivesSet(false);を移動しました。 'to GUI.javaクラスのactionPerformed()には、私は信じられているはずですが、私の死はまだありません。 –

+0

'nodes'、' cNodes'、および 'bCurves'リストに何も追加することは決してありません。あなたは' arcX'と 'arcY'を初期化していないので、ペイントするものはありません。おそらく、 'actionPerformed'で動作するすべてのことをしたいと思っていました。これは、Timerの時間が経過するたびにTimerによって呼び出されます。 – VGR

答えて

2

まず:

public fireworkCanvas() 

クラス名は大文字で始まる必要があります。あなたのコードの他のすべてのクラスは、このルールに従います。例で学ぶ。

選択肢はAWTコンポーネントで、SwingアプリケーションにAWTコンポーネントを混在させないでください。 JComboBoxを使用します。私のpaintComponent()メソッドは、フレーム)あなたが(パックした後に、フレームにキャンバスを追加

fireworkCanvas canvas = new fireworkCanvas(); 
canvasFrame.pack(); 
canvasFrame.add(canvas); 

を実行していないので、キャンバスのサイズは(0、0である

)、塗装するものはありません。

pack()の前にフレームにキャンバスを追加し、FireworkCanvasクラスにgetPreferredSize()を実装して、pack()メソッドが正常に動作するようにする必要があります。

スイングチュートリアルのCustom Paintingのセクションを読み、基礎を学びましょう。

+0

私はあなたがJCheckBoxではなくJComboBoxを意味すると信じています。 – VGR

+0

@vgr、おっと、ありがとう。 – camickr

関連する問題