Note that there are some explanatory texts on larger screens.

plurals
  1. POjava swing - how to repaint all components at once so user will not notice they are filled one by one?
    primarykey
    data
    text
    <p>I have created a Hexagon component that extends <strong>JPanel</strong>. It draws a hexagon polygon in <strong>PaintComponent()</strong>. To make it fill the polygon with a given color, I created a <strong>Highlight()</strong> method that causes the component to repaint:</p> <pre><code>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(); } } </code></pre> <p>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</p> <p><img src="https://i.stack.imgur.com/k78s7.jpg" alt="Hexagons before being filled with color"> <img src="https://i.stack.imgur.com/3pIOj.jpg" alt="Hexagons during filling"> <img src="https://i.stack.imgur.com/z26Sd.jpg" alt="Hexagons after being filled"></p> <p>I tried another way of repaint by letting the containing JPanel that contains the Hexagons to repaint it self. Example:</p> <pre><code> // this code is inside the containing Jpanel that hosts all the hexagons public void highlightManyHexagons(List&lt;Hexagon&gt; 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(); } </code></pre> <p>This didn't work. Any idea how to make all the hexagons to be filled at once? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload