0

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.

Danny
  • 369
  • 2
  • 3
  • 11
  • Please post the signature of your class. (the lines that says `public class class_name...`) – ItamarG3 Jun 09 '17 at 13:05
  • I have a public class for the jFrame itself, so : `public class LookAndfeel extends javax.swing.JFrame` – Danny Jun 09 '17 at 13:09
  • 2
    *"It says that it cannot find symbol for:`super.paintComponent(g)`"* Well, yes, you need to override method `paintComponent(...)` as shown in the answer of MadProgrammer (in the linked question), for that you need to extend `JPanel`. Also take a look at the [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/painting/) for custom painting. If that doesn't work, post a valid [mcve] that demonstrates your issue and not code snippets – Frakcool Jun 09 '17 at 13:09
  • I add the panel manually and it is locked. I don't know whether I can add a panel with writing code in the JFrame class and get it working. I tried but it does not seem to work as there aren't any errors but no panel is visible on the screen apart from the one added manually. – Danny Jun 09 '17 at 13:26
  • 1
    *"I add them manually when I click on "Design"."* Uses a GUI designer and thinks that equates to 'manually'. .. Funny! – Andrew Thompson Jun 09 '17 at 13:41
  • Maybe I am not using the 'correct' word. I meant that I add them with the designer. – Danny Jun 09 '17 at 13:46
  • *"I add the panel manually and it is locked"*, get rid of NetBeans and start doing the GUI by hand, from scratch :) that's going to give you super powers and you'll be able to override those things easier C: @AndrewThompson he's doing it manually, he needs to use his hands to Drag & Drop the components – Frakcool Jun 09 '17 at 13:54
  • I will consider that. Do you know how to make the image appear again and again from the code from MadProgrammer? – Danny Jun 09 '17 at 14:00
  • 1
    Yes, but, first show us your best try into doing it yourself, tip add @Frakcool or whoever you want to reply – Frakcool Jun 09 '17 at 14:23
  • Every time the component is repainted, the previously drawn image is erased. You must either keep a list of the points and redraw all of them on every repaint or draw the icons in a BufferedImage and than draw the image on top of the panel on repaint. – dsboger Jun 09 '17 at 15:13
  • @Frakcool I tried doing something without using the GUI Designer, you can see the edit. I have the interface but the drawing on the JPanel is not working. – Danny Jun 10 '17 at 16:08
  • The basic problem now is that the code is using `null` layouts. As a result of that, the `TestPane` has a size of 0 x 0. The solution. ***Use layouts.*** Not the GUI designer, not `null` layouts, but sensible use of layouts is going to fix this. Go through the [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) lesson of the tutorial. Note: 1) Layouts can be combined, so choose the most logical layout for each section of the GUI. 2) Top section with the buttons is well suited to a `JToolBar`. – Andrew Thompson Jun 11 '17 at 17:10
  • @AndrewThompson I've managed to fix it and now the issue is how to keep drawing again and again. Currently, I draw once, then I click again on another location and the image disappears from the previous location and appers on the new one. The code for this is the same, I fixed the code with the layouts so the JFrame and JPanel can appear. – Danny Jun 11 '17 at 18:26
  • *"I've managed to fix it and now the issue is.."* You seem to be mistaking this Q&A site for a help desk. If this question is solved, either enter the answer below or delete it. Ask a new question (with an MCVE as mentioned by @Frakcool) and either generate images in the code or hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jun 11 '17 at 19:35
  • @AndrewThompson I've edited the code to show the part that was used for the solution. Now I asked a new question: https://stackoverflow.com/questions/44500110/draw-an-image-on-jpanel-multiple-times-with-mouse-clicks – Danny Jun 12 '17 at 15:06
  • I've already seen it. That question was marked as a duplicate 2 hours ago. – Andrew Thompson Jun 12 '17 at 15:10
  • I still can't fix it. Can you help? – Danny Jun 12 '17 at 15:19

0 Answers0