public
Created

  • Download Gist
PolygonExample.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
 
package polygonexample;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
/**
*
* @author ndunn
*/
public class Main extends JPanel {
 
private static final int NUM_POLYGONS = 20;
 
private List<MapPolygon> polygons;
 
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private Random random = new Random();
public Main() {
 
polygons = new LinkedList<MapPolygon>();
for (int i = 0; i < NUM_POLYGONS; i++) {
int x1 = random.nextInt(WIDTH);
int x2 = random.nextInt(WIDTH);
int x3 = random.nextInt(WIDTH);
 
int y1 = random.nextInt(HEIGHT);
int y2 = random.nextInt(HEIGHT);
int y3 = random.nextInt(HEIGHT);
 
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
Color randomColor = new Color(r,g,b);
 
polygons.add(new MapPolygon(new int[]{x1,x2,x3}, new int[]{y1,y2,y3}, 3, randomColor));
}
 
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
for (MapPolygon mapPiece : polygons) {
if (mapPiece.contains(e.getPoint())) {
mapPiece.setSelected(!mapPiece.isSelected());
repaint();
break;
}
}
}
});
}
 
@Override
public Dimension getPreferredSize() {
return new Dimension(WIDTH, HEIGHT);
}
 
 
 
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
final Color outlineColor = Color.BLACK;
for (MapPolygon mapPiece : polygons) {
if (mapPiece.isSelected()) {
g.setColor(mapPiece.getFillColor());
g.fillPolygon(mapPiece);
}
else {
g.setColor(outlineColor);
g.drawPolygon(mapPiece);
}
}
}
 
 
 
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new Main();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
 
private class MapPolygon extends Polygon {
 
private boolean selected;
private Color fillColor;
 
public MapPolygon(int[] xpoints, int[] ypoints, int npoints, Color color) {
super(xpoints, ypoints, npoints);
this.fillColor = color;
this.selected = false;
}
 
public Color getFillColor() {
return fillColor;
}
 
public void setFillColor(Color fillColor) {
this.fillColor = fillColor;
}
 
public boolean isSelected() {
return selected;
}
 
public void setSelected(boolean selected) {
this.selected = selected;
}
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.