2016-11-06 67 views
1

我的程序在这里设计为创建一个GUI,允许用户通过单击并释放鼠标在JPanel上绘制图形。其他选项包括更改颜色和是否填充形状。绘制一个形状后,其他选项应该使用任何新的修改器重新绘制相同的形状。例如,如果我绘制一个红色矩形,如果我选择蓝色,矩形应该只是改变颜色。到目前为止,我只实现了“矩形”选项。我的mouseListner正确地捕获和存储值,paintComponent()在值被硬编码时绘制了一个矩形(我已经注释掉了paintComponent()中的部分,但为简单起见,我正在初始化我的坐标变量),以及一系列System.out.println()显示程序按照捕获鼠标按下的顺序,释放鼠标,调用paintComponent(),直到实际的g.drawRect()方法。我是100%肯定的,价值观没有被破坏,因为我可以在尝试绘制形状之前打印正确的值。我怀疑问题是绘制多个形状,但即使如此,当我硬编码值并更改形状的颜色时,我正在有效地绘制新形状,而不是更改旧形状。PaintComponent()绘制图形,但即使在调用repaint()时也不使用更新的值

帮助。

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(); 
相关问题