its supposed to draw an oval on MouseClicked:

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

public class PaintPad extends JPanel implements MouseListener {
    private int mouseX, mouseY;

    public static void main(String args[]) {
        PaintPad pad = new PaintPad();
        pad.setGui();
    }

    public void setGui() {
        JFrame frame = new JFrame();
        JPanel drawP = new JPanel();

        drawP.addMouseListener(this);

        frame.getContentPane().add(BorderLayout.CENTER, drawP);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 500);
        frame.setVisible(true);
    }
    
    public void paintComponent(Graphics g) {
        g.fillOval(mouseX, mouseY, 10, 10);
    }

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

   public void mousePressed(MouseEvent e){}
   public void mouseReleased(MouseEvent e){}
   public void mouseEntered(MouseEvent e){}
   public void mouseExited(MouseEvent e){}
}

works but doesnt do anything. how to fix this?

You don't need drawP . Add this to the frame, not drawP . Ditto with the MouseListener. Add the Listener to this .

Just a couple of minor but important changes in your setGui() method

public void setGui() {
        JFrame frame = new JFrame();
// remove this, you want to use your current class instance here
//        JPanel drawP = new JPanel();  

       // again, current instance
        addMouseListener(this);

        // same here
        frame.getContentPane().add(BorderLayout.CENTER, [B]this[/B]);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 500);
        frame.setVisible(true);
    }

You were creating a new JPanel object and putting that in your frame and using your existing "pad" object as a mouse listener. Since your class extends JPanel, you really want to use this instead of a new JPanel object.

Edit: heh, same time as Vernon.

reworked my code. it works mostly except for putting an oval at launch by default, which im not sure cant be fixed without a check with my architecture. also if tweaking my first post and actually making a drawP = new JPanel(), how can i achieve the same functionality?
also all but the last oval disapear on resize. is there any way around that without disabling resize and not putting mouseX/mouseY values in an array?

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

public class PaintPad {
    private int mouseX, mouseY;

    public static void main(String args[]) {
        PaintPad pad = new PaintPad();
        pad.setGui();
    }

    public void setGui() {
        JFrame frame = new JFrame();

        frame.getContentPane().add(BorderLayout.CENTER, new drawPanel());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 500);
        frame.setVisible(true);
    }

    class drawPanel extends JPanel implements MouseListener {
        MouseEvent mouseEvent; 

        public drawPanel() {
            new JPanel();
            addMouseListener(this);
        }

        public void paintComponent(Graphics g) {
            if(mouseEvent!=null) //draws an oval at launch without this
            g.fillOval(mouseX, mouseY, 10, 10);
        }

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

   public void mousePressed(MouseEvent e){}
   public void mouseReleased(MouseEvent e){}
   public void mouseEntered(MouseEvent e){}
   public void mouseExited(MouseEvent e){}
    }
}

reworked my code. it works mostly except for putting an oval at launch by default, which im not sure cant be fixed without a check with my architecture.

You already changed it so it doesn't put the oval on there without a mouse click. If you don't want to have a MouseEvent variable, you can take it out and initialize mouseX and mouseY to be -50 or something so it'll be off the canvas so you won't see it.

also if tweaking my first post and actually making a drawP = new JPanel(), how can i achieve the same functionality?

Not sure what you are asking here. You are saying that you no longer want to have a class that extends JPanel?

also all but the last oval disapear on resize. is there any way around that without disabling resize and not putting mouseX/mouseY values in an array?

I think there may be a call to JPanel's paintComponent when the resize happens, so that may be why it's clearing. If you put this line at the top of your paintComponent function:

super.paintComponent (g);

the same behavior should occur (only the last oval is visible). I think that is the root cause of why that's occurring (not positive though). As for a solution that doesn't require you to save all the coordinates in an array (or somewhere else), I don't know of any, but that doesn't mean a solution doesn't exist!

Not sure what you are asking here. You are saying that you no longer want to have a class that extends JPanel?

what i meant was is it posible to have

public void setGui() {
JFrame frame = new JFrame();
JPanel drawP = new JPanel();
drawP.addMouseListener(this);
frame.getContentPane().add(BorderLayout.CENTER, drawP);
...

do the same as the later version?
i just cant understand why my first code is flawed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.