2016-11-06 1 views
1

私のプログラムは、JPanelでマウスをクリックして離してシェイプを描画できるGUIを作成します。その他のオプションには、色の変更や図形の塗りつぶしの有無などがあります。シェイプが描画されると、他のオプションは同じシェイプを新しいモディファイアで再描画する必要があります。たとえば、赤い四角形を描くと、青色を選択すると、四角形が色を変えるだけです。私はこれまでに「長方形」オプションのみを実装しています。PaintComponent()はグラフィックスをペイントしますが、repaint()が呼び出されても更新された値は使用されません

my mouseListnerは値を正しくキャプチャして格納しますが、paintComponent()は値がハードコードされているときに矩形を描画します(paintComponent()のセクションをコメントアウトしましたが、シンプルさのために座標変数を初期化しています) System.out.println()は、プログラムが実際にg.drawRect()メソッドまでpaintComponent()を呼び出してマウスが押された、マウスが解放されたことをキャプチャする順番に進むことを示します。私は100%肯定的な値は、私は正しい値を印刷することができますので、私はしようとする前に、形を描画するclobberedされていません。私は、複数の図形を描画することに問題があると考えていますが、その場合でも、値をハードコードして形状の色を変更しても、古い図形を変更するのではなく、新しい図形を効果的に描画します。

ヘルプ。私はパネルの周りに数回押し

public class WardA4 extends JFrame implements ActionListener{ 
    private String[] chooseShapeOptions = {"Rectangle", "Square", "Oval", "Circle", "Line", 
           "Rounded Rectangle", "3D Rectangle"}; 
    private JCheckBox chooseFill; 
    private JComboBox chooseShape; 
    private JButton chooseColor; 
    private JPanel userInterface, displayPanel; 
    private JLabel chooseFillLabel, chooseColorLabel; 
    private Color color = Color.WHITE; 
    private int shapeIndex = 0; 
    private double xStart = 100, yStart = 100, xEnd = 200, yEnd = 200; 
    private boolean isFilled; 

public WardA4(){ 
    super("Sandbox"); 
    chooseShape = new JComboBox(chooseShapeOptions); 
    chooseFill = new JCheckBox(); 
    chooseColor = new JButton(); 
    chooseFillLabel = new JLabel("Fill"); 
    chooseColorLabel = new JLabel ("Color"); 

    userInterface = new JPanel(new FlowLayout()); 
    userInterface.add(chooseShape); 
    userInterface.add(chooseFillLabel); 
    userInterface.add(chooseFill); 
    userInterface.add(chooseColorLabel); 
    userInterface.add(chooseColor); 

    displayPanel = new JPanel(){   
     public void paintComponent (Graphics g){ 
      super.paintComponent(g); 
      System.out.println("Entering paint component"); 
      System.out.println("starting coordinates are (" + xStart + "," + yStart + ")\n width is " + (int)Math.abs(xStart-xEnd) + "\n height is " + (int)Math.abs(yStart-yEnd)); 
      g.setColor(color); 
      //System.out.println("" + (int)xStart + " " + (int)yStart + " " + (int)Math.abs(xStart-xEnd) + " " + (int)Math.abs(yStart-yEnd)); 
      switch(shapeIndex){ 
       case 0: 
        if(isFilled){ 
         System.out.println("Entering is filled"); 
         g.fillRect((int)xStart, (int)yStart, (int)Math.abs(xStart-xEnd), (int)Math.abs(yStart-yEnd)); 
         //g.fillRect(100,100,100,100); 
        } 
        else{ 
         System.out.println("Entering is not filled"); 
         g.drawRect((int)xStart, (int)yStart, (int)Math.abs(xStart-xEnd), (int)Math.abs(yStart-yEnd)); 
         //g.drawRect(100,100,100,100); 
        } 
        break; 
       case 1: 
        break; 
       case 2: 
        break; 
       case 3: 
        break; 
       case 4: 
        break; 
       case 5: 
        break; 
       case 6: 
        break; 
      } 
     } 
    }; 

    displayPanel.setBackground(Color.BLACK); 

    add(displayPanel, BorderLayout.CENTER); 
    add(userInterface, BorderLayout.SOUTH); 

    chooseShape.addActionListener(this); 
    chooseFill.addActionListener(this); 
    chooseColor.addActionListener(this); 

    displayPanel.addMouseListener(new MouseAdapter(){ 
     public void mousePressed (MouseEvent me){ 
      System.out.println("Entering mouse pressed"); 
      xStart = MouseInfo.getPointerInfo().getLocation().getX(); 
      yStart = MouseInfo.getPointerInfo().getLocation().getY(); 
      System.out.println("mouse pressed at (" + xStart + "," + yStart + ")"); 
     } 
     public void mouseReleased (MouseEvent me){ 
      System.out.println("Entering mouse released"); 
      xEnd = MouseInfo.getPointerInfo().getLocation().getX(); 
      yEnd = MouseInfo.getPointerInfo().getLocation().getY(); 
      System.out.println("mouse released at (" + xEnd + "," + yEnd + ")"); 
      repaint(); 
     } 
    }); 
} 

public void actionPerformed(ActionEvent e){ 
    if (e.getSource() == chooseShape){ 
     shapeIndex = chooseShape.getSelectedIndex(); 
    } 
    else if (e.getSource() == chooseFill){ 
     isFilled = chooseFill.isSelected(); 
    } 
    else if (e.getSource() == chooseColor){ 
     color = JColorChooser.showDialog(null, "Choose color", color); 
     if (color == null) 
      color = Color.WHITE; 
    } 
    repaint(); 
} 

public static void main(String[] args) { 
    WardA4 frame = new WardA4(); 
    frame.setSize(400,300); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
} 

出力例、:

Entering paint component 
starting coordinates are (100.0,100.0) 
width is 100 
height is 100 
Entering is not filled 
Entering paint component 
starting coordinates are (100.0,100.0) 
width is 100 
height is 100 
Entering is not filled 

Entering mouse pressed 
mouse pressed at (906.0,449.0) 
Entering mouse released 
mouse released at (1092.0,612.0) 
Entering paint component 
starting coordinates are (906.0,449.0) 
width is 186 
height is 163 
Entering is not filled 

Entering mouse pressed 
mouse pressed at (1092.0,612.0) 
Entering mouse released 
mouse released at (1092.0,612.0) 
Entering paint component 
starting coordinates are (1092.0,612.0) 
width is 0 
height is 0 
Entering is not filled 
+0

ようこそ!デバッガの使い方を学ぶ必要があるようです。 [補完的なデバッグ手法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)にご協力ください。その後も問題が残っている場合は、もう少し詳しくお聞かせください。 –

答えて

1

照会されるイベントが故障している(間違った座標使用)。例えば。

xStart = MouseInfo.getPointerInfo().getLocation().getX(); // gets location ON SCREEN 
yStart = MouseInfo.getPointerInfo().getLocation().getY(); 

は次のようになります。スタックオーバーフローへ

xStart = me.getX(); // gets location relative TO PANEL 
yStart = me.getY(); 
関連する問題