本文主要是介绍java设置一个窗体_java程序设计一个窗体实现每隔1s在窗体上随机显示1个红心园点...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
满意答案
udwzm
2015.12.04
采纳率:48% 等级:9
已帮助:463人
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.util.Random; import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Timer; public class TestDemo extends JPanel implements ActionListener{ final Dimension dim = new Dimension(500, 500); Graphics bg; BufferedImage sc; Timer timer = new Timer(1000, this); public TestDemo() { this.setPreferredSize(dim); sc = new BufferedImage(dim.width, dim.height, 1); bg = sc.getGraphics(); bg.setColor(Color.WHITE); bg.fillRect(0, 0, dim.width - 1, dim.height - 1); timer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(sc, 0, 0, this); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Random rnd = new Random(); int x = rnd.nextInt(dim.width), y = rnd.nextInt(dim.height), r = rnd.nextInt(10) + 10; new Circle(x, y, r).draw(bg); repaint(); } public static void main(String[] args) { JFrame frame = new JFrame("Demo"); frame.add(new TestDemo()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } class Circle { int x, y, r; public Circle(int x, int y, int r) { this.x = x; this.y = y; this.r = r; } public void draw(Graphics g) { g.setColor(Color.RED); g.fillOval(x - r, y - r, 2 * r, 2 * r); }}
00分享举报
这篇关于java设置一个窗体_java程序设计一个窗体实现每隔1s在窗体上随机显示1个红心园点...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!