-2

I am trying to write a program that allows the user to draw a polygon by drawing a line every time they click within the frame. On the first click, a small square should be drawn. Every click following, a line should be drawn from where the endpoint of the last line is to where the user has clicked. Once the user clicks within the square that was made originally, the polygon will be completed and the square will disappear. My code is as follows; it runs but it does not operate correctly.

import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;    
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.util.ArrayList;
import javax.swing.JFrame;

public class DrawPolygonComponent {

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setSize(300, 400);
    frame.setTitle("Draw a Polygon");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final ArrayList<Point2D.Double> path = new ArrayList();

    //addRectangle(frame, 10, 10);
    //

    class ClickListener implements MouseListener {
        @Override
        public void mouseClicked(MouseEvent me) {
            if (path.isEmpty()) {
                System.out.println("First");
                addRectangle(frame, me.getX(),me.getY());
                path.add(new Point2D.Double(me.getX(),me.getY()));
            } else {
                System.out.println("Second");
                Point2D.Double prev = path.get(path.size()-1);
                addLine(frame, (int) prev.x, (int) prev.y,me.getX(),me.getY());
                path.add(new Point2D.Double(me.getX(),me.getY()));


                frame.repaint();
            }

        }

        public void mousePressed(MouseEvent me) {}
        public void mouseReleased(MouseEvent me) {}
        public void mouseEntered(MouseEvent me) {}
        public void mouseExited(MouseEvent me) {}
    }
    MouseListener listener = new ClickListener();
    frame.addMouseListener (listener);
    frame.setVisible (true);

}
public static void addRectangle(JFrame frame, int x , int y) {
    RectangleComponent r = new RectangleComponent(x, y, 10, 10);
    frame.add(r);
}
public static void addLine(JFrame frame, int x1, int y1, int x2, int y2) {
    LineComponent line = new LineComponent(x1, y1, x2, y2);
    frame.add(line);
}

}

/////////other

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;

public class LineComponent extends JComponent {
private int px;
private int py;
private int x;
private int y;

public LineComponent(int px, int py, int x, int y){
  this.px=px;
  this.py=py;
this.x=x;
this.y=y;
}

@Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.drawLine(px,py,x,y);
}

}


 ////////other
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;

public class RectangleComponent extends JComponent {
private Rectangle box;

public RectangleComponent(int x,int y, int l, int w){
box = new Rectangle(x,y,l,w);
}

public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.fill(box);
g2.draw(box);
}


}
BraxtonLanc
  • 11
  • 1
  • 2
  • 5
  • A little more precission. Which part of it fails? How does it fail? Just dumping your code and expecting someone to solve it does not reflect well in your efforts. – SJuan76 Apr 25 '13 at 23:46
  • I do not know what part fails. It seems to me everything is in order and should run properly but upon running it just presents an empty frame. Upon clicking it does not print out a line, nor a square. – BraxtonLanc Apr 25 '13 at 23:48
  • Well, that is more information to begin with... no expert with GUI, but you are adding components to the JFrame... what you need to do is to override the JFrame `paintComponent` method, not to add components with that method overriden. When you add them, the JFrame tries to deal with them as different entities, each with they different rectangular areas. – SJuan76 Apr 25 '13 at 23:57
  • 1
    @SJuan76 FYI - `JFrame` doesn't have a `paintComponent` method and even if it did, it would not be the recommend method to override any way - the frame contains a `JRootPane`, which as a `contentPane` which sits ontop of every thing else...however, you are on the right track – MadProgrammer Apr 25 '13 at 23:58

1 Answers1

1
  1. You've attached your mouse listener to the frame, but not provided any means for the frame to paint your paths...
  2. The component you have setup to "apparently" paint the polygon has not been added to the frame.

Instead.

Create a custom component, using something like JPanel. Attach the mouse listener to this component. Override it's paintComponent method. When a mouse event occurs (that should generate a new line), call repaint to request an update to the component.

Within the paintComponent method, re-draw all the lines.

Take a look at Java Applet Polygon array and How can I draw a polygon using path2d and see if a point is within it's area? and drawPolygon keeps drawing lines from starting (mousePressed) location to current (mouseDragged) location for some conceptional ideas

Ps You may also like to check out Performing Custom Painting

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366