-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleGui3C.java
52 lines (40 loc) · 1.27 KB
/
SimpleGui3C.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui3C implements ActionListener{
JFrame frame;
public static void main(String[] args) {
SimpleGui3C gui=new SimpleGui3C();
gui.go();
}
public void go(){
frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button=new JButton("change colors");
button.addActionListener(this);
MyDrawPanel drawpanel= new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawpanel);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
frame.repaint();
}
}
class MyDrawPanel extends JPanel{
public void paintComponent(Graphics g){
Graphics2D g2d=(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 gradient=new GradientPaint(70,70, startColor,150,150,endColor);
g2d.setPaint(gradient);
g2d.fillOval(70,70,100,100);
}
}