📄 example09_moveballtime.java
字号:
package example.ch09;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
public class Example09_MoveBallTime extends JFrame implements ActionListener
{
JMenuItem go;
public Example09_MoveBallTime()
{
super("Thread Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
go=new JMenuItem("Go",'g');
go.addActionListener(this);
JMenu menu=new JMenu("Start");
menu.setMnemonic('S');
menu.add(go);
JMenuBar bar=new JMenuBar();
bar.add(menu);
setJMenuBar(bar);
setVisible(true);
setExtendedState(MAXIMIZED_BOTH);
}
public void actionPerformed(ActionEvent evt)
{
go.setEnabled(false);
MovetextPanel movetext=new MovetextPanel();
DispballPanel dispball=new DispballPanel();
DisptimePanel disptime=new DisptimePanel();
Box box=Box.createVerticalBox();
box.add(movetext);
box.add(dispball);
box.add(disptime);
getContentPane().add(box);
validate();
new Thread(movetext).start();
new Thread(disptime).start();
}
public static void main(String[] args)
{
new Example09_MoveBallTime();
}
}
class MovetextPanel extends JPanel implements Runnable
{
int x;
int step=5;
int fsize;
String text="You are welcome! 欢迎光临!";
boolean move=true;
public MovetextPanel()
{
x=0;
setBackground(Color.blue);
Font font=new Font("黑体",Font.PLAIN,32);
setFont(font);
addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent evt)
{
step=0;
}
public void mouseExited(MouseEvent evt)
{
step=5;
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.yellow);
fsize=g.getFontMetrics().stringWidth(text);
g.drawString(text,x-fsize,getSize().height/2-20);
g.drawString(text,getSize().width-x,getSize().height/2+20);
}
public void run()
{
while (true)
{
if (x>getSize().width+fsize)
{
x=0;
}
x+=step;
repaint();
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
}
}
}
}
class DispballPanel extends JPanel implements Runnable,ActionListener
{
int delay=10;
JPanel p;
Color bgcolor=new Color(255,255,255);
JButton up;
JButton down;
JButton remain;
JButton addball;
JButton removeball;
JButton randomball;
int n=0;
boolean remove=false;
boolean random=false;
public DispballPanel()
{
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
addball=new JButton("增加");
removeball=new JButton("减少");
removeball.setEnabled(false);
up=new JButton("加速");
down=new JButton("减速");
remain=new JButton("轨迹");
randomball=new JButton("随机");
addball.addActionListener(this);
removeball.addActionListener(this);
up.addActionListener(this);
down.addActionListener(this);
remain.addActionListener(this);
randomball.addActionListener(this);
Box buttbox=Box.createVerticalBox();
buttbox.add(addball);
buttbox.add(removeball);
buttbox.add(up);
buttbox.add(down);
buttbox.add(remain);
buttbox.add(randomball);
p=new JPanel();
p.setBackground(bgcolor);
add(p);
add(Box.createHorizontalStrut(10));
add(buttbox);
add(Box.createHorizontalStrut(10));
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource()==up)
{
up.setEnabled(--delay!=0);
}
if (evt.getSource()==down)
{
up.setEnabled(++delay>0);
}
if (evt.getSource()==remain)
{
String text=remain.getText();
if (text.equals("轨迹"))
{
remain.setText("去除");
bgcolor=new Color(0,255,0);
}
else
{
remain.setText("轨迹");
bgcolor=new Color(255,255,255);
}
}
if (evt.getSource()==addball)
{
Thread t=new Thread(this);
t.start();
n++;
removeball.setEnabled(true);
}
if (evt.getSource()==removeball)
{
if (n>0)
{
n--;
remove=true;
}
if (n<=0)
{
removeball.setEnabled(false);
}
}
if (evt.getSource()==randomball)
{
String text=randomball.getText();
if (text.equals("随机"))
{
randomball.setText("规范");
random=true;
}
else
{
randomball.setText("随机");
random=false;
}
}
}
public void run()
{
int w=p.getSize().width;
int h=p.getSize().height;
int x=!random?0:(int)(Math.random()*w);
int y=!random?0:(int)(Math.random()*h);
int dx=!random?2:1+(int)(Math.random()*5);
int dy=!random?2:1+(int)(Math.random()*5);
int size=!random?20:10+(int)(Math.random()*40);
Color forecolor=new Color((!random?255:(int)(Math.random()*255)),(!random?0:(int)(Math.random()*255)),(!random?0:(int)(Math.random()*255)));
Graphics g=getGraphics();
while (true)
{
if (remove)
{
remove=false;
return;
}
x+=dx;
y+=dy;
if (x<0)
{
x=0;
dx=-dx;
}
if (x+size>w)
{
x=w-size;
dx=-dx;
}
if (y<0)
{
y=0;
dy=-dy;
}
if (y+size>h)
{
y=h-size;
dy=-dy;
}
g.setColor(forecolor);
g.fillOval(x,y,size,size);
try
{
Thread.sleep(delay);
}
catch (InterruptedException e)
{
}
g.setColor(bgcolor);
g.fillOval(x,y,size,size);
}
}
}
class DisptimePanel extends JPanel implements Runnable
{
SimpleDateFormat fmt;
public DisptimePanel()
{
fmt=new SimpleDateFormat("hh:mm:ss");
setBackground(Color.black);
Font font=new Font("黑体",Font.PLAIN,72);
setFont(font);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
g.drawString(fmt.format(new Date()),getSize().width/2-144,getSize().height/2);
}
public void run()
{
while (true)
{
repaint();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
}
} public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
g.drawString(fmt.format(new Date()),getSize().width/2-144,getSize().height/2);
}
public void run()
{
while (true)
{
repaint();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -