📄 centralperk.java
字号:
package Marcel;
import java.awt.*;
import java.beans.*;
import java.io.*;
import java.util.*;
public class CentralPerk extends Canvas implements Runnable {
private String msg;
private int x, y, width, height;
private int rate = 100;
private transient Thread mover;
private boolean stopped = false;
public CentralPerk () {
this ("Coffee: 10 kurus");
}
public CentralPerk (String s) {
this (s, 200, 200);
}
public CentralPerk (String s, int width, int height) {
msg = s;
this.width = width;
this.height = height;
setSize (getPreferredSize());
initMoving();
}
private void initMoving() {
mover = new Thread(this);
mover.start();
}
public Dimension getPreferredSize () {
return new Dimension (width, height);
}
public void paint (Graphics g) {
Dimension d = getSize();
if (x <= 0) {
x = d.width;
y = d.height / 2;
notifyPerked();
} else {
x--;
}
g.drawString (msg, x, y);
}
// Properties
public void setMessage(String message) {
if (msg != message) {
String oldMessage = msg;
msg = message;
repaint();
changes.firePropertyChange (
"message", oldMessage, msg);
}
}
public String getMessage () {
return msg;
}
public void setMovingRate (int rate) {
if (this.rate != rate) {
Integer oldRate = new Integer (this.rate);
this.rate = rate;
changes.firePropertyChange (
"rate", oldRate, new Integer (this.rate));
}
}
public int getMovingRate () {
return rate;
}
// Animation
public void start () {
startMoving();
}
public void stop () {
stopMoving();
}
public void run () {
try {
while(true) {
// First wait until the animation is not stopped.
synchronized (this) {
while (stopped || !isEnabled()) {
wait();
}
}
// Now draw the current frame.
repaint();
mover.sleep(rate);
}
} catch (InterruptedException e) {
// Ignore
}
}
public synchronized void setEnabled(boolean x) {
super.setEnabled(x);
notify();
}
public synchronized void startMoving() {
stopped = false;
notify();
}
public synchronized void stopMoving() {
stopped = true;
}
// Property Changing
private PropertyChangeSupport changes =
new PropertyChangeSupport (this);
public void addPropertyChangeListener (
PropertyChangeListener p) {
changes.addPropertyChangeListener (p);
}
public void removePropertyChangeListener (
PropertyChangeListener p) {
changes.removePropertyChangeListener (p);
}
// Event Listeners
private Vector perkListeners = new Vector();
public synchronized void addPerkListener (PerkListener l) {
perkListeners.addElement (l);
}
public synchronized void removePerkListener (PerkListener l) {
perkListeners.removeElement (l);
}
protected void notifyPerked () {
Vector l;
// Create Event
PerkEvent p = new PerkEvent (this, msg);
// Copy listener vector so it won't change while firing
synchronized (this) {
l = (Vector)perkListeners.clone();
}
for (int i=0;i<l.size();i++) {
PerkListener pl = (PerkListener)l.elementAt (i);
pl.startedPerking(p);
}
}
// Serialization
private void writeObject(ObjectOutputStream s)
throws IOException {
s.defaultWriteObject();
}
private void readObject(ObjectInputStream s)
throws ClassNotFoundException, IOException {
s.defaultReadObject();
initMoving();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -