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