How to move an Oval to the coordinates of the mouseClick?
How to move an Oval to the coordinates of the mouseClick?
I am trying to build a little program in Java OOP, that will display an oval that changes its position to mouse click coordinates. However, as I am a beginner I dont know how to finish it.
Does anyone know what is wrong on my program?
public class OvalWindow
JFrame window;
int windowWidth = 500;
int windowHeight = 500;
OvalPanel panel;
public void run()
//Set a window
window = new JFrame("Oval moving");
window.setVisible(true);
window.setSize(windowWidth, windowHeight);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a panel and add it to the window
panel = new OvalPanel();
window.setContentPane(panel);
//Change the location on mouse input
panel.addMouseListener(new OvalMouseClick(panel));
public static void main(String args)
OvalWindow oWndow = new OvalWindow();
oWndow.run();
File: Oval.java
Create an Oval class
public class Oval
private int diameter;
private int x;
private int y;
private Color color;
public Oval(int x, int y, int diameter, Color color)
this.x = x;
this.y = y;
this.diameter = diameter;
this.color = color;
public int getX()
return x;
public int getY()
return y;
public int getDiameter()
return diameter;
public Color getColor()
return color;
/*
* Draw the oval
*/
public void draw(Graphics g)
g.fillOval(x, y, diameter, diameter);
g.setColor(color);
File: OvalPanel.java
This class add objects ovals to the screen.
Also, sets the initial position of the oval
public class OvalPanel extends JPanel
Oval oval;
public void addOval(Oval oval)
oval = new Oval(100, 100, 50, Color.BLACK);
this.repaint();
public void paintComponent(Graphics g)
oval.draw(g);
File: OvalMouseClick.java
This class add a new oval to the screen, at the same position as the mouse clicked coordinates.
public class OvalMouseClick extends MouseAdapter
OvalPanel panel;
public OvalMouseClick(OvalPanel panel)
this.panel = panel;
@Override
public void mouseClicked(MouseEvent e)
panel.addOval(new Oval(e.getX(), e.getY(), 50, Color.BLUE));
Just a typo in the title :) “I ma ...” to “I am ...”. You are free to undo if you want to.
– pfx
Aug 20 at 17:07
Pasting a slab of code and saying "something's wrong" is not a good way to get help. You need to tell us what's wrong.
– Kayaman
Aug 20 at 17:08
The code does not display an oval at the coordinates of the mouseClick
– Cristea
Aug 20 at 17:12
Well, you never seem to call
Oval.draw()
anywhere for one. Presumably you'd call that in OvalPanel.paintComponent()
, but you haven't implemented it.– Kayaman
Aug 20 at 17:20
Oval.draw()
OvalPanel.paintComponent()
3 Answers
3
Write a custom paintComponent()
method to make your OvalPanel
actually display something. If you added components to it, they would be displayed automatically, but your Oval
class is not a component (in the UI sense) so you'll need to explicitly write the code to display them.
paintComponent()
OvalPanel
Oval
@Override
public void paintComponent(Graphics g)
for(Oval o : ovals)
o.draw(g);
I also recommend reading the Swing trail at https://docs.oracle.com/javase/tutorial/uiswing/ because you can't just "guess" how things work. Swing is complex, and you need to understand what it does. Then you'll realize that draw(Graphics g)
and paintComponent(Graphics g)
are more than similar methods with a different name.
draw(Graphics g)
paintComponent(Graphics g)
that indeed helped me to display the oval on the window @Kayaman, but I think I need to find a way to delete the old one, as I want to move the position not to add many ovals to the panel.
– Cristea
Aug 20 at 18:05
Then why do you have a list of Ovals instead of a single
Oval
? The list would indicate that the panel can contain multiple ovals.– Kayaman
Aug 20 at 18:10
Oval
because I dont know how to store a single oval. I know is stupid, but i just dont know :(
– Cristea
Aug 20 at 18:16
You know how to store a single
OvalPanel
, you know how to store a single Color
and so on. Unless you didn't write the code you're showing?– Kayaman
Aug 20 at 18:18
OvalPanel
Color
I know some stuff, but obviously I am not understanding completely how it works yet. I also got inspired from internet.
– Cristea
Aug 20 at 18:28
In case someone is looking for the answer. Here is my solution:
I have modified the file: OvalWindow.java
public class OvalWindow
JFrame window;
OvalPanel panel;
int windowWidth = 500;
int windowHeight = 500;
/**
* Run the program
*/
public void run()
// Set a window
window = new JFrame("Oval moving");
window.setVisible(true);
window.setSize(windowWidth, windowHeight);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a panel and add it to the window
panel = new OvalPanel();
window.add(panel, BorderLayout.CENTER);
panel.addMouseListener(new MouseAdapter()
public void mouseClicked(MouseEvent e)
panel.moveToCoord(e);
);
public static void main(String args)
OvalWindow oWndow = new OvalWindow();
oWndow.run();
I have also significantly modified the file: OvalPanel.java
public class OvalPanel extends JPanel
Oval oval;
int x = 50;
int y = 10;
int diameter = 50;
Color color = Color.BLUE;
public void moveToCoord(MouseEvent e)
x = e.getX() - diameter / 2;
y = e.getY() - diameter / 2;
repaint();
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
oval = new Oval(x, y, diameter, color);
oval.draw(g);
On the file: Oval.java was only a small change:
public void draw(Graphics g)
g.setColor(color);
g.fillOval(x, y, diameter, diameter);
The file: OvalMouseClick.java was removed.
Just let the class „OvalMouseClick“ implement the interface MouseListener. If you want to do something when the mouse moves, just let the class also implement the interface „MouseMotionListener“. For the MouseWheel you can use the interface „MouseWheelListener“.
It already extends
MouseAdapter
. MouseMotionListener
and MouseWheelListener
aren't needed for this.– Kayaman
Aug 20 at 17:27
MouseAdapter
MouseMotionListener
MouseWheelListener
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What have you edited @pfx? Cuz I dont see the changes.
– Cristea
Aug 20 at 17:06