📄 huatu.java
字号:
package huatu;import java.awt.*;import java.awt.geom.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.util.*;public class huatu extends JFrame { private ObjectInputStream input; private ObjectOutputStream output; private JButton choices[]; private String names[]={ "新建", "打开", "保存", "铅笔", "直线", "空心矩形", "实心矩形", "空心椭圆", "实心椭圆", "圆", "实心圆", "圆角矩形", "实心圆角矩形", "橡皮檫", "颜色", "线条粗细", "文字" }; private String styleNames[]={ " 宋体 " , " 隶书 " , " 华文彩云 " , " 仿宋_GB2312 " , " 华文行楷 " , " 方正舒体 " , " Times New Roman " , " Serif " , " Monospaced " , " SonsSerif " , " Garamond " }; JToolBar buttonPanel ; private JLabel statusBar; private DrawPanel drawingArea; private int width=800,height=600; 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 huatu() { super("Drawing Pad"); JMenuBar bar=new JMenuBar(); JMenu fileMenu=new JMenu("文件"); fileMenu.setMnemonic('F'); JMenuItem newItem=new JMenuItem("新建"); newItem.setMnemonic('N'); newItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { newFile(); } } ); fileMenu.add(newItem); JMenuItem saveItem=new JMenuItem("保存"); saveItem.setMnemonic('S'); saveItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { saveFile(); } } ); fileMenu.add(saveItem); JMenuItem loadItem=new JMenuItem("打开"); loadItem.setMnemonic('L'); loadItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { loadFile(); } } ); fileMenu.add(loadItem); fileMenu.addSeparator(); JMenuItem exitItem=new JMenuItem("退出"); 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("颜色"); colorMenu.setMnemonic('C'); JMenuItem colorItem=new JMenuItem("设置颜色"); colorItem.setMnemonic('O'); colorItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { chooseColor(); } } ); colorMenu.add(colorItem); bar.add(colorMenu); JMenu strokeMenu=new JMenu("线条"); strokeMenu.setMnemonic('S'); JMenuItem strokeItem=new JMenuItem("设置线条"); strokeItem.setMnemonic('K'); strokeItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { setStroke(); } } ); strokeMenu.add(strokeItem); bar.add(strokeMenu); JMenu helpMenu=new JMenu("帮助"); helpMenu.setMnemonic('H'); JMenuItem aboutItem=new JMenuItem("关于画图板"); aboutItem.setMnemonic('A'); aboutItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "这是一个小的画图板 由翱翔2006年制作! ", " 画图板程序说明 ", JOptionPane.INFORMATION_MESSAGE ); } } ); helpMenu.add(aboutItem); bar.add(helpMenu);//创建各种基本图形的按钮 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++) { choices[i]=new JButton(names[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 The Little Drawing Pad!!! :)"); createNewItem(); setSize(width,height); show(); }//按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作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();} } } }//按钮侦听器ButtonHanler1类,用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作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(); } } }//鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作 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() + "]"); } }//鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作 class mouseB extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) {statusBar.setText(" Mouse Dragged @:[" + e.getX() + ", " + e.getY() + "]"); if(currentChoice==3||currentChoice==13) { itemList[index-1].x1=itemList[index].x2=itemList[index].x1=e.getX(); itemList[index-1].y1=itemList[index].y2=itemList[index].y1=e.getY(); index++; createNewItem(); } else { itemList[index].x2=e.getX(); itemList[index].y2=e.getY(); } repaint(); } public void mouseMoved(MouseEvent e) {statusBar.setText(" Mouse Moved @:[" + e.getX() + ", " + e.getY() + "]");} }//选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -