📄 minidrawpad.java
字号:
/***************************************
file name: minidrawpad
copyright by:****
dept: **********************
date: 2007.10.10
version: 1.00
*****************************************/
package MiniDrawPad;
//导入相应的包
import Java.awt.*;
import Java.awt.grom.*;
import Java.awt.event.*;
import Javax.swing.*;
import Java.io.*;
import Java.util.*;
public class MiniDrawPad extends JFrame //主类,扩展了JFrame类,用来生成主界面
{
private ObjectInputStream input;
private ObjectOutputStream output;//定义输入输出流,用来调用和保存图像文件
private JButton choices[]; //按钮数组,存放一下名称的功能按钮
private String name[]={
"New",
"Open",
"Save", //基本按钮
"Pencil", //绘制直线
"Line", //绘制空心矩形
"fRect", //绘制指定颜色填充的实心矩形
"Oval", //绘制空心椭圆
"fOval", //
"Circle",
"fcircle",
"RoundRect",
"frRect",
"Rubber",
"color",
"Stroke",
"Word",
};
private Sting styleNames[]={
"Time New Roman","Serif","Monospaced",
"sonsSerif","Garamond"
}; //可供选择的字体项
private Icon items[];
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;
private int width=800,height=550;
drawings[] itemList=new drawings[5000];
private int currentChioce=3;
int index=0;
private Color color=Color.black;
int R,G,B;
int f1,f2;
String style;
private float stroke=1.0f;
JCheckBox bold,italic;
JComboBox styles;
public MiniDrawPad()
{
super("Drawing Pad");
JMenuBar bar=new JMenuBar();
JMenu fileMenu=JMenu("File");
fileMenu.setMnemonic('F');
JMnuItem newItem=new JMenuItem("NEW");
newItem.setMnemonic('N');
newItem.addActionListener(
new ActionListener(){
public void actionperformed(ActionEvent e)
{
loadFile();
}
}
);
fileMenu.add(saveItem);
JMenItem loadItem=new JMenuItem("Load");
loadItem.setMnemonic('L');
loadItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
loadFile();
}
}
);
fileMenu.add(loadItem);
fileMenu.addSeparator();
JMenuItem exitItem=new JMenuItem("Exit");
exitItem.setMnemonic('X');
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
fileMenu.add(exitItem);
bar.add(fileMenu);
JMenu colorMenu=new JMenu("color");
colorMenu.setMnemonic('C');
JMenuItem colorItem=JMenuItem("Choose Color");
colorItem.setMnemonic('O');
colorItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
chooseColor();
}
}
);
colorMenu.add(colorItem);
bar.add(colorMenu);
JMenu strokeMenu=new JMenu("Stroke");
strokeMenu.setMnemonic('S');
JMenuItem strokeMenu=new JMenuItem("Set Stroke");
strokeItem.setMnemonic('K');
strokeItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
steStroke();
}
}
);
strokeMenu.add(strokeItem);
bar.add(strokeMenu);
JMenu helpMenu=new JMenu("Help");
helpMenu.setMnemonic('H');
JMenuItem aboutItem=new JMenuItem("About this Drawing Pad!");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,
"This is a mini drawingpad!\nCopyright(c)2002Tsinghua University",
"introduce",
JOptionPane.INFORMATION_MESSAGE);
}
}
);
helpMenu.add(aboutItem);
bar.add(helpMenu);
item=new ImageIcon[names.length];
drawingArea=new DrawPanel();
choices=new JButton[names.length];
buttonPanel=new JToolBar(JToolBar.VERTICAL);
buttonPanel=new JToolBar(JToolBar.HORIZONTAL);
ButtonHandler handler=new ButtonHandler();
ButtonHandler1 handler=new ButtonHandler();
for(int i=0;i<choices.length;i++)
{
item[i]=new ImageIcon(names[i]+".gif");
choices[i]=new JButton("",item[i]);
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
}
for(int i=3;i<choices.length-3;i++)
{
choices[i].addActionListener(handler);
}
choices[0].addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
newFile();
}
}
);
choice[1].addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
loadFile();
}
}
);
choice[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);
styles=new JComboBox(styleNames);
styles.setMaximumRowCount(8);
styles.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
style1=styleNames[styles.getSelctedIndex()];
}
}
);
bold=new JCheckBox("BOLD");
italic=new JCheckBox("ITALIC");
chackBoxHandler 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.sttText("Welcome To THE Little Drawing Pad!!:)");
createNewItem();
setSize(width,height);
show();
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int j=3;j<choice.length-3;j++)
{
if(e.getSource()==choices[j])
{currentChoice=j;
createNewItem();
repaint();}
}
}
}
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])
{setColor();}
if(e.getSource()==choices[choices.length-1])
{LOptionPane.showMessageDialog(null,
"Please hit the drawing pad to choose the word input position",
"Hint",JOptionPane.INGORMATION_MESSAGE);
currentChoice=14;
createNewItem();
repair();
}
}
}
class mouseA extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{statusBar.setText(" Mouse pressed@:["+e.getX()+","+e.getY()+"]");
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
if(currentChoice==3||currentChoice==13)
{
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
index++;
createNewItem();
}
if(currentChoice==14)
{
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
String input;
input=JOptionPane.showInputDialog(
"Please input the text you want!");
itemList[index].s1=input;
itemList[index].x2=f1;
itemList[index].y2=f2;
itemList[index].s2=style;
index++;
currentChoice=14;
createNewItem();
drawingArea.repaint();
}
}
public void mouseReleased(MouseEvent e)
{
statusBar.setText("Mouse Released@:["+e.getX()+","+e.getY()+"]");
if(currentChoice==3||curremtChoice==13)
{
itemList[index].x1=e.getX();
itemList[index].y1=e.getY();
}
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
repaint();
index++;
createNewItem();
}
public void mouseEntered(MouseEvent e)
{
statusBar.setText("Mouse Entered@:["+e.getX()+","+e.getY()+"]");
}
public void mouseExited(MouseEvent e)
{
statusBar.setText("Mouse Exited@:["+e.getX()+","+e.getY()+"]");
}
}
class mouseB extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{ statusBar.setText("Mouse Dragged@:["+e.getX()+","+e.getY()+"]");
if(currentChoice==3||currentChoice==13)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -