import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sun.java2d.loops.DrawRect;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
import java.awt.geom.Line2D;
public class paintBoard extends JFrame {
private static int Width=500;
private static int Height=600;//设置窗口大小
private JLabel statusBar; //显示鼠标状态的提示条
String timeString="";
JPanel panel;
private ObjectInputStream input;
private ObjectOutputStream output; //定义输入输出流,用来调用和保存图像文件
private DrawPanel drawingArea; //画图区域
drawings[] itemList=new drawings[5000]; //用来存放基本图形的数组
private int currentChoice=1; //设置默认画图状态为随笔画
int index=0; //当前已经绘制的图形数目
private Color color=Color.black; //当前画笔颜色
private float stroke=1.0f; //设置画笔粗细,默认值为1.0f
int R,G,B; //用来存放当前色彩值
int f1,f2; //用来存放当前字体风格
String style1; //用来存放当前字体
int music_flag=0;
public paintBoard(){
setTitle("迷你小画板");//设置标题
setSize(Width,Height);//设置窗口
setAlwaysOnTop(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the window always the center of the current window
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation( (scr.width - frm.width) / 2,
(scr.height - frm.height) / 2 - 18);
//addMouseMotionListener(this); //注册监听器 实现鼠标移动接口
//addMouseListener(this); //注册监听器 实现鼠标按钮接口
drawingArea=new DrawPanel();
JMenuBar bar=new JMenuBar();//设置菜单栏
setJMenuBar(bar);
JMenu fileMenu=new JMenu("文件");//创建菜单对象fileMenu
JMenu paintMenu=new JMenu("图像");//创建菜单对象paintMenu
JMenu fillMenu=new JMenu("填充图像");//创建菜单对象fillMenu
JMenu musicMenu=new JMenu("音乐");
JMenu otherMenu=new JMenu("其它");//创建菜单对象otherMenu
JMenu helpMenu=new JMenu("帮助");//创建菜单对象helpMenu
fileMenu.setForeground(Color.BLUE);
paintMenu.setForeground(Color.BLUE);
fillMenu.setForeground(Color.BLUE);
musicMenu.setForeground(Color.BLUE);
otherMenu.setForeground(Color.BLUE);
helpMenu.setForeground(Color.BLUE);
fileMenu.setFont(new Font("华文行楷", Font.PLAIN, 20));
paintMenu.setFont(new Font("华文行楷", Font.PLAIN, 20));
fillMenu.setFont(new Font("华文行楷", Font.PLAIN, 20));
musicMenu.setFont(new Font("华文行楷", Font.PLAIN, 20));
otherMenu.setFont(new Font("华文行楷", Font.PLAIN, 20));
helpMenu.setFont(new Font("华文行楷", Font.PLAIN, 20));
bar.add(fileMenu);
bar.add(paintMenu);
bar.add(fillMenu);
//bar.add(musicMenu);
bar.add(otherMenu);
bar.add(helpMenu);
JMenuItem newItem=new JMenuItem("新建(N)");//创建菜单项对象newItem
newItem.setFont(new Font("华文行楷", Font.PLAIN, 20));
newItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
index=0;
currentChoice=3;
color=Color.black;
stroke=1.0f;
createNewItem();
repaint();//将有关值设置为初始状态,并且重画
}
});
JMenuItem openItem=new JMenuItem("打开(O)");//创建菜单项对象openItem
openItem.setFont(new Font("华文行楷", Font.PLAIN, 20));
openItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result =fileChooser.showOpenDialog(paintBoard.this);
if(result==JFileChooser.CANCEL_OPTION)
return ;
File fileName=fileChooser.getSelectedFile();
fileName.canRead();
if (fileName==null||fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
else {
try {
FileInputStream fis=new FileInputStream(fileName);
input=new ObjectInputStream(fis);
drawings inputRecord;
int countNumber=0;
countNumber=input.readInt();
for(index=0;index< countNumber ;index++)
{
inputRecord=(drawings)input.readObject();
itemList[ index ] = inputRecord ;
}
createNewItem();
input.close();
repaint();
}
catch(EOFException endofFileException){
JOptionPane.showMessageDialog(paintBoard.this,"no more record in file",
"class not found",JOptionPane.ERROR_MESSAGE );
}
catch(ClassNotFoundException classNotFoundException){
JOptionPane.showMessageDialog(paintBoard.this,"Unable to Create Object",
"end of file",JOptionPane.ERROR_MESSAGE );
}
catch (IOException ioException){
JOptionPane.showMessageDialog(paintBoard.this,"error during read from file",
"read Error",JOptionPane.ERROR_MESSAGE );
}
}
}
});
JMenuItem saveItem=new JMenuItem("保存(S)");//创建菜单项对象saveItem
saveItem.setFont(new Font("华文行楷", Font.PLAIN, 20));
saveItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result =fileChooser.showSaveDialog(paintBoard.this);
if(result==JFileChooser.CANCEL_OPTION)
return ;
File fileName=fileChooser.getSelectedFile();
fileName.canWrite();
if (fileName==null||fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
else{
try {
fileName.delete();
FileOutputStream fos=new FileOutputStream(fileName);
output=new ObjectOutputStream(fos);
drawings record;
output.writeInt( index );
for(int i=0;i< index ;i++)
{
drawings p= itemList[ i ] ;
output.writeObject(p);
output.flush(); //将所有图形信息强制转换成父类线性化存储到文件中
}
output.close();
fos.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
});
JMenuItem exitItem=new JMenuItem("退出(E)");//创建菜单项对象exitItem
exitItem.setFont(new Font("华文行楷", Font.PLAIN, 20));
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
newItem.setMnemonic('N');//设置快捷键
openItem.setMnemonic('O');
saveItem.setMnemonic('S');
exitItem.setMnemonic('E');
newItem.setForeground(new Color(110,10,10));
openItem.setForeground(new Color(110,10,10));
saveItem.setForeground(new Color(110,10,10));
exitItem.setForeground(new Color(110,10,10));
fileMenu.add(newItem);
fileMenu.addSeparator();
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
JMenuItem ziyouItem=new JMenuItem("自由线");//创建菜单项对象ziyouItem
ziyouItem.setFont(new Font("华文行楷", Font.PLAIN, 20));
ziyouItem.addActionListener
- 1
- 2
前往页