importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassGame{JFrameframe;JButtonbutton;publicstaticvoidmain(String[]args){Gamed=newGame();d.go();}publicvoi...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Game {
JFrame frame;
JButton button;
public static void main(String []args){
Game d=new Game();
d.go();
}
public void go(){
frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button=new JButton("Change Color");
button.addActionListener(new button1());
Image im =new Image();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, im);
frame.setSize(600, 600);
frame.setVisible(true);
}
public class button1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
frame.repaint();
}
class Image 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,blue,green);
red = (int)(Math.random()*255);
blue=(int)(Math.random()*255);
green=(int)(Math.random()*255);
Color endColor =new Color(red,blue,green);
GradientPaint gradient=new GradientPaint(70,70,startColor,150,150,endColor);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
}
}
}
展开