Home > Back-end >  Consult, swing set filloval method setcolor brush color gradient, is invalid.
Consult, swing set filloval method setcolor brush color gradient, is invalid.

Time:03-10

As follows, draw method, have been used red variables, as well as variable accumulation, also set up 255 back to 0,

 import Java awt. *; 

Public class LED {
Private int x, y;//the location of the LED lights
Private int diameter;//the size of the LED lights
Private int red=0;
Private int green=0;
Private int blue=0;//the color of the LED lights

Public LED (int x, int y, int diameter) {
This.=x x;
This. Y=y;
Enclosing the diameter=diameter;
}

//, have been used red variables, but also as a variable accumulation, also set up 255 back to 0,
Void the draw Graphics (g) {
If (red==255) {
Red=0;
}
G.s etColor (new Color (red, 255, 50));
G.f illOval (x, y, diameter, diameter);
Red++;
}
}


 import Java awt. *; 

Public class TicTacToe extends Frame {
Private Image offScreenImage=null;
Leds led1=new LED (50, 50, 200);

Public static void main (String [] args) {
TicTacToe TTT=new TicTacToe ();
TTT. LaunchFrame ();
}

Public void launchFrame () {
Enclosing setLocation (400, 10);
Enclosing setSize (900, 800);
Enclosing setResizable (false);
Enclosing setTitle (" demo tic tac toe ");
Enclosing setBackground (Color. White);
Enclosing setVisible (true);
New Thread (new PaintThread ()). The start ();
}

@ Override
Public void paint (Graphics g) {
Led1. The draw (g);
}

@ Override
Public void update (Graphics g) {
If (offScreenImage==null) {
OffScreenImage=this. CreateImage (800800);
}
Graphics g_offScreen=offScreenImage. GetGraphics ();
Color=c g_offScreen. GetColor ();
G_offScreen. SetColor (Color. White);
G_offScreen. FillRect (0, 0, 900800);
G_offScreen. SetColor (c);
Paint (g_offScreen);
G.d rawImage (offScreenImage, 0, 0, null);
}

Private class PaintThread implements Runnable {
@ Override
Public void the run () {
While (true) {
Repaint ();
Try {
Thread.sleep (800);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
}
}

CodePudding user response:

1, you this is not Swing, but the AWT
2, you should actually be effective, but draw cycle is too long, you don't feel change
You change the drawing thread of sleep to below 50 ms, should can see the obvious change

CodePudding user response:

This change red=red + 30