So I'm making this drawing program using Java Swing where i can either draw normally or pick a shape and draw that.
This is how it looks like when I start the program
This is after I click on where the buttons are supposed to be
Here are the codes to the program. I'm not sure why the buttons only appear after I click on them. Any ideas?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.*;
public class DrawProgram extends JFrame implements MouseMotionListener, MouseListener, ChangeListener{
private Point mousePnt = new Point();
private Point mousePnt2 = new Point();
public static Color penColor = new Color(0,0,0);
private JSlider penSize = new JSlider(JSlider.HORIZONTAL,1,30,4);
public static int pen = 4;
private boolean toggle = false;
private int pointCounter = 1;
private int shape = 0;
JToggleButton rectangle;
JToggleButton circle;
JToggleButton triangle;
JToggleButton rhombus;
public DrawProgram(){
super("Painter");
JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel jp = new JPanel();
ImageIcon rectangleIcon = new ImageIcon(this.getClass().getResource("/images/rectangle_icon.png"));
ImageIcon circleIcon = new ImageIcon(this.getClass().getResource("/images/circle_icon.png"));
ImageIcon triangleIcon = new ImageIcon(this.getClass().getResource("/images/triangle_icon.png"));
ImageIcon rhombusIcon = new ImageIcon(this.getClass().getResource("/images/rhombus_icon.png"));
rectangle = new JToggleButton(rectangleIcon);
circle = new JToggleButton(circleIcon);
triangle = new JToggleButton(triangleIcon);
rhombus = new JToggleButton(rhombusIcon);
toolbar.add(new Label("Drag mouse to draw"));
toolbar.add(penSize);
toolbar.add(rectangle);
toolbar.add(circle);
toolbar.add(triangle);
toolbar.add(rhombus);
this.add(toolbar,BorderLayout.SOUTH);
this.add(jp,BorderLayout.CENTER);
rectangle.addItemListener(new rectangleItemListener());
circle.addItemListener(new circleItemListener());
triangle.addItemListener(new triangleItemListener());
rhombus.addItemListener(new rhombusItemListener());
jp.addMouseMotionListener(this);
jp.addMouseListener(this);
penSize.addChangeListener(this);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void mouseMoved(MouseEvent me){}
public void mouseDragged(MouseEvent me){
mousePnt = me.getPoint();
repaint();
}
public void mouseClicked(MouseEvent me){
if(me.getModifiers()== MouseEvent.BUTTON3_MASK){
penColor = JColorChooser.showDialog(null,"Change pen colour",penColor);
}
else if(me.getModifiers()== MouseEvent.BUTTON1_MASK){
if(toggle == true){
if(pointCounter == 1){
mousePnt = me.getPoint();
pointCounter = 2;
}
else{
mousePnt2 = me.getPoint();
pointCounter = 1;
repaint();
}
}
}
}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void stateChanged(ChangeEvent e){
JSlider source = (JSlider)e.getSource();
if(!source.getValueIsAdjusting()){
pen = (int)source.getValue();
}
}
public void paint(Graphics g){
g.setColor(penColor);
if(shape == 0)
g.fillOval(mousePnt.x+8,mousePnt.y+30,pen,pen);
else if(shape == 1)
paintRectangle(g);
else if(shape == 2)
paintCircle(g);
else if(shape == 3)
paintTriangle(g);
else
paintRhombus(g);
}
public void paintRectangle(Graphics g){
if(mousePnt.x <= mousePnt2.x && mousePnt.y <= mousePnt2.y) // Up Left Corner
g.drawRect(mousePnt.x+8, mousePnt.y+30, Math.abs((mousePnt.x+8)-(mousePnt2.x+8)), Math.abs((mousePnt.y+30)-(mousePnt2.y+30)));
else if(mousePnt.x <= mousePnt2.x && mousePnt.y >= mousePnt2.y) // Down Left Corner
g.drawRect(mousePnt.x+8, mousePnt2.y+30, Math.abs((mousePnt.x+8)-(mousePnt2.x+8)), Math.abs((mousePnt.y+30)-(mousePnt2.y+30)));
else if(mousePnt.x >= mousePnt2.x && mousePnt.y <= mousePnt2.y) // Up Right Corner
g.drawRect(mousePnt2.x+8, mousePnt.y+30, Math.abs((mousePnt.x+8)-(mousePnt2.x+8)), Math.abs((mousePnt.y+30)-(mousePnt2.y+30)));
else // Down Right Corner
g.drawRect(mousePnt2.x+8, mousePnt2.y+30, Math.abs((mousePnt.x+8)-(mousePnt2.x+8)), Math.abs((mousePnt.y+30)-(mousePnt2.y+30)));
}
public void paintCircle(Graphics g){
if(mousePnt.x <= mousePnt2.x && mousePnt.y <= mousePnt2.y) // Up Left Corner
g.drawOval(mousePnt.x+8, mousePnt.y+30, Math.abs((mousePnt.x+8)-(mousePnt2.x+8)), Math.abs((mousePnt.y+30)-(mousePnt2.y+30)));
else if(mousePnt.x <= mousePnt2.x && mousePnt.y >= mousePnt2.y) // Down Left Corner
g.drawOval(mousePnt.x+8, mousePnt2.y+30, Math.abs((mousePnt.x+8)-(mousePnt2.x+8)), Math.abs((mousePnt.y+30)-(mousePnt2.y+30)));
else if(mousePnt.x >= mousePnt2.x && mousePnt.y <= mousePnt2.y) // Up Right Corner
g.drawOval(mousePnt2.x+8, mousePnt.y+30, Math.abs((mousePnt.x+8)-(mousePnt2.x+8)), Math.abs((mousePnt.y+30)-(mousePnt2.y+30)));
else // Down Right Corner
g.drawOval(mousePnt2.x+8, mousePnt2.y+30, Math.abs((mousePnt.x+8)-(mousePnt2.x+8)), Math.abs((mousePnt.y+30)-(mousePnt2.y+30)));
}
public void paintTriangle(Graphics g){
if(mousePnt.y <= mousePnt2.y){
g.drawLine(mousePnt.x+8,mousePnt2.y+30,mousePnt2.x+8,mousePnt2.y+30);
g.drawLine(mousePnt.x+8,mousePnt2.y+30,(mousePnt.x+8+mousePnt2.x+8)/2,mousePnt.y+30);
g.drawLine(mousePnt2.x+8,mousePnt2.y+30,(mousePnt.x+8+mousePnt2.x+8)/2,mousePnt.y+30);
}
else{
g.drawLine(mousePnt.x+8,mousePnt.y+30,mousePnt2.x+8,mousePnt.y+30);
g.drawLine(mousePnt.x+8,mousePnt.y+30,(mousePnt.x+8+mousePnt2.x+8)/2,mousePnt2.y+30);
g.drawLine(mousePnt2.x+8,mousePnt.y+30,(mousePnt.x+8+mousePnt2.x+8)/2,mousePnt2.y+30);
}
}
public void paintRhombus(Graphics g){
g.drawLine((mousePnt.x+8+mousePnt2.x+8)/2,mousePnt.y+30,mousePnt.x+8,(mousePnt.y+30+mousePnt2.y+30)/2);
g.drawLine((mousePnt.x+8+mousePnt2.x+8)/2,mousePnt.y+30,mousePnt2.x+8,(mousePnt.y+30+mousePnt2.y+30)/2);
g.drawLine((mousePnt.x+8+mousePnt2.x+8)/2,mousePnt2.y+30,mousePnt.x+8,(mousePnt.y+30+mousePnt2.y+30)/2);
g.drawLine((mousePnt.x+8+mousePnt2.x+8)/2,mousePnt2.y+30,mousePnt2.x+8,(mousePnt.y+30+mousePnt2.y+30)/2);
}
public class rectangleItemListener implements ItemListener{
public void itemStateChanged(ItemEvent eve){
if(rectangle.isSelected()){
toggle = true;
shape = 1;
}
else{
toggle = false;
shape = 0;
}
}
}
public class circleItemListener implements ItemListener{
public void itemStateChanged(ItemEvent eve){
if(circle.isSelected()){
toggle = true;
shape = 2;
}
else{
toggle = false;
shape = 0;
}
}
}
public class triangleItemListener implements ItemListener{
public void itemStateChanged(ItemEvent eve){
if(triangle.isSelected()){
toggle = true;
shape = 3;
}
else{
toggle = false;
shape = 0;
}
}
}
public class rhombusItemListener implements ItemListener{
public void itemStateChanged(ItemEvent eve){
if(rhombus.isSelected()){
toggle = true;
shape = 4;
}
else{
toggle = false;
shape = 0;
}
}
}
public static void main(String[] a){
new DrawProgram();
}
}