50,526
社区成员
发帖
与我相关
我的任务
分享
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D graphics2D = (Graphics2D) g;
int red = (int) (Math.random()*255);
int green = (int) (Math.random()*255);
int blue = (int) (Math.random()*255);
Color startColor = new Color(red,green,blue);
red = (int) (Math.random()*255);
green = (int) (Math.random()*255);
blue = (int) (Math.random()*255);
Color endColor = new Color(red,green,blue);
GradientPaint gradientPaint = new GradientPaint(70,70,startColor,150,150,endColor);
graphics2D.setPaint(gradientPaint); //将虚拟的笔刷画成渐层
graphics2D.fillOval(70,70,100,100); //用目前的笔刷来填满椭圆形区域
}
}
public class SimpleGUI implements ActionListener {
JFrame frame;
public static void main(String[] args){
SimpleGUI gui = new SimpleGUI();
gui.go();
}
public void actionPerformed(ActionEvent event){
frame.repaint();
}
public void go(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭即停止
JButton button = new JButton("do not click") ;
button.addActionListener(this);
MyDrawPanel drawPanel = new MyDrawPanel(); //创建我们的myPanel对象
frame.add(BorderLayout.CENTER,drawPanel); //add进frame框架中
frame.add(BorderLayout.SOUTH,button);
frame.setSize(300,300);
frame.setVisible(true); //设置框架为可见
}
}