4

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

I'm also wondering how I could make it 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();
  }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Tim
  • 1,056
  • 4
  • 17
  • 34

3 Answers3

5

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

for eg:

Ellipse2D.Float ellipse=new Ellipse2D.Float(50,50,100,100);

graphics.draw(ellipse);
Nikki
  • 3,664
  • 2
  • 15
  • 14
  • graphics is pseudo. I'm referring to whatever graphics object you're using. The example you have uses 'g' as the reference. Try g.draw() – Nikki Jun 28 '13 at 22:44
  • +1 for painting with Shapes. However, the Graphics class does not know how to paint Shapes. You need to use the `Graphics2D` class. Also you should use the `fill()` method. The draw() method just paints the outline of the Shape. See [Playing With Shapes](http://tips4java.wordpress.com/2013/05/13/playing-with-shapes/) for more information. – camickr Jun 29 '13 at 00:28
4

1) See this answer for clicking/selecting a drawn objects and here for creating lines via press, and drag of mouse.

2) You should not be overriding JFrame paint(..).

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

@Override
protected void paintComponent(Graphics g) {
   super.paintComponent(g);

   g.setColor(Color.yellow);
   g.fillOval(x,y,100,100);

}

As per paintComponent(Graphics g) docs:

Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.

3) Dont call setSize on JFrame use correct LayoutManager and/or override getPreferredSize (usually done when drawing to JPanel so it may fit our Graphic content) and than call pack() on JFrame before setting it visible.

4) Have a read on Concurrecny in Swing especially Event-Dispatch-Thread.

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
0

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