📄 minidrawpad.java
字号:
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.applet.*;
public class MiniDrawPad extends JFrame
{
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[]={
" 宋体 " , " 隶书 " , " 华文彩云 " , " 仿宋_GB2312 " , " 华文行楷 " ,
" 方正舒体 " , " Times 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 currentChoice=3;
int index=0;
private Color color=Color.black;
int R,G,B;
int f1,f2;
String style1;
private float stroke=1.0f;
JCheckBox bold,italic;
JComboBox styles;
public MiniDrawPad()
{
super("Drawing Pad");
JMenuBar bar=new JMenuBar();
JMenu fileMenu=new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem newItem=new JMenuItem("New");
newItem.setMnemonic('N');
newItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
newFile();
}
}
);
fileMenu.add(newItem);
JMenuItem saveItem=new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
saveFile();
}
}
);
fileMenu.add(saveItem);
JMenuItem 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=new 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 strokeItem=new JMenuItem("Set Stroke");
strokeItem.setMnemonic('K');
strokeItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
setStroke();
}
}
);
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,
" 制作人:计算机六班 袁佩,谈旭,彭诗节,刘彦斌 ",
" 谢谢使用我们的软件 ",
JOptionPane.INFORMATION_MESSAGE );
}
}
);
helpMenu.add(aboutItem);
bar.add(helpMenu);
items=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 handler1=new ButtonHandler1();
//导入我们需要的图形图标,这些图标都存放在与源文件相同的目录下面
for(int i=0;i<choices.length;i++)
{//items[i]=new ImageIcon( MiniDrawPad.class.getResource(names[i] +".gif"));
//如果在jbuilder下运行本程序,则应该用这条语句导入图片
items[i]=new ImageIcon(names[i] + ".gif");
//默认的在jdk或者jcreator下运行,用此语句导入图片
choices[i]=new JButton("按扭",items[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();
}
}
);
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);
//字体风格选择
styles=new JComboBox(styleNames);
styles.setMaximumRowCount(8);
styles.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
style1=styleNames[styles.getSelectedIndex()];
}
}
);
//字体选择
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(" Welcome To our Drawing Pad!!! :)");
createNewItem();
setSize(width,height);
show();
}
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();}
}
}
}
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() + "]");
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=style1;
index++;
currentChoice=14;
createNewItem();
drawingArea.repaint();
}
}
public void mouseReleased(MouseEvent e)
{statusBar.setText(" Mouse Released @:[" + e.getX() +
", " + e.getY() + "]");
if(currentChoice==3||currentChoice==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() + "]");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -