📄 whiteboardframe.java
字号:
package whiteboard;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.rmi.*;
/**
* Sample application using Frame.
*
* @author
* @version 1.00 04/12/14
*/
public class WhiteboardFrame extends Frame {
/**
* The constructor.
*/
Panel p1;
CheckboxGroup cg;
Checkbox c1;
Checkbox c2;
Checkbox c3;
Checkbox c4;
Button b1;
eboard board1;
CallBack callback1;
int callbackid;
mainframe_mouseAdapter ada1=new mainframe_mouseAdapter(this);
mainframe_mouseMotionAdapter ada2=new mainframe_mouseMotionAdapter(this);
public WhiteboardFrame(String hostname) {
//this.setLayout(new FlowLayout());
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
WhiteboardFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("Whiteboard");
setMenuBar(menuBar);
setSize(new Dimension(600, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
WhiteboardFrame.this.windowClosed();
}
}
);
cg=new CheckboxGroup();
c1=new Checkbox("pen",cg,true);
c2=new Checkbox("line",cg,false);
c3=new Checkbox("rect",cg,false);
c4=new Checkbox("circle",cg,false);
b1=new Button("clear");
b1.addMouseListener(new button1_mouseAdapter(this));
p1=new Panel();
p1.add(c1);
p1.add(c2);
p1.add(c3);
p1.add(c4);
p1.add(b1);
this.add(p1,BorderLayout.NORTH);
this.addMouseListener(ada1);
this.addMouseMotionListener(ada2);
if(System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
else
System.out.println("Already has a security manager, so cant set RMI SM");
try{
//board1=new eboardServant();
board1=(eboard)Naming.lookup("rmi://"+hostname+"/WhiteBoard");
System.out.println("find remote object");
callback1=new CallBackServant(this);
callbackid=board1.regist(callback1);
System.out.println("initiate call back success");
}catch(Exception e) {
System.out.println(e.getMessage());
}
//callback1=new CallBackServant();
}
protected void whiteboardupdate()
{
try{
Vector shapelist=board1.getall();
for(int i=0;i<shapelist.size();i++)
{
shape ashape=(shape)shapelist.elementAt(i);
ashape.draw(this.getGraphics());
}
}catch(Exception e){
;
}
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
try
{
board1.deregist(callbackid);
System.out.println("deregist");
}catch(Exception e)
{
System.out.println("deregist error"+e.getMessage());;
}
System.exit(0);
}
public void paint(Graphics g)
{
whiteboardupdate();
}
}
class mainframe_mouseAdapter extends java.awt.event.MouseAdapter
{
WhiteboardFrame frame;
Point startP;
Point endP;
public mainframe_mouseAdapter(WhiteboardFrame f)
{
frame=f;
}
public void mousePressed(MouseEvent e)
{
frame.ada2.P=e.getPoint();
startP=e.getPoint();
}
public void mouseReleased(MouseEvent e)
{
shape ashape;
endP=e.getPoint();
Checkbox selectedc=frame.cg.getSelectedCheckbox();
if(selectedc!=frame.c1)
{
if(selectedc==frame.c2)
{
ashape=new line(startP,endP);
}
else if(selectedc==frame.c3)
{
ashape=new rect(startP,endP);
}
else if(selectedc==frame.c4)
{
ashape=new circle(startP,endP);
}
else
{
ashape=new circle(startP,endP);
}
try{
frame.board1.add(ashape);
System.out.println("ok");
}catch(Exception ev) {
System.out.println("Lookup: " + ev.getMessage());
}
}
//ashape.draw(frame.getGraphics());
}
}
class button1_mouseAdapter extends java.awt.event.MouseAdapter
{
WhiteboardFrame frame;
public button1_mouseAdapter(WhiteboardFrame f)
{
frame=f;
}
public void mouseClicked(MouseEvent e)
{
try{
frame.board1.clear();
System.out.println("clear");
//frame.update(frame.getGraphics());
}catch(Exception ev) {
System.out.println("Lookup: " + ev.getMessage());
}
}
}
class mainframe_mouseMotionAdapter extends java.awt.event.MouseMotionAdapter
{
WhiteboardFrame frame;
Point P;
public mainframe_mouseMotionAdapter(WhiteboardFrame f)
{
frame=f;
}
public void mouseDragged(MouseEvent e)
{
Point currentP;
currentP=e.getPoint();
Checkbox selectedc=frame.cg.getSelectedCheckbox();
if(selectedc==frame.c1)
{
shape ashape=new line(P,currentP);
try{
frame.board1.add(ashape);
System.out.println("ok");
}catch(Exception ev) {
System.out.println("Lookup: " + ev.getMessage());
}
}
//frame.getGraphics().drawLine((int)P.getX(),(int)P.getY(),(int)currentP.getX(),(int)currentP.getY());
P=currentP;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -