java - paintComponent is executing twice -


this bothering me, code works , runs when went run it, seems looping loops twice, can me logic? thanks...

package pkgcirc; import java.util.random; import javax.swing.jframe; import javax.swing.jpanel; import java.awt.*;  /* * notes: * draw 20 circles * radius/location (x/y/r) random * if (circle) between radii pt (step thru loop) of values, if within , *  draw cyan if overlaps, else black *   */ public class main extends jpanel {     int[] radius = new int [3];     int[] xarray = new int [3];     int[] yarray = new int [3];      public main()     {                random g = new random();         setpreferredsize (new dimension(300, 200));         for(int = 0; < radius.length; i++)         {             radius[i] = g.nextint(50)+1;             xarray[i] = g.nextint(250)+1;             yarray[i] = g.nextint(150)+1;         }     }      public void paintcomponent(graphics page)     {         super.paintcomponent(page);         for(int = 0; < radius.length; i++)         {             (int j = 0; j < radius.length; j++)             {                 int xpoint1 = xarray[i]+radius[i];                 int ypoint1 = yarray[i]+radius[i];                 int xpoint2 = xarray[j]+radius[j];                 int ypoint2 = yarray[j]+radius[j];                 int radius1 = radius[i];                 int radius2 = radius[j];                 boolean collide = circlescollide(xpoint1, ypoint1, radius1, radius2, xpoint2, ypoint2);                  if (i != j && collide == false)                 {                     page.setcolor(color.cyan);                     page.filloval(xarray[i] ,yarray[i], radius[i], radius[i]);                     system.out.println("false");                 }                 else{                     system.out.println("true");                     page.setcolor(color.black);                     page.drawoval(xarray[j] ,yarray[j], radius[j], radius[j]);                 }             }             system.out.println("break");             }     }      public boolean circlescollide(double x1, double y1, double r1, double x2, double y2, double r2){         return (distance(x1, y1, x2, y2) <= (r1 + r2));     }      public double distance(double x1, double y1, double x2, double y2) {         return math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));     }             public static void main (string[] args)     {         jframe frame = new jframe ("circles");         frame.setdefaultcloseoperation (jframe.exit_on_close);          frame.getcontentpane().add (new main());          frame.pack();         frame.setvisible(true);     } } 

calling setpreferredsize() , pack() cause paintcomponent() called twice because display needs redrawn each call.

try removing pack() , move set size bottom, this:-

public main() {     random g = new random();      //setpreferredsize(...); // commented line      (int = 0; < radius.length; i++) {         radius[i] = g.nextint(50) + 1;         xarray[i] = g.nextint(250) + 1;         yarray[i] = g.nextint(150) + 1;     } }  public void paintcomponent(graphics page) {         ... }  public static void main(string[] args) {     jframe frame = new jframe("circles");     frame.setdefaultcloseoperation(jframe.exit_on_close);      frame.setsize(300, 200); // added line      frame.getcontentpane().add(new main());      // frame.pack(); // commented line      frame.setvisible(true); } 

this should work now.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -