package bag;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MiniDrawPad implements ItemListener{
static int choice=0;
JFrame f;
JMenuBar mbar;
JMenu fm;
JMenu cm;
JMenu sm;
JMenu hm;
JMenuItem ni;
JMenuItem oi;
JMenuItem si;
JMenuItem ei;
JMenuItem ai;
JMenuItem ci;
JMenuItem strokei;
JToolBar tbar;
JButton[] bt;
String[] str1;
String[] str2;
JComboBox combo;
JCheckBox chk1,chk2;
JPanel panel;
JLabel lb;
JColorChooser colorchooser;
DrawPanel drawPanel;
public MiniDrawPad(){
f=new JFrame("画图板 作者:毛晓蓉 学号:0706550102");
f.setSize(600,800);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel=new DrawPanel();
f.add(drawPanel,BorderLayout.CENTER);
lb=new JLabel();
f.add(lb,BorderLayout.SOUTH);
showMenu();
showTool();
f.setSize(800, 1000);
f.setVisible(true);
f.setLocationRelativeTo(null);
}
public void showMenu(){
mbar=new JMenuBar();
fm=new JMenu("File");
cm=new JMenu("Color");
sm=new JMenu("Stroke");
hm=new JMenu("Help");
ni=new JMenuItem("New(N)");
ni.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
oi=new JMenuItem("Open(O)");
oi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
si=new JMenuItem("Save(S)");
si.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
ei=new JMenuItem("Exit(E)");
ei.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
ai=new JMenuItem("About this Drawing Pad!");
ci=new JMenuItem("Choose Color");
strokei=new JMenuItem("Set Stroke");
mbar.add(fm);
mbar.add(cm);
mbar.add(sm);
mbar.add(hm);
fm.add(ni);
fm.add(oi);
fm.add(si);
fm.add(ei);
cm.add(ci);
sm.add(strokei);
hm.add(ai);
f.setJMenuBar(mbar);
ni.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPanel.newFile();}
});
oi.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPanel.openFile();}
});
si.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPanel.saveFile();}
});
ei.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);}
});
ci.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Color color;
colorchooser=new JColorChooser();
color=JColorChooser.showDialog(f, "Color Chooser", drawPanel.getLineColor());
if(color!=null)drawPanel.setLineColor(color);}
});
strokei.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String input;
input=JOptionPane.showInputDialog(f,"线条粗细为:","设置线条粗细对话框",JOptionPane.QUESTION_MESSAGE);
if(input!=null)
{
float strokes=Integer.parseInt(input);
drawPanel.setLineStroke(strokes);
}
}
});
ai.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(f,"this is a mini drawing pad! You can draw at it!","MessageDialog",JOptionPane.INFORMATION_MESSAGE);
}
});
}
public void showTool(){
str1=new String[]{"Draw at will","Draw a straight line","Draw a rectangle","Fill a rectangle","Draw a oval","Fill a oval","Draw a circle",
"Fill a circle","Draw a round rectangle","Fill a round rectangle","Erase at will"};
bt=new JButton[11];
JButton b1=new JButton(new ImageIcon("New.gif"));
b1.setToolTipText("Draw a new picture");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPanel.newFile();}
});
JButton b2=new JButton(new ImageIcon("Open.gif"));
b2.setToolTipText("Open a saved picture");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPanel.openFile();}
});
JButton b3=new JButton(new ImageIcon("Save.gif"));
b3.setToolTipText("Save current drawing");
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPanel.saveFile();}
});
tbar=new JToolBar();
tbar.add(b1);
tbar.add(b2);
tbar.add(b3);
Icon icon1=new ImageIcon("Pencil.gif");
bt[0]=new JButton(icon1);
bt[1]=new JButton(new ImageIcon("Line.GIF"));
bt[2]=new JButton(new ImageIcon("Rect.gif"));
bt[3]=new JButton(new ImageIcon("fRect.gif"));
bt[4]=new JButton(new ImageIcon("Oval.gif"));
bt[5]=new JButton(new ImageIcon("fOval.gif"));
bt[6]=new JButton(new ImageIcon("Circle.gif"));
bt[7]=new JButton(new ImageIcon("fCircle.gif"));
bt[8]=new JButton(new ImageIcon("RoundRect.gif"));
bt[9]=new JButton(new ImageIcon("frRect.gif"));
bt[10]=new JButton(new ImageIcon("Rubber.gif"));
for(int i=0;i<11;i++)
{
bt[i].setToolTipText(str1[i]);
tbar.add(bt[i]);
bt[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
for(int j=0;j<11;j++){
if(e.getSource()==bt[j])choice=j+1;
}
}
});
}
JButton b4=new JButton(new ImageIcon("Color.gif"));
b4.setToolTipText("Choose current drawing color");
b4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Color color;
colorchooser=new JColorChooser();
color=JColorChooser.showDialog(f, "Color Chooser", drawPanel.getLineColor());
if(color!=null)drawPanel.setLineColor(color);}
});
tbar.add(b4);
JButton b5=new JButton(new ImageIcon("Stroke.gif"));
b5.setToolTipText("Set current drawing stroke");
b5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String input;
input=JOptionPane.showInputDialog(f,"线条粗细为:","设置线条粗细对话框",JOptionPane.QUESTION_MESSAGE);
if(input!=null)
{
float strokes=Integer.parseInt(input);
drawPanel.setLineStroke(strokes);
}
}
});
tbar.add(b5);
JButton b6=new JButton(new ImageIcon("Word.gif"));
b6.setToolTipText("Write down what u want");
b6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String input;
input=JOptionPane.showInputDialog(f,"输入文本内容为:","输入文本对话框",JOptionPane.QUESTION_MESSAGE);
if(input!=null)
{
drawPanel.setText(input);
choice=12;
}
}
});
tbar.add(b6);
chk1=new JCheckBox("BOLD");
chk2=new JCheckBox("ITALIC");
chk1.addItemListener(this);
chk2.addItemListener(this);
tbar.add(chk1);
tbar.add(chk2);
str2=new String[]{"宋体","隶书","华文彩云","仿宋_GB2312","华文行楷","方正舒体","Time New Roman",
"Serif","Monospaced","SonsSerif","Garamond"};
combo=new JComboBox(str2);
combo.setBorder(BorderFactory.createTitledBorder("字体"));
combo.setMaximumRowCount(8);
combo.setMaximumSize(new Dimension(120,20));
combo.setMinimumSize(new Dimension(50,20));
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
String name;
name=str2[combo.getSelectedIndex()];
drawPanel.setfontName(name);
}
});
tbar.add(combo);
f.add(tbar,BorderLayout.NORTH);
}
public void itemStateChanged(ItemEvent e){
int style=0;
if(chk1.isSelected()){style=Font.BOLD;
if(chk2.isSelected()){style=style+Font.ITALIC;}
}else{
if(chk2.isSelected()){style=Font.ITALIC;}
}
drawPanel.setStyle(style);
}
public static void main(String args[]){
new MiniDrawPad();
}
class drawings implements Serializable{
private static final long serialVersionUID = 1L;
public int x1,x2,y1,y2;
public int R,G,B;
public float stroke;
public int style;
public int type;
public String s1;
public String s2;
public void draw(Graphics2D g2d){}
MiniDraw.rar_minidraw_绘图
版权申诉
17 浏览量
2022-09-20
15:53:27
上传
评论
收藏 35KB RAR 举报
weixin_42653672
- 粉丝: 93
- 资源: 1万+