0

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 how it looks like when I start the program

This is after I click on where the buttons are supposed to be enter image description here

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();
    }
}
Zaen Nik
  • 11
  • 2
  • 1
    It has been years since I did anything in Swing, but I believe the problem is that you have overridden `paint(Graphics)`, which means the component doesn't draw itself properly. See also [Overriding the paint() method](https://stackoverflow.com/questions/34036540/overriding-the-paint-method) – Mark Rotteveel Mar 24 '22 at 17:19
  • I've been trying to convert the paint into paintComponent as you said, but I'm a bit stuck on how to do it. For now, I've managed to make a separate public class painting extends JPanel just for the paintComponent but I think it's started another problem with the mouse listener – Zaen Nik Mar 24 '22 at 19:04
  • If you want to paint on part of your GUI, make that a separate JPanel. So subclass JPanel and override paint as you have done in that subclass, and then put that JPanel on CENTER of your main application. The paint stuff would all go there, and the mouse stuff would stay where it is. I hope that makes sense. – ferzle Mar 24 '22 at 19:39
  • *I've been trying to convert the paint into paintComponent* - See [Custom Painting Approaches](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/). It contains tutorials for you to read to learn basic painting along with working examples to get you started. – camickr Mar 24 '22 at 19:57

0 Answers0