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 .
source share