My program here is designed to create a GUI that allows the user to draw shapes on it by clicking and releasing the mouse on a JPanel. Further options include changing color and whether or not the shape is filled. Once a shape is drawn, the other options should redraw the same shape, with any new modifiers. For instance, if I draw a red rectangle, if I go to choose the color blue, the rectangle should just change color. I have thus far only implemented the "rectangle" option.
My mouseListner correctly captures and stores values, paintComponent() draws a rectangle when values are hardcoded in (I have commented out the sections in paintComponent(), but I am initializing my coordinate variables for simplicity), and a series of System.out.println() show that the program goes through the order of capturing the mouse pressed, the mouse released, calling paintComponent(), all the way down to the actual g.drawRect() methods. I am 100% positive the values aren't being clobbered, as I can print the correct values before I try and draw a shape. I suspect the problem is with drawing more than one shape, but even then, when I hardcode values in and change the color of the shape, I am effectively drawing a new shape, not changing the old one.
Help.
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);
}
An example output, where I press around on the panel a couple times:
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
The events being queried are faulty (using the wrong co-ordinates). E.G.
xStart = MouseInfo.getPointerInfo().getLocation().getX(); // gets location ON SCREEN
yStart = MouseInfo.getPointerInfo().getLocation().getY();
Should be:
xStart = me.getX(); // gets location relative TO PANEL
yStart = me.getY();