import javax.swing.*;
public class SimpleGuil
{
public static void main(String [] args)
{
JFrame frame=new JFrame();
MyDrawPanel my=new MyDrawPanel();
frame.getContentPane().add(my);
//JButton button=new JButton();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//这一行程序会在window关闭时候把程序结束掉
//frame.getContentPane().add(button);//
frame.setSize(300,300);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
int red=(int)(Math.random()*255);
int blue=(int)(Math.random()*255);
int green=(int)(Math.random()*255);
Color startColor=new Color(red,green,blue);
red=(int)(Math.random()*255);
blue=(int)(Math.random()*255);
green=(int)(Math.random()*255);
Color endColor=new Color(red,green,blue);
GradientPaint gradient=new GradientPaint(70,70,startColor,100,100,endColor);
g2d.setPaint(gradient);
g2d.fillOval(70,70,100,100);
}
}