📄 whiteboardframe.java
字号:
package whiteboard;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.util.Vector;
import java.rmi.*;
public class WhiteboardFrame extends JFrame
{
boolean saved = false;
File savedFile;
eboard board1;
CallBack callback1;
int callbackid;
String name;
boolean connected=false;
String hostname;
boolean update=false;
// IO
private ObjectInputStream input;
private ObjectOutputStream output;
private JButton choices[];
private String names[]={
"New",
"Open",
"Save",
"Pencil",
"Line",
"Rect",
"fRect",
"Oval",
"fOval",
"Circle",
"fCircle",
"RoundRect",
"frRect",
"Rubber",
"Color",
"Stroke",
"Word"
};
private String styleNames[]={
" Times New Roman " , " Serif " , " Monospaced " ,
" SonsSerif " , " Garamond "
};
//font style
private Icon items[];
// explaination of icon's function
private String tipText[]={
"Draw a new picture",
"Open a saved picture",
"Save current drawing",
"Draw at will",
"Draw a straight line",
"Draw a rectangle",
"Fill a ractangle",
"Draw an oval",
"Fill an oval",
"Draw a circle",
"Fill a circle",
"Draw a round rectangle",
"Fill a round rectangle",
"Erase at will",
"Choose current drawing color",
"Set current drawing stroke",
"Write down what u want"
};
JToolBar buttonPanel ;
private JLabel statusBar;
private DrawPanel drawingArea;
// initial frame size
private int width=1000,height=800;
drawings[] itemLista=new drawings[5000];
drawings anitem=new drawings();
Vector itemlist;
int itemtempy1;
int itemtempx1;
int pressedtempy1;
int pressedtempx1;
private int currentChoice=3; //set default tool as pen
int index=0;
private Color color=Color.black; // set default color as black
int R,G,B; // store RBG color values
int f1,f2; // store font style
String style1; // store font style
private float stroke=1.0f; // set default stroke size as 1.0f
JCheckBox bold,italic; //name od font style
JComboBox styles;
public WhiteboardFrame(String hostname,String name) //constructor
{
super(name);
this.name=name;
this.hostname=hostname;
JMenuBar bar=new JMenuBar();
JMenu fileMenu=new JMenu("File");
fileMenu.setMnemonic('F');
// new file
JMenuItem newItem=new JMenuItem("New");
newItem.setMnemonic('N');
newItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
newFile();
}
}
);
fileMenu.add(newItem);
// save file
JMenuItem saveItem=new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
saveFile();
}
}
);
fileMenu.add(saveItem);
// save as file
JMenuItem saveAsItem=new JMenuItem("Save As");
saveAsItem.setMnemonic('A');
saveAsItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
saveAsFile();
}
}
);
fileMenu.add(saveAsItem);
// open file
JMenuItem loadItem=new JMenuItem("Load");
loadItem.setMnemonic('L');
loadItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
loadFile();
}
}
);
fileMenu.add(loadItem);
fileMenu.addSeparator();
// exit program
JMenuItem exitItem=new JMenuItem("Exit");
exitItem.setMnemonic('X');
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
WhiteboardFrame.this.windowClosed();
}
}
);
fileMenu.add(exitItem);
bar.add(fileMenu);
// set color menu
JMenu colorMenu=new JMenu("Color");
colorMenu.setMnemonic('C');
// set color item
JMenuItem colorItem=new JMenuItem("Choose Color");
colorItem.setMnemonic('O');
colorItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
chooseColor();
}
}
);
colorMenu.add(colorItem);
bar.add(colorMenu);
// set stroke menu
JMenu strokeMenu=new JMenu("Stroke");
strokeMenu.setMnemonic('S');
// set stroke item
JMenuItem strokeItem=new JMenuItem("Set Stroke");
strokeItem.setMnemonic('K');
strokeItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
setStroke();
}
}
);
strokeMenu.add(strokeItem);
bar.add(strokeMenu);
// set help menu
JMenu helpMenu=new JMenu("Help");
helpMenu.setMnemonic('H');
// set about item
JMenuItem aboutItem=new JMenuItem("About this white board!");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,
"White board ",
" White board ",
JOptionPane.INFORMATION_MESSAGE );
}
}
);
helpMenu.add(aboutItem);
bar.add(helpMenu);
items=new ImageIcon[names.length];
// set draw function buttons
drawingArea=new DrawPanel();
choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;
ButtonHandler handler=new ButtonHandler();
ButtonHandler1 handler1=new ButtonHandler1();
// import icons
for(int i=0;i<choices.length;i++)
{
items[i]=new ImageIcon(names[i] + ".gif");
choices[i]=new JButton("",items[i]);
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
}
//add listemer to bottons
for(int i=3;i<choices.length-3;i++)
{
choices[i].addActionListener(handler);
}
choices[0].addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
newFile();
}
}
);
choices[1].addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
loadFile();
}
}
);
choices[2].addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
saveFile();
}
}
);
choices[choices.length-3].addActionListener(handler1);
choices[choices.length-2].addActionListener(handler1);
choices[choices.length-1].addActionListener(handler1);
// font style
styles=new JComboBox(styleNames);
styles.setMaximumRowCount(8);
styles.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
style1=styleNames[styles.getSelectedIndex()];
}
}
);
//font style
bold=new JCheckBox("BOLD");
italic=new JCheckBox("ITALIC");
checkBoxHandler cHandler=new checkBoxHandler();
bold.addItemListener(cHandler);
italic.addItemListener(cHandler);
JPanel wordPanel=new JPanel();
buttonPanel.add(bold);
buttonPanel.add(italic);
buttonPanel.add(styles);
styles.setMinimumSize( new Dimension ( 50, 20 ) );
styles.setMaximumSize(new Dimension ( 100, 20 ) );
Container c=getContentPane();
super.setJMenuBar( bar );
c.add(buttonPanel,BorderLayout.NORTH);
c.add(drawingArea,BorderLayout.CENTER);
statusBar=new JLabel();
c.add(statusBar,BorderLayout.SOUTH);
statusBar.setText(" Distributed white board :)");
createNewItem();
setSize(width,height);
show();
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
WhiteboardFrame.this.windowClosed();
}
}
);
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");
// board1=(eboard)Naming.lookup("rmi://"+hostname+":"+"1999"+"/WhiteBoard");
System.out.println("find remote object");
callback1=new CallBackServant(this);
//board1.addEraser(eraser);
System.out.println("add eraser");
//itemlist=board1.getEraser();
callbackid=board1.regist(callback1,name);
// board1.add(anitem);
System.out.println("initiate call back success");
}catch(Exception e) {
System.out.println(" cannot connect");
System.out.println(e.getMessage());
// System.exit(0);
}
}
protected void showWarning(String warning){
/*
JOptionPane window =new JOptionPane();
window.showMessageDialog(this,warning,"",JOptionPane.WARNING_MESSAGE);
*/
JDialog dialog = new JDialog(this, "Warning ");
//Add contents to it.
JLabel label = new JLabel(warning);
label.setHorizontalAlignment(JLabel.CENTER);
Container contentPane = dialog.getContentPane();
contentPane.add(label, BorderLayout.CENTER);
//Show it.
dialog.setSize(new Dimension(300, 150));
dialog.setLocationRelativeTo(this);
dialog.setVisible(true);
}
protected void whiteboardupdate()
{
update=true;
try{
if(board1.getConnectStatus(callbackid)){
itemlist=board1.getall();}
repaint();
}catch(Exception e){
;
}
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
try
{
if (name.compareTo("SERVER") ==0)
{
board1.notifyallwarning("Server has left");
}
board1.deregist(callbackid);
System.out.println("deregist");
}catch(Exception e)
{
System.out.println("deregist error"+e.getMessage());;
}
System.exit(0);
}
// listen to function buttons
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int j=3;j<choices.length-3;j++)
{
if(e.getSource()==choices[j])
{currentChoice=j;
createNewItem();
repaint();}
}
}
}
// listen to color stroke and text
public class ButtonHandler1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==choices[choices.length-3])
{chooseColor();}
if(e.getSource()==choices[choices.length-2])
{setStroke();}
if(e.getSource()==choices[choices.length-1])
{JOptionPane.showMessageDialog(null,
"Please hit the drawing pad to choose the word input position",
"Hint",JOptionPane.INFORMATION_MESSAGE );
currentChoice=14;
createNewItem();
repaint();
}
}
}
class mouseA extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{statusBar.setText(" Mouse Pressed @:[" + e.getX() +
", " + e.getY() + "]");
itemLista[index].x1=itemLista[index].x2=e.getX();
itemLista[index].y1=itemLista[index].y2=e.getY();
anitem.x1=anitem.x2=e.getX();
anitem.y1=anitem.y2=e.getY();
itemtempy1=anitem.y1;
itemtempx1=anitem.x1;
pressedtempy1 = anitem.y1;
pressedtempx1 = anitem.x1;
// if pen or eraser selected do following
if(currentChoice==3||currentChoice==13)
{
itemLista[index].x1=itemLista[index].x2=e.getX();
itemLista[index].y1=itemLista[index].y2=e.getY();
index++;
anitem.x1=anitem.x2=e.getX();
anitem.y1=anitem.y2=e.getY();
itemtempy1=anitem.y1;
itemtempx1=anitem.x1;
try{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -