📄 flyword.java
字号:
/**
@version 1.30 2001-05-06
@author Cay Horstmann
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
/**
Shows an animated bouncing ball running in a separate thread
*/
public class FlyWord
{
public static void main(String[] args)
{
JFrame frame = new FlyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
The frame with canvas and buttons.
*/
class FlyFrame extends JFrame
{
/**
Constructs the frame with the canvas for showing the
bouncing ball and Start and Close buttons
*/
public FlyFrame()
{
setSize(WIDTH, HEIGHT);
setTitle("FlyThread");
Container contentPane = getContentPane();
canvas = new WordCanvas();//建立一个用于显示字体的面板
contentPane.add(canvas, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();//建立一个用于放置按钮的面板
addButton(buttonPanel, "Start",
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
addWord();
}
});//建立Start按钮,点击执行addWord()方法
addButton(buttonPanel, "Close",
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
});//建立Close按钮,点击执行System.exit(0)方法,退出程序
contentPane.add(buttonPanel, BorderLayout.SOUTH);//把按钮面板添加入容器
}
/**
Adds a button to a container.
@param c the container
@param title the button title
@param listener the action listener for the button
*/
public void addButton(Container c, String title,
ActionListener listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}//构建具体的按钮,并且添加事件监听器
/**
Adds a bouncing ball to the canvas and starts a thread
to make it bounce
*/
public void addWord()
{
y=y+size;
color=color+50;
size=size+10;//这三行分别用于动态的改变字体的位置,颜色和大小
Word b = new Word(canvas,x,y);
b.setColor(color);
b.setFontsize(size);
canvas.add(b);
WordThread thread = new WordThread(b);
thread.start();//建立并且启动线程的运行
}
private WordCanvas canvas;
public static final int WIDTH = 450;
public static final int HEIGHT = 350;
private static int x=0,y=0,color=0,size=10;
}
/**
A thread that animates a bouncing ball.
*/
class WordThread extends Thread
{
/**
Constructs the thread.
@aBall the ball to bounce
*/
public WordThread(Word aWord) { b = aWord; }
public void run()
{
try
{
for (int i = 1; i <= 1000; i++)
{
b.move();
sleep(5);
}
}
catch (InterruptedException exception)
{
}
//run()方法用于字体的水平移动
}
private Word b;
}
/**
The canvas that draws the balls.
*/
class WordCanvas extends JPanel
{
/**
Add a ball to the canvas.
@param b the ball to add
*/
public void add(Word b)
{
words.add(b);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < words.size(); i++)
{
Word b = (Word)words.get(i);
b.draw(g2);
}
}
private ArrayList words = new ArrayList();
}
/**
A ball that moves and bounces off the edges of a
component
*/
class Word
{
/**
Constructs a ball in the upper left corner
@c the component in which the ball bounces
*/
public Word(Component c,int aX,int aY) { canvas = c;x=aX;y=aY;}
/**
Draws the word at its current position
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
String string = "Hello,World!";
g2.drawString(string,x,y);
Font f=new Font("SansSerif",Font.BOLD,fontsize);
g2.setFont(f);
g2.setPaint(new Color(colornumber));
//用于具体的显示字体
}
/**
Moves the ball to the next position, reversing direction
if it hits one of the edges
*/
public void setFontsize(int size)
{fontsize=size;}
public void setColor(int c)
{colornumber= c;}
public void move()
{ x += dx;
if (x >= canvas.getWidth())
{
x = 0;
}
canvas.repaint();//刷新面板
}
private Component canvas;
private Color color;
private static final int XSIZE = 15;
private static final int YSIZE = 15;
private int x ;
private int y ;
private int dx = 2;
private int dy = 2;
private int fontsize=0;
private int colornumber=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -