//file ShapesPaths.java
// showing off primitives
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.*;
import java.util.Random;

public class ShapesPaths extends JComponent {

public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D)g;
	int wd = getSize().width;
	int ht = getSize().height;
 	//System.out.println(wd);
	//System.out.println(ht);

// 10 black points - with dots drawn over them
// upper left 
// Use drawLine(x, y, x, y) to set a Pixel
	Random rnums = new Random();	
	g2.setStroke(new BasicStroke(1));
	g2.setPaint(Color.black);
	for (int pnum = 0; pnum < 10; pnum++){
		Point2D.Double P = new Point2D.Double(rnums.nextInt(wd)/2,rnums.nextInt(ht)/2);	
		// This draws single pixels.
		g2.drawLine((int)P.x, (int)P.y, (int)P.x, (int)P.y);	
		// This draws dots over the pixels so you can see them.
		g2.fillOval((int)P.x-4,(int)P.y-4,8,8);	
	}

// 10 black points - connected
// upper right
	//Random rnums = new Random();	
	g2.setStroke(new BasicStroke(1));
	g2.setPaint(Color.black);
	Point2D P = new Point2D.Double((wd)/2 + rnums.nextInt(wd)/2,rnums.nextInt(ht)/2);	
	for (int pnum = 1; pnum < 10; pnum++){
		Point2D Q = new Point2D.Double((wd)/2 + rnums.nextInt(wd)/2,rnums.nextInt(ht)/2);
		Line2D line = new Line2D.Double(P,Q);
		g2.draw(line);
		P = Q;		
	}

// a shape with boxes at the corners
// lower left		
	Shape lineShape = createLineShape2(wd,ht);
	g2.setPaint(Color.black);
	g2.draw(lineShape);
	g2.setPaint(Color.red);
	g2.fill(lineShape);
	
	
	
// a shape
	g2.setPaint(Color.orange);
	g2.setStroke(new BasicStroke(3));
	Shape s = createShape(  );
    g2.fill(s);
   
// bounding box
	g2.setPaint(Color.black);
	Rectangle2D bounds = s.getBounds2D();
	g2.draw(bounds);

}

//==========================================================
Shape createLineShape(int wd, int ht) {
    GeneralPath linePath = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
    //GeneralPath linePath = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  
  	Random rnums = new Random();
	linePath.moveTo(rnums.nextInt(wd),rnums.nextInt(ht));
	for (int pnum = 1; pnum < 10; pnum++){
		linePath.lineTo(rnums.nextInt(wd),rnums.nextInt(ht));
	}
    linePath.closePath();
    return linePath; 
    
  }
  
Shape createLineShape2(int wd, int ht) {
    //GeneralPath linePath = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
    GeneralPath linePath = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  
  	Random rnums = new Random();
	Point2D.Double P = new Point2D.Double(rnums.nextInt(wd)/2,ht/2 + rnums.nextInt(ht)/2);
	linePath.moveTo((int)P.x,(int)P.y);
	linePath.append(new Rectangle2D.Double(P.x,P.y,10,10),false);
	for (int pnum = 1; pnum < 10; pnum++){
		P = new Point2D.Double(rnums.nextInt(wd/2),ht/2 + rnums.nextInt(ht)/2);
		linePath.lineTo((int)P.x,(int)P.y);
		linePath.append(new Rectangle2D.Double(P.x,P.y,10,10),false);
		
	}
    linePath.closePath();
    return linePath; 
    
  }
  
Shape createShape(  ) {
	int base = 400;
	int size = 350;
	int xValues[] = {base, base+size, base+size, base, base+size, base+size/2};
	int yValues[] = {base, base+size/2, base, base+size/2, base+size, base};
//Try this out with each of these:
    //GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
    GeneralPath path = new GeneralPath(GeneralPath.WIND_NON_ZERO);
    
    path.moveTo(xValues[0], yValues[0]);
    for (int i = 1; i < 6; i += 1)
    	path.lineTo(xValues[i], yValues[i]);
    path.closePath(  );

   	return path;   
  }

public static void main(String[] args){
	JFrame f = new JFrame("ShapesPaths");
	Container c = f.getContentPane();
	c.setLayout(new BorderLayout());
	c.add(new ShapesPaths(), BorderLayout.CENTER);
	f.setSize(800, 822);

	f.addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e) { System.exit(0); }
	});

	f.setVisible(true);
}

}