发信人: TOMHZT (snail), 信区: JavaTutorial
标 题: Re: java中如何自定义消息?
发信站: BBS 水木清华站 (Mon Nov 8 19:18:46 2004), 站内
这是从书上找的一个例子:
import java.util.EventObject;
import java.awt.Image;
class SignatureEvent extends EventObject
{
protected Image signatureImage;
public SignatureEvent(Object source, Image signature)
{
super(source);
signatureImage = signature;
}
public Image getImage()
{
return signatureImage;
}
}public interface SignatureEventListener
{
public void userSigned(SignatureEvent signature);
}
import java.awt.Canvas;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
class SigningSurface extends Canvas
{
protected int width, height;
protected int x, y, lastX, lastY;
protected Image offscreenImage = null;
protected Graphics offscreenG;
public SigningSurface()
{
enableEvents(AWTEvent.MOUSE_EVENT_MASK |
AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
if (offscreenImage == null)
{
width = getSize().width;
height = getSize().height;
offscreenImage = createImage(width, height);
if (offscreenImage == null)
{
System.out.println("Error: Couldn't create offScreen.");
}
offscreenG = offscreenImage.getGraphics();
}
g.drawImage(offscreenImage, 0, 0, this);
}
public void processMouseEvent(MouseEvent mouseEvent)
{
x = mouseEvent.getX();
y = mouseEvent.getY();
switch (mouseEvent.getID())
{
case MouseEvent.MOUSE_PRESSED :
{
lastX = x;
lastY = y;
repaint();
break;
}
default:
break;
}
}
protected void processMouseMotionEvent(MouseEvent mouseMotionEvent)
{
x = mouseMotionEvent.getX();
y = mouseMotionEvent.getY();
switch (mouseMotionEvent.getID())
{
case MouseEvent.MOUSE_DRAGGED:
{
offscreenG.drawLine(x, y, lastX, lastY);
lastX = x;
lastY = y;
repaint();
break;
}
default:
break;
}
}
public Image getImage()
{
return offscreenImage;
}
public void wipeSignature()
{
wipeImage(offscreenImage, Color.white, this);
offscreenG.setColor(Color.black);
repaint();
}
public static void wipeImage(Image im, Color col, ImageObserver observer)
{
Graphics g;
int width, height;
g = im.getGraphics();
width = im.getWidth(observer);
height = im.getHeight(observer);
g.setColor(col);
g.fillRect(0, 0, width, height);
}
} //endof class SigningSurface
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SignMe extends Panel implements ActionListener
{
protected SigningSurface scribble;
protected Button eraseButton, acceptButton;
protected SignatureEventListener sigListener = null;
public SignMe()
{
Panel buttonsPanel, but1Panel, but2Panel;
setLayout(new BorderLayout());
scribble = new SigningSurface();
buttonsPanel = new Panel();
but1Panel = new Panel();
but2Panel = new Panel();
eraseButton = new Button("擦除");
acceptButton = new Button("确定");
but1Panel.add(eraseButton);
but2Panel.add(acceptButton);
buttonsPanel.add(but1Panel);
buttonsPanel.add(but2Panel);
add(scribble, "Center");
add(buttonsPanel, "South");
eraseButton.addActionListener(this);
acceptButton.addActionListener(this);
}
public void actionPerformed(ActionEvent buttonClick)
{
if (buttonClick.getSource() == eraseButton)
{
scribble.wipeSignature();
}
if (buttonClick.getSource() == acceptButton)
{
System.out.println("user accepted signature!");
fireSignedEvent();
}
}
public void addSignatureListener(SignatureEventListener listener)
throws java.util.TooManyListenersException
{
if (sigListener != null)
{
throw new TooManyListenersException("SignMe already has an event listener");
}
sigListener = listener;
}
public void removeSignatureListener(SignatureEventListener listener)
{
if (sigListener == null)
{
throw new IllegalArgumentException("Can't remove SignMe event");
}
if (sigListener != listener)
{
throw new IllegalArgumentException("different from currently set!");
}
sigListener = null;
}
protected void fireSignedEvent()
{
SignatureEvent event;
if (sigListener != null)
{
event = new SignatureEvent(this, scribble.getImage());
sigListener.userSigned(event);
}
}
} //end of class SignMe
import java.awt.Frame;
import utilities.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class SignMeClient extends Frame implements SignatureEventListener
{
protected SignMe sigWindow;
public static void main(String[] args)
{
new SignMeClient();
}
public SignMeClient()
{
super("Sign Me Demo Window");
sigWindow = new SignMe();
try
{
sigWindow.addSignatureListener(this);
}
catch (TooManyListenersException ex)
{
System.out.println("Could not register " + ex);
}
add(sigWindow, "Center");
setSize(new Dimension(350, 250));
setVisible(true);
}
public void userSigned(SignatureEvent signed)
{
Frame receivedSignWindow;
Graphics g;
Image sigImage;
int width, height;
receivedSignWindow = new Frame("Received Signature");
sigImage = signed.getImage();
width = sigImage.getWidth(this);
height = sigImage.getHeight(this);
receivedSignWindow.setSize(new Dimension(width + 20, height + 20));
receivedSignWindow.setVisible(true);
MiscKit.delay(300);
g = receivedSignWindow.getGraphics();
int x = receivedSignWindow.getInsets().left;
int y = receivedSignWindow.getInsets().top;
g.drawImage(sigImage, x, y, receivedSignWindow);
MiscKit.delay(1000);
receivedSignWindow.setVisible(false);
}
}
【 在 liyuexp (雁南飞) 的大作中提到: 】
: 如何在java中自定义消息 并且接受处理 请给个例子
--
※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.112.78.*]