I'm working on a project about graph-coloring (with GUI). I have a map divided into little polygons. When I clicked on one of these polygons, I want it to be filled with a specific color. How can I do that?
I got my event listeners all set. I can recognize the area that I clicked on. So, I have no problem with which polygon I'm going to color. I tried the fillPolygon(Polygon p) method to do that, it didn't work. Actually, it filled the polygon that I want; but, when I clicked on another polygon, it colored the new one and erased the older one. I think I know what is causing this: I placed the fillPolygon(Polygon p) in the paintComponent(Graphics g) method which draws the complete map on my panel everytime I started the program.
I have this method in my Map class, to draw the map on the panel.
public void draw ( Graphics screen ) {
screen.setColor ( Color.BLACK );
for ( Polygon thePoly : theShapes )
screen.drawPolygon ( thePoly.getPolygon() );
}
Also, I have following lines in my MapPanel class.
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.eve开发者_运维技巧nt.*;
public class MapPanel extends JPanel {
private Map theMap; // collection of Regions to be colored
/* Some other variables here */
public MapPanel() {
theMap = new Map( );
this.addMouseListener( new ClickListener() );
}
public JMenuBar getMenu() {
/* Bunch of lines for the main panel, menus etc... */
}
public void paintComponent( Graphics g ) {
super.paintComponent(g);
theMap.draw ( g );
if( j != null )
g.fillPolygon( j.getPolygon() );
}
private class ClickListener implements MouseListener
{
public void mousePressed (MouseEvent event)
{
Point p = event.getPoint();
for(int i = 0; i < theMap.theShapes.size(); i++){
if( theMap.theShapes.get(i).getPolygon().contains( p ) ) {
j = theMap.theShapes.get(i);
}
}
repaint();
}
public void mouseClicked (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
}
/* Other listener classes */
}
How can I use the fillPolygon(Polygon p) method individually?
Thanks in advance.
As Tim says, you need an ancillary data structure to keep track of the color and selection state of each polygon. See my example code here
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 PolygonExample 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 PolygonExample() {
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 PolygonExample();
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;
}
}
}
It sounds like you need to save the Color
of the Polygon
as an attribute for future renderings. Without knowing how you structured your UI code or having some sample of where you think things went wrong, it is hard to answer.
精彩评论