0

so I've been working on this drawing program using Java Swing and I've separated the Drawing Frame and Drawing Panel into separate classes.

I'm not sure why but when I start drawing, it gets very glitchy and a duplicate JSlider appears at the top enter image description here

To make things more confusing, after I right click to select the colors and start drawing, a duplicate color selection screen appears at the top near the JSlider

enter image description here

For reference, here are the codes of the program

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class DrawingFrame extends JFrame{
       private Point mousePnt=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;
 
            DrawingFrame() {
                JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT));
                DrawingPanel jp = new DrawingPanel(); 
        toolbar.add(new Label("Drag mouse to draw"));
                toolbar.add(penSize);
        this.add(toolbar,BorderLayout.SOUTH);
                this.add(jp,BorderLayout.CENTER);
             
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setSize(800,600);
                setVisible(true);
            }
            
        public static void main(String[] a){
        new DrawingFrame();
    }
        
        public class DrawingPanel extends JPanel implements MouseMotionListener, MouseListener, ChangeListener {  
    public DrawingPanel(){
                addMouseMotionListener(this);
                addMouseListener(this);
                penSize.addChangeListener(this);
                setBackground(Color.white);

    }
        
        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);
            }      
        }
        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();
            }

            
        }
        
        @Override
       public void paintComponent(Graphics g){
            //g.setStroke(new BasicStroke(pen));
           
            g.setColor(penColor);
            g.fillOval(mousePnt.x,mousePnt.y,pen,pen);
       }
    
}
}

Following the comments, I added super.paintComponent(g); under paintComponent

 @Override
       public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(penColor);
            g.fillOval(mousePnt.x,mousePnt.y,pen,pen);
       }

However, this presented a new problem where the gui worked perfectly to change sizes and color but now nothing happens when I try to draw. Instead it only shows a single circle everytime I left click

enter image description here

Zaen Nik
  • 11
  • 2
  • 3
    You *must* call `super.paintComponent(g);` when you override paintComponent. Usually it should be the very first line in your paintComponent method. See https://docs.oracle.com/javase/tutorial/uiswing/painting/. – VGR Mar 24 '22 at 20:29
  • Hi so I've tried to add that line and now it presented a new problem where the gui and sliders worked fine but now I have problem drawing. Whenever I left click it only shows a single circle. I've added a screenshot to the post – Zaen Nik Mar 24 '22 at 21:40
  • *"Instead it only shows a single circle everytime I left click"* - painting in Swing is destructive. That is, every time `paintComponent` is called, you are expected to repaint the entire state, from scratch. This means that you need to maintain some kind of model of what has preciously been painted and reapply it. You could use a `BufferedImage`, but I prefer to use a model of some kind – MadProgrammer Mar 24 '22 at 21:44
  • For [example](https://stackoverflow.com/questions/23966290/java-paint-not-drawing-in-swing/23966309#23966309); [example](https://stackoverflow.com/questions/23708522/calling-paintcomponent-with-parameters-using-repaint/23708548#23708548); [example](https://stackoverflow.com/questions/14764157/mouseevent-is-not-registering-a-release-when-i-release-the-mouse-button/14764264#14764264); [example](https://stackoverflow.com/questions/12683533/drawing-a-rectangle-that-wont-disappear-in-next-paint/12683601#12683601); – MadProgrammer Mar 24 '22 at 21:52
  • [example](https://stackoverflow.com/questions/35447533/why-are-drawn-lines-automatically-erased/35447699#35447699). All of this suggests to me that really should take the time to read through [Painting in AWT and Swing](https://www.oracle.com/java/technologies/painting.html) and [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) to get a better understanding of how painting actually works – MadProgrammer Mar 24 '22 at 21:54

0 Answers0