0

The paintComponent method not only draws the red circle but also a copy of the JMenu despite the fact that that area being drawn on is a JPanel not a JFrame

Screenshot of the issue

I tried doing super.paintComponent(g) but i want it to leave a trail as I am working on a paint app for Java.

Here is the primary code:

Main.java

public class Main {

    public static void main(String[] args) {
        new MainApplication();
    }

}

MainApplication.java

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

//import menu_items.PicImport;

public class MainApplication extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    private double width = screenSize.getWidth(), height = screenSize.getHeight();
    //ButtonsMenu bm = new ButtonsMenu();
    //TopMenu tm = new TopMenu();
    CanvasPanel cp = new CanvasPanel();
    JMenu fileMenu,helpMenu;
    //PicImport pi;
    JMenuBar jmb = new JMenuBar();
    MainApplication() {
        this.setSize(new Dimension((int) width - 20, (int) height - 60));
        this.setTitle("Canvas Painter");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        jmb.add(new JMenuItem("asd"));
        this.add(cp,BorderLayout.CENTER);
        //this.add(bm, BorderLayout.WEST);
        this.setJMenuBar(jmb);
        //bm.setLayout(new FlowLayout(FlowLayout.LEFT));
        //this.setLayout(null);
        this.setVisible(true);

    }
}

CanvasPanel.java

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

/**
 * This class represents the primary panel in which the user will be drawing on
 * 
 * @author 
 *
 */
public class CanvasPanel extends JPanel implements MouseListener, MouseMotionListener {
    int mouseX, mouseY;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Default Constructor for the CanvasPanel Class
     */
    public CanvasPanel() {
        addMouseMotionListener(this);
        setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
//      mouseX = e.getX();
//      mouseY = e.getY();
//      repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
        System.out.println("Lmao");
        repaint();

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        g2D.setColor(Color.RED);
        g2D.fillOval(mouseX - 15, mouseY - 20, 30, 30);

    }
}

MZX
  • 1
  • 3
  • 1
    *"I tried doing super.paintComponent(g)..."* -- and this is the *exact* solution. *"... but i want it to leave a trail as I am working on a paint app for Java."* -- well, you don't do this by skipping the super method but rather by drawing the trail in other ways, such as via use of a collection, and drawing the collection in the paintComponent via a for loop. – Hovercraft Full Of Eels Jun 03 '23 at 03:07
  • For instance, create a `List points = new ArrayList<>();`, and fill this with the mouse coordinates in your mouse listener. Then in paintComponent, call the super's method, and then iterate through the list of Points, drawing each point. Another possible solution is to draw onto a BufferedImage and then display that in paintComponent, but still and *always*, call `super.paintComponent(g);` in your override. Otherwise your JPanel cannot clean out its artifactual "dirty" pixels, which is what is causing your problem. – Hovercraft Full Of Eels Jun 03 '23 at 03:10
  • @HovercraftFullOfEels the point solution actually worked, thank you so much :) – MZX Jun 03 '23 at 03:21

0 Answers0