JPanel will not draw correctly

I am new to Java and Swing, and this is also my first post, so sorry if it doesn't make much sense.

What I'm trying to do is when I click on JPanel, I want it to add the circle that I click on. At the moment, everything that seems to happen when I click, a small little square appears inside the JPanel that I want to add, but I can’t find a way to make it draw as a circle.

I have a class that extends a JPanel called "Ball", which is added when I click. At the moment, I'm not too worried that he is in the right place, just so that he draws the ball correctly. The following is the code for my Ball class:

package paintsliders; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; class Ball extends JPanel{ private int x,y,w,h; //I will use this constructor to put the ball in the correct location later. Ball(){ /*this.w = 100; this.h = 100; this.x = 200; this.y = 200;*/ } //draw the ball @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawOval(200,200,10,10); g.setColor(Color.RED); } } 

I can guess that this has something to do with the paintComponent method, but wherever I looked, it seems like I have no solution.

Any help would be great, thanks!

+4
source share
2 answers

The Graphcis context Graphcis already been translated to match the x / y location that the component should appear in the parent container, which means that the upper left corner of the Graphics context in the paintComponent method is actually 0x0.

You need to determine some size for the ball, you draw at 10x10, which assumes your step component should return preferredSize from 10x10

 public Dimension getPreferredSize() { return new Dimension(10, 10); } 

You will be responsible for providing the appropriate layout details to the ball when it is added to the parent container ...

 public void mouseClicked(MouseEvent evt) { Point p = evt.getPoint(); Ball ball = new Ball(); Dimension size = ball.getPreferredSize(); ball.setBounds(new Rectangle(p, size)); add(ball); } 

This of course assumes that you have a null layout for the parent container

UPDATED

Sort of...

See spot run

 public class PaintBalls { public static void main(String[] args) { new PaintBalls(); } public PaintBalls() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new Board()); frame.setSize(200, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class Board extends JPanel { public Board() { setLayout(null); setBackground(Color.WHITE); addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { Point p = e.getPoint(); Ball ball = new Ball(); Dimension size = ball.getPreferredSize(); px -= size.width / 2; py -= size.height / 2; ball.setBounds(new Rectangle(p, size)); add(ball); repaint(); } }); } } public class Ball extends JPanel { public Ball() { setOpaque(false); } @Override public Dimension getPreferredSize() { return new Dimension(10, 10); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.RED); g2d.fillOval(0, 0, 10, 10); g2d.dispose(); } } } 
+5
source

You probably have the main JPanel that you click on.

I would rather have the main panel handle the mouse click and the Ball class be a simple Object that defines the drawBall(Graphics g, int x, int y) method drawBall(Graphics g, int x, int y) that knows how to draw Ball . This will be called by the paintComponent() method on the main panel. In the main panel, you process a mouse click, create an object of type Ball and call repaint() . Inside paintComponent() you call ball.drawBall() .

+4
source

Source: https://habr.com/ru/post/1439908/


All Articles