I am trying to make a GUI in Java. I have a JFrame and I create a JPanel in it. I add them manually when I click on "Design".
When a click with the mouse on the Panel then an icon should appear where I clicked. It should appear as many times as I click on the Panel. I try to use the answer given here but it does not work as the Panel there is not added manually:
Add image on mouse click? Java applet
I am using the same code and I try to adjust it to the event MouseClicked for the jPanel1 which I have added manually:
private Point drawPoint;
Graphics g;
private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.out.println("I can click.");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("Icons/p.png");
drawPoint = new Point(evt.getPoint());
repaint();
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (drawPoint != null) {
g2d.drawImage(image, drawPoint.x, drawPoint.y, this);
}
g2d.dispose();
}
It says that it cannot find symbol for:super.paintComponent(g)
If I use the given code in the example in a newly created class, it works fine without the fact that I want the image to appear again and again instead of removing it each time and appearing only once.
Does anyone know how to fix it?
EDIT
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.imageio.ImageIO;
import javax.swing.*;
public class editorPTnets{
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
public editorPTnets(){
JFrame f = new JFrame("P/T nets");
f.setSize(800, 500);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setBackground(Color.gray);
f.add(new TestPane());
f.setLocationRelativeTo(null);
JPanel p = new JPanel();
p.setBounds(5, 50, 760, 400);
p.setBackground(Color.white);
f.add(p);
p.setVisible(true);
b1 = new JButton("");
b1.setBounds(5, 5, 40, 40);
b1.setVisible(true);
b1.setIcon(new
ImageIcon("C:\\Users\\Toshiba\\Desktop\\Java\\Icons\\new.png"));
f.add(b1);
b2 = new JButton("");
b2.setBounds(50, 5, 40, 40);
b2.setIcon(new
ImageIcon("C:\\Users\\Toshiba\\Desktop\\Java\\Icons\\open.png"));
b2.setVisible(true);
f.add(b2);
f.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new editorPTnets();
System.out.println("Hello, world");
public class TestPane extends JPanel {
Image image;
private Point drawPoint;
public TestPane() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
image =
toolkit.getImage("C:\\Users\\Toshiba\\Desktop\\Java\\Icons\\p.png");
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
drawPoint = new Point(e.getPoint());
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 500);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (drawPoint != null) {
g2d.drawImage(image, drawPoint.x, drawPoint.y, this);
}
g2d.dispose();
}
}
}
EDIT 2 FIXED
public mainFrame() {
JFrame f = new JFrame("Editor for graphs");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 600);
f.getContentPane().setBackground(Color.blue);
f.setResizable(false);
f.setLayout(new BorderLayout());
f.add(new Panel1(), BorderLayout.SOUTH);
f.setLocationRelativeTo(null);
f.setVisible(true);
This helped me fix the problem.