试试看: import javax.swing.JFrame; import java.awt.*; import java.util.*;public class Draw7Circles extends JFrame { public static void main(String[] args) throws HeadlessException { Draw7Circles dc = new Draw7Circles(); dc.setSize(500,500); dc.setVisible(true); } public void paint(Graphics g) { super.paint(g); Random rand = new Random(System.currentTimeMillis()); int x=(int)getSize().getWidth()/2,y=(int)getSize().getHeight()/2; for(int i = 0; i < 7; i++){ int r = 200-20*i; Color c = new Color(Math.abs(rand.nextInt()) % 256, Math.abs(rand.nextInt()) % 256, Math.abs(rand.nextInt()) % 256); g.setColor(c); g.fillOval(x-r,y-r,2*r, 2*r); if(i != 6) continue; // draw the gradient color circle int cr = c.getRed(); int cg = c.getGreen(); int cb = c.getBlue(); float dr = (255-cr)/r; float dg = (255-cg)/r; float db = (255-cb)/r; int R=r; for(; r>0; r--) { int r1 = R-r; g.setColor(new Color((int)(cr+dr*r1),(int)(cg+dg*r1),(int)(cb+db*r1))); g.fillOval(x-r,y-r,2*r,2*r); } } } }
怎么实现?
import javax.swing.JFrame;
import java.awt.*;
import java.util.*;public class Draw7Circles extends JFrame { public static void main(String[] args) throws HeadlessException {
Draw7Circles dc = new Draw7Circles();
dc.setSize(500,500);
dc.setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
Random rand = new Random(System.currentTimeMillis());
int x=(int)getSize().getWidth()/2,y=(int)getSize().getHeight()/2;
for(int i = 0; i < 7; i++){
int r = 200-20*i;
Color c = new Color(Math.abs(rand.nextInt()) % 256,
Math.abs(rand.nextInt()) % 256,
Math.abs(rand.nextInt()) % 256);
g.setColor(c);
g.fillOval(x-r,y-r,2*r, 2*r);
if(i != 6)
continue;
// draw the gradient color circle
int cr = c.getRed();
int cg = c.getGreen();
int cb = c.getBlue();
float dr = (255-cr)/r;
float dg = (255-cg)/r;
float db = (255-cb)/r;
int R=r;
for(; r>0; r--) {
int r1 = R-r;
g.setColor(new Color((int)(cr+dr*r1),(int)(cg+dg*r1),(int)(cb+db*r1)));
g.fillOval(x-r,y-r,2*r,2*r);
}
}
}
}