/** the problem is that if you place ANY window ON TOP of the the java2d app window that this program *runs - when you ALT-tab inbetween, the colors change *this does NOT just happen with Colors and Java2d, if you use Random to generate a "coordinate position" for instance *the COORDs themselves will change each time you ALT Tab *do not understand what to do here */package ran2dprob;import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;public class Ran2dProb extends JFrame { public Ran2dProb() { super( "Java2d problem when using Random" ); getContentPane().setBackground( Color.gray ); } public void paint( Graphics g ) { // call superclass' paint method super.paint( g ); int xPoints[] = { 50, 100, 100, 50}; int yPoints[] = { 50, 50, 100, 100}; Graphics2D graphics2D = ( Graphics2D ) g; // start box1 GeneralPath box1 = new GeneralPath(); // set the starting point for box1 box1.moveTo( xPoints[ 0 ], yPoints[ 0 ] ); // make the rest of the box for ( int count = 1; count < xPoints.length; count++ ) box1.lineTo( xPoints[ count ], yPoints[ count ] ); // finish the box box1.closePath(); // translate the origin to (200, 200) // graphics2D.translate( 400, 400 ); // random colors for ( int count = 1; count <= 20; count++ ) { graphics2D.setColor( new Color( ( int ) ( Math.random() * 256 ), ( int ) ( Math.random() * 256 ), ( int ) ( Math.random() * 256 ) ) ); // put in on screen graphics2D.fill( box1 ); } } // end method paint // execute application public static void main( String args[] ) { Ran2dProb application = new Ran2dProb(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); application.setSize( 200, 200 ); application.setVisible( true ); }}