总结:只是意外的收获吧。之前一篇是老师教的,一个点,从底层开始升起,到鼠标按下的地方开始画圈圈,
现在改变了一下,因为点上升的一个循环和画圈的循环是分开的
现在让点点自己跑,并且边跑边画圈。而且在for循环的最大值时终止“绽放”。很美
package com.aa; import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random; //实现一个礼花绽放的界面
import javax.swing.*; public class Test6 { public static void main(String[] args) {
JPanel jp = new JPanel();
JFrame f = new JFrame();
JLabel jl = new JLabel("周杰伦周杰伦周杰伦周杰伦周杰伦");
jl.setForeground(Color.red);// 字体设置红色
jl.setFont(new Font("", Font.ITALIC, 42));
JLabel jl2 = new JLabel("听妈妈的话");
jl2.setForeground(Color.green);
jl2.setFont(new Font("", Font.TRUETYPE_FONT, 33));
// f.setBackground(Color.black);//frame是块玻璃,不能直接添加背景颜色
f.setDefaultCloseOperation(3);
Panel p = new Panel();
p.add(jl);// 将label添加到panel里面
p.add(jl2);
p.setBackground(Color.black);// 这里还是改成
f.add(p);//
f.setVisible(true);
f.setBounds(100, 200, 500, 500);
}
} class Panel extends JPanel implements Runnable, MouseListener, ActionListener {// 实现Runnable接口 int x, y; public Panel() {
this.addMouseListener(this);
this.setBackground(Color.green);// } @Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
Thread t = new Thread(this);
t.start(); } @Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void run() {
int xx = x;
int yy = y;
int max_y = 450;
while (yy < max_y) {
Graphics g = this.getGraphics();// 绘图的方法
g.setColor(Color.yellow);
g.fillOval(xx, max_y = max_y - 10, 10, 10);
// 这里睡眠是小点点上升的时间吗?
try {
Thread.currentThread().sleep(60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.setColor(Color.black);
g.fillOval(xx, max_y, 10, 10);
// 把for循环放在while循环里了,所以点点跑哪里它就画哪里
Random r = new Random();
for (int i = 0; i < 30; i++) {
// g.setColor(Color.green);
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r
.nextInt(255)));
g.drawOval(xx - i * 1, max_y - i * 1, i * 2, i * 2);
g.drawOval(xx - i * 2, max_y - 2 * i, i * 4, i * 4);
g.drawOval(xx - i * 3, max_y - i * 3, i * 6, i * 6);
try {
Thread.currentThread().sleep(60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 这里涉及一个睡眠,这个睡眠是每一次画圈后的等待时间吗?,还是点击其他点点的间隔时间
g.setColor(Color.black);// 画笔的颜色
g.drawOval(xx - i * 1, max_y - i * 1, i * 2, i * 2);
g.drawOval(xx - i * 2, max_y - 2 * i, i * 4, i * 4);
g.drawOval(xx - i * 3, max_y - i * 3, i * 6, i * 6); } } } @Override
public void actionPerformed(ActionEvent e) { } }