按下按钮圆圈可以改变颜色
2015-08-09 10:23
627 查看
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); } }
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(); } }
相关文章推荐
- hdu 5365 /BC 50B Run
- bzoj-3203 保护出题人
- 更多RANK37
- JAVA环境变量配置
- china-pub
- http——访问图片资源
- Java中wait,notify方法
- 【日常学习】【背包DP(完全背包)】洛谷1616 疯狂的采药题解
- linux自定义脚本添加到rc.local脚本无法正常运行的问题
- china-pub
- 生活记录(1)
- 内存泄露
- POJ 3050 Hopscotch (穷竭搜索)
- VM克隆虚拟机不能上网的一些小问题。
- POJ - 3667 Hotel (线段树 + 区间合并)
- 学点平面知识丨快速去除水印
- java i/o操作
- HDU 2759 Billboard 线段树
- Hadop使用Partitioner后,结果还是一个文件,如何解决??
- jQuery操作Select