Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3496932
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:18:33+00:00 2026-05-18T12:18:33+00:00

I’m working on a project about graph-coloring (with GUI). I have a map divided

  • 0

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

How can I use the fillPolygon(Polygon p) method individually?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:18:34+00:00Added an answer on May 18, 2026 at 12:18 pm

    alt text

    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;
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.