在Java中填充单击的区域

时间:2010-12-10 02:03:53

标签: java colors polygon fill

我正在开展一个关于图形着色的项目(使用GUI)。我有一张地图分为小多边形。当我点击其中一个多边形时,我希望它用特定的颜色填充。我怎样才能做到这一点?

我的所有事件监听器都已设置好。我可以识别我点击的区域。所以,我对哪种多边形颜色没有问题。我尝试了fillPolygon(Polygon p)方法来做到这一点,它没有用。实际上,它填满了我想要的多边形;但是,当我点击另一个多边形时,它会将新的多边形着色并删除旧的多边形。我想我知道造成这种情况的原因是:我将fillPolygon(Polygon p)放在paintComponent(Graphics g)方法中,每次启动程序时都会在我的面板上绘制完整的地图。

我在Map类中使用此方法,以在面板上绘制地图。

public void draw ( Graphics screen ) {
   screen.setColor ( Color.BLACK );
   for ( Polygon thePoly : theShapes ) 
      screen.drawPolygon ( thePoly.getPolygon() );
}

另外,我在MapPanel类中有以下行。

import java.awt.*;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.event.*;

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 */
}

如何单独使用fillPolygon(Polygon p)方法?

提前致谢。

2 个答案:

答案 0 :(得分:1)

听起来您需要保存Color的{​​{1}}作为将来渲染的属性。如果不知道你是如何构建你的UI代码,或者在你认为出错的地方有一些样本,就很难回答。

答案 1 :(得分:1)

alt text

正如蒂姆所说,你需要一个辅助数据结构来跟踪每个多边形的颜色和选择状态。查看我的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;
        }
    }

}