Java Graphics - Shape Tracking

So, I am making an application, and I want to keep track of the shapes added to the screen. So far I have the following code, but when a circle is added, it cannot be moved / resized. Ideally, I would like something like shift-click to move it / select it.

I am also interested in how I can do this so that you can drag a line from one circle to another. I don't know if I'm using the wrong tools for the job here, but any help would be appreciated.

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MappingApp extends JFrame implements MouseListener { private int x=50; // leftmost pixel in circle has this x-coordinate private int y=50; // topmost pixel in circle has this y-coordinate public MappingApp() { setSize(800,800); setLocation(100,100); addMouseListener(this); setVisible(true); } // paint is called automatically when program begins, when window is // refreshed and when repaint() is invoked public void paint(Graphics g) { g.setColor(Color.yellow); g.fillOval(x,y,100,100); } // The next 4 methods must be defined, but you won't use them. public void mouseReleased(MouseEvent e ) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseClicked(MouseEvent e) { x = e.getX(); // x-coordinate of the mouse click y = e.getY(); // y-coordinate of the mouse click repaint(); //calls paint() } public static void main(String argv[]) { DrawCircle c = new DrawCircle(); } } 
+4
source share
3 answers

Use java.awt.geom. * To create shapes, use the fields to reference them, and then use a graphic object to draw them.

eg:

 Ellipse2D.Float ellipse=new Ellipse2D.Float(50,50,100,100); graphics.draw(ellipse); 
+5
source

1) See this answer for selecting / selecting drawn objects and here for creating lines through the press and dragging the mouse.

2) You should not override JFrame paint(..) .

Rather, add a JPanel to the JFrame and override paintComponent(Graphics g) in the JPanel , super.paintComponent(g); call super.paintComponent(g); as the first call to the overriden method:

 @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.yellow); g.fillOval(x,y,100,100); } 

By paintComponent(Graphics g) docs:

In addition, if you are not making a super call, you must observe the opaque property, that is, if this component is opaque, you must completely fill the background with an opaque color. Unless you read the opaque property, you are likely to see visual artifacts.

3) Do not call setSize on the JFrame use the correct LayoutManager and / or override getPreferredSize (this is usually done when painting on the JPanel to match our graphic content), but did not call pack() on JFrame before setting it visible.

4) Read Concurrecny in Swing , especially the -Dispatch-Thread event .

+4
source

You are extending the JFrame, so you should consider calling super.paint (g); at the beginning of the overridden paint method.

0
source

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


All Articles