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

  • SEARCH
  • Home
  • 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 7674987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:51:35+00:00 2026-05-31T16:51:35+00:00

I have created a Hexagon component that extends JPanel . It draws a hexagon

  • 0

I have created a Hexagon component that extends JPanel. It draws a hexagon polygon in PaintComponent(). To make it fill the polygon with a given color, I created a Highlight() method that causes the component to repaint:

public class Hexagon{
    private Color highlightColor;
    private boolean highlighted;
    private Polygon polygon;

    public Hexagon(int width ... etc){
         // code to create the hexagon polygon to be drawn
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawPolygon(polygon);

        // highlighting
        if(highlighted){
            g2d.setColor(highlightColor);
            g2d.fillPolygon(polygon);
        }
    }

    public void highlight(Color highlightColor){
        highlight(highlightColor, false);
    }

    public void highlight(Color highlightColor, boolean dontRepaint){
        highlighted = true;
        this.highlightColor = highlightColor;
        if(dontRepaint) { return; }
        repaint();
    }
}

Now, the problem occurs when I want to highlight (fill) many hexagons at once. To highlight them I will use a for loop and call Hexagon.highlight(Color.orange). However, It will be obvious for the user that one hexagon is being filled at a time. Here is a sample image of how the filling takes place

Hexagons before being filled with color
Hexagons during filling
Hexagons after being filled

I tried another way of repaint by letting the containing JPanel that contains the Hexagons to repaint it self. Example:

 // this code is inside the containing Jpanel that hosts all the hexagons
 public void highlightManyHexagons(List<Hexagon> hexes){
     for(Hexagon h : hexes){
         // use overload that prevents the hexagon to do a repaint
         h.highlight(Color.orange, true);
     }
     // repaint the whole panel
     repaint();   
 }

This didn’t work.
Any idea how to make all the hexagons to be filled at once? Thanks.

  • 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-31T16:51:36+00:00Added an answer on May 31, 2026 at 4:51 pm

    You can try to create one Shape consisting from all the hexagons to be filled.
    Use Area class and public void add(Area rhs) method. Area can be created based on Hexagon Shape object.
    Then fill the single area.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a PHP-script to update a web server that is live inside
I have created a UserControl that has a ListView in it. The ListView is
I have created a few small flash widgets that stream .mp3 audio from an
i have created a workflow activity that do give the item creater of a
Have created simple Ajax enabled contact forms before that have around 12 fields -
I have created some forms using zendframework on my local machine that send the
I have created a SeedData class, and populated it with the values that I
Have created a custom navigation menu in wordpress that has some pages and some
I have created a little program that reads in a Java file and feeds
Have created an attribute property in TableView1.h and pushing that attributes value to another

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.