0

I am trying to draw a point on the frame whenever i click the mouse on it on the same position of the click. The code I wrote/copied call the drawing function but the point isn't drawed and I can't figure out the problem. Here it's the code:

import static com.sun.java.accessibility.util.AWTEventMonitor.addMouseListener;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class MyCanvas
{
    JLabel view;
    BufferedImage surface;

    public MyCanvas()
    {
        surface = new BufferedImage(600,400,BufferedImage.TYPE_INT_RGB);
        view = new JLabel(new ImageIcon(surface));
        Graphics g = surface.getGraphics();
        g.setColor(Color.ORANGE);
        g.fillRect(0,0,600,400);
        //g.setColor(Color.BLACK);
        //g.drawLine(10, 20, 350, 380);
        g.dispose();

        /*
        // RANDOM POINTS WORKING
        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                int x = random.nextInt(600);
                int y = random.nextInt(400);
                addNewElement(x,y);
            }
        };
        Timer timer = new Timer(200, listener);
        timer.start();
        */
    }

    public void addNewElement(int x,int y) {
        Graphics g = surface.getGraphics();
        drawNode(x,y,g);
        g.dispose();
        view.repaint();
    }

    public static void main(String[] args)
    {
        MyCanvas canvas = new MyCanvas();
        JFrame frame = new JFrame();
        int vertexes = 0;
        // Change this next part later to be dynamic.
        vertexes = 10;
        int canvasSize = vertexes * vertexes;
        frame.setSize(canvasSize, canvasSize);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(canvas.view);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        frame.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                //Restituisco le coordinate del click(x,y)
                System.out.println(e.getPoint());
                //NOT WORKING
                MyCanvas a = new MyCanvas(); 
                a.addNewElement(e.getPoint().x,e.getPoint().y);
            }
        });
    }

    public void drawNode(int x, int y, Graphics g)
    {
            g.setColor(Color.white);
            g.fillOval(x, y, 8, 8);
            g.drawOval(x, y, 8, 8);
    }
}
  • So, you are creating a completely **new** canvas `a` on mouse clicks; and then you don't do anything with that canvas. What do you expect to happen from that? Hint: consider manipulating that `canvas` instance instead ... – GhostCat May 23 '16 at 06:57

2 Answers2

0

Remove:

  g.dispose();
  view.repaint();

Never dispose a GUI object you don't create. Also, repaint() will remove your point after drawing it, as it calls paint() that refreshes the image. Your point is not refreshed, as it is not included in paint. Also, note that you define the mouse event on the frame, and you apply the coordinates on the view.

AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35
0

Ok, I just figgured out the problem: I was instantiating a new class at every click. To solve it I just changed

MyCanvas a = new MyCanvas(); 
a.addNewElement(e.getPoint().x,e.getPoint().y);

to

canvas.addNewElement(e.getPoint().x,e.getPoint().y);