📄 paintbrush.java
字号:
metric.addItemListener(new ItemListener() {// 字体选择
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED)
stroke2 = Float.parseFloat(fontMetrics[metric
.getSelectedIndex()]);
}
});
stroke2 = Float.parseFloat(fontMetrics[0]);
countrys = new JComboBox(nation);
countrys.setMaximumRowCount(2);
bold = new JCheckBox("粗体");
bold.addItemListener(new ItemListener() {// 选择粗体
public void itemStateChanged(ItemEvent event) {
if (bold.isSelected())
valBold = Font.BOLD;
else
valBold = Font.PLAIN;
}
});
italic = new JCheckBox("斜体");
italic.addItemListener(new ItemListener() {// 选择斜体
public void itemStateChanged(ItemEvent event) {
if (italic.isSelected())
valItalic = Font.ITALIC;
else
valItalic = Font.PLAIN;
}
});
plain = new JLabel(new ImageIcon("Icons/1.gif"));
fontBar.add(fontl);
fontBar.addSeparator();
fontBar.add(metric);
fontBar.addSeparator();
fontBar.add(countrys);
fontBar.addSeparator();
fontBar.add(bold);
fontBar.add(italic);
fontBar.addSeparator();
fontBar.add(plain);
// 添加弹出菜单及其事件监听
popupMenu = new JPopupMenu();
pUndoItem = new JMenuItem("撤销(U)", 'U');
pUndoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,
InputEvent.CTRL_MASK));
pSaveItem = new JMenuItem("储存(S)", 'S');
pSaveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
pToolItem = new JMenuItem("工具箱(T)", 'T');
pToolItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
InputEvent.CTRL_MASK));
;
pColorBoxItem = new JMenuItem("颜料盒(C)", 'C');
pColorBoxItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
pTextEditItem = new JMenuItem("文字工具栏(E)", 'E');
pTextEditItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
InputEvent.CTRL_MASK));
pAddItem = new JMenuItem("子窗口(A)", 'A');
pAddItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
InputEvent.CTRL_MASK));
pStateItem = new JMenuItem("状态栏(S)", 'S');
pStateItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
popupMenu.add(pUndoItem);
pUndoItem.addActionListener(this);
pUndoItem.setEnabled(false);
popupMenu.add(pSaveItem);
pSaveItem.addActionListener(this);
popupMenu.addSeparator();
popupMenu.add(pToolItem);
pToolItem.addActionListener(this);
popupMenu.add(pColorBoxItem);
pColorBoxItem.addActionListener(this);
popupMenu.add(pTextEditItem);
pTextEditItem.addActionListener(this);
pTextEditItem.setEnabled(false);
popupMenu.add(pAddItem);
pAddItem.addActionListener(this);
popupMenu.addSeparator();
popupMenu.add(pStateItem);
pStateItem.addActionListener(this);
addMouseListener(new MouseAdapter() {// 鼠标事件:弹出菜单
public void mousePressed(MouseEvent event) {
checkForTriggerEvent(event);
}
public void mouseReleased(MouseEvent event) {
checkForTriggerEvent(event);
}
private void checkForTriggerEvent(MouseEvent event) {// 检查是否弹出菜单
if (event.isPopupTrigger())
popupMenu.show(event.getComponent(), event.getX(), event
.getY());
checkMenuItemEnabled();
}
});
// 添加状态栏
statusBar = new JPanel();
statusBar.setLayout(new GridLayout(1, 3));
tip = new JLabel();
coordinate = new JLabel();
information = new JLabel();
tip.setText("要获得帮助,请在“帮助“菜单中,单击“帮助主题“.");
statusBar.add(tip);
statusBar.add(coordinate);
statusBar.add(information);
// container.add(drawArea,BorderLayout.CENTER );
container.add(fontBar, BorderLayout.NORTH);
fontBar.setVisible(false);
container.add(colorBar, BorderLayout.EAST);
// colorBar.setVisible(false);
container.add(toolBar, BorderLayout.WEST);
// toolBar.setVisible(false);
container.add(statusBar, BorderLayout.SOUTH);
// statusBar.setVisible(false);
createNewItem();
checkMenuItemEnabled();
validate();
setLocation(250, 200);// 设置窗口位置
setSize(WIDTH, HEIGHT);// 设置窗口大小
setVisible(true);
}// end constructor
/**
* author 王宇新,author 王德恩<br>
* version 1.0 07-6-4<br>
* author 周新飞<br>
* version 2.0 07-6-12<br>
* 完成各种事件处理
*/
public void actionPerformed(ActionEvent event) {
// 完成’文件‘菜单的事件
if (event.getSource() == newItem) {// 新建一个文件
int saveChoose = JOptionPane.NO_OPTION;
if (index != 0) {
saveChoose = JOptionPane.showConfirmDialog(this,
"您的文件尚未保存。是否保存?", "提示",
JOptionPane.YES_NO_CANCEL_OPTION);
}
if (saveChoose == JOptionPane.YES_OPTION) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
fileName.canWrite();
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser, "不合法的文件名",
"不合法的文件名", JOptionPane.ERROR_MESSAGE);
else {
try {
img = ImageIO.read(fileName);
img_g2d = img.createGraphics();
index = 0;
createNewItem();
repaint();
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (saveChoose == JOptionPane.NO_OPTION) {
index = 0;
currentChoice = 5;
color = Color.black;
stroke1 = 1.0f;
createNewItem();
repaint();// 将有关值设置为初始状态,并且重画
} else if (saveChoose == JOptionPane.CANCEL_OPTION)
return;
}
else if (event.getSource() == saveItem
|| event.getSource() == pSaveItem) {// 保存文件
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileFilter(new JPGFilter());
fileChooser.setFileFilter(new GIFFilter());
fileChooser.setFileFilter(new PNGFilter());
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
fileName.canWrite();
// filter.addSuffix( fileName);
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser, "不合法的文件名",
"不合法的文件名", JOptionPane.ERROR_MESSAGE);
else {
try {
fileName.delete();
FileOutputStream fos = new FileOutputStream(fileName);
int j = 0;
while (j <= index) {
img_g2d.drawImage(img, 0, 0, frame);
draw(img_g2d, itemList[j]);
j++;
}
repaint();
ImageIO.write(img, "JPEG", fileName);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
else if (event.getSource() == openItem) {// 打开文件
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
fileName.canRead();
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser, "文件错误", "文件错误",
JOptionPane.ERROR_MESSAGE);
else {
try {
img = ImageIO.read(fileName);
img_g2d = img.createGraphics();
index = 0;
createNewItem();
repaint();
} catch (IOException e) {
e.printStackTrace();
}
}
}
else if (event.getSource() == exitItem) {// 退出程序
int exitChoose = JOptionPane.showConfirmDialog(this, "确定要退出么?",
"退出提示", JOptionPane.OK_CANCEL_OPTION);
if (exitChoose == JOptionPane.OK_OPTION)
System.exit(0);
else
return;
}
// 完成‘编辑’菜单的事件
else if (event.getSource() == addItem || event.getSource() == pAddItem) {// 添加子窗口
inframeNum++;
frame = new JInternalFrame("子窗口" + inframeNum, true, true, true,
true);
frame.setBackground(SystemColor.textHighlightText);
frame.setLocation(80 * (inframeNum - 1), 80 * (inframeNum - 1));
Container container = frame.getContentPane();
container.add(drawArea, BorderLayout.CENTER);
// frame.addMouseListener( new MouseA());
// frame.addMouseMotionListener(new MouseB());
frame.pack();
theDesktop.add(frame);
frame.setVisible(true);
} else if (event.getSource() == undoItem
|| event.getSource() == pUndoItem) {// 撤销功能
checkMenuItemEnabled();
drawArea.setEnabled(false);
redoItem.setEnabled(true);
index = undoStep[undoIndex - 2];
System.out.print(undoIndex);
undoIndex--;
createNewItem();
repaint();
} else if (event.getSource() == redoItem) {// 重复功能
checkMenuItemEnabled();
drawArea.setEnabled(false);
index = undoStep[undoIndex++];
createNewItem();
repaint();
}
// 实现‘颜色’菜单的事件
else if (event.getSource() == editcolorItem
|| event.getSource() == otherButton) {// 编辑颜色
color = JColorChooser.showDialog(PaintBrush.this, "自定义", color);
if (color == null)
color = Color.LIGHT_GRAY;
R = color.getRed();
G = color.getGreen();
B = color.getBlue();
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
}
// 实现‘察看‘菜单的事件
else if (event.getSource() == toolItem
|| event.getSource() == pToolItem) {// 工具栏
if (toolBar.isVisible())
toolBar.setVisible(false);
else
toolBar.setVisible(true);
} else if (event.getSource() == colorBoxItem
|| event.getSource() == pColorBoxItem) {// 颜色工具栏
if (colorBar.isVisible())
colorBar.setVisible(false);
else
colorBar.setVisible(true);
} else if (event.getSource() == textEditItem
|| event.getSource() == pTextEditItem) {// 文字工具栏
checkMenuItemEnabled();
if (fontBar.isVisible())
fontBar.setVisible(false);
else
fontBar.setVisible(true);
}
else if (event.getSource() == stateItem
|| event.getSource() == pStateItem) {// 状态栏
if (statusBar.isVisible())
statusBar.setVisible(false);
else
statusBar.setVisible(true);
}
// 实现‘帮助’菜单的事件
else if (event.getSource() == helpItem) {// 帮助窗口
String output = "\n 欢迎访问个人版画图工具!\n\n 如有疑问,请参考Microsoft 画图工具.";
JOptionPane.showMessageDialog(this, output, "画图",
JOptionPane.QUESTION_MESSAGE);
}
else if (event.getSource() == aboutItem) {// 关于窗口
String output = " 厦门大学软件学院(R)\n\n 个人版2007\n\n 版权所有 (C) 2007- 周新飞"
+ "\n\n 欢迎大家使用!";
JOptionPane.showMessageDialog(this, output, "关于画图",
JOptionPane.INFORMATION_MESSAGE);
}
// 处理工具栏的按钮事件
else if (event.getSource() == penButton) {
currentChoice = 1;
createNewItem();// 调用函数画图
} else if (event.getSource() == brushButton) {
currentChoice = 2;
createNewItem();
} else if (event.getSource() == eraserButton) {
currentChoice = 5;
createNewItem();
} else if (event.getSource() == sprayButton) {
currentChoice = 6;
createNewItem();
} else if (event.getSource() == characterButton) {
currentChoice = 7;
fontBar.setVisible(true);
textEditItem.setEnabled(true);
pTextEditItem.setEnabled(true);
createNewItem();
} else if (event.getSource() == lineButton) {
currentChoice = 8;
createNewItem();
} else if (event.getSource() == rectangleButton) {
currentChoice = 9;
normalRadioButton.setSelected(true);
createNewItem();
} else if (event.getSource() == polygonButton) {
currentChoice = 12;
normalRadioButton.setSelected(true);
createNewItem();
} else if (event.getSource() == ellipseButton) {
currentChoice = 15;
normalRadioButton.setSelected(true);
createNewItem();
} else if (event.getSource() == roundrectangleButton) {
currentChoice = 18;
normalRadioButton.setSelected(true);
createNewItem();
}
// 处理颜色工具栏的事件
else if (event.getSource() == blackButton) {
color = Color.BLACK;
R = color.getRed();
G = color.getGreen();
B = color.getBlue();
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
} else if (event.getSource() == whiteButton) {
color = Color.WHITE;
R = color.getRed();
G = color.getGreen();
B = color.getBlue();
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
} else if (event.getSource() == redButton) {
color = Color.RED;
R = color.getRed();
G = color.getGreen();
B = color.getBlue();
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
} else if (event.getSource() == greenButton) {
color = Color.GREEN;
R = color.getRed();
G = color.getGreen();
B = color.getBlue();
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
} else if (event.getSource() == blueButton) {
color = Color.BLUE;
R = color.getRed();
G = color.getGreen();
B = color.getBlue();
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
}
}// end actionPerformed
/**
* @author 周新飞
* @version 1.0 07-6-14
* @version 2.0 07-6-18
* @see MouseA 功能:实现鼠标事件功能
*/
public class MouseA extends MouseAdapter {
public void mousePressed(MouseEvent event) {
checkForTriggerEvent(event);
itemList[index].x1 = itemList[index].x2 = event.getX();
itemList[index].y1 = itemList[index].y2 = event.getY();
start_x = event.getX();
start_y = event.getY();
coordinate.setText(event.getX() + "," + event.getY());
if (currentChoice == 1 || currentChoice == 2 || currentChoice == 3
|| currentChoice == 4 || currentChoice == 5) {
itemList[index].x1 = itemList[index].x2 = event.getX();
itemList[index].y1 = itemList[index].y2 = event.getY();
index++;
createNewItem();
}
else if (currentChoice == 7) {
itemList[index].x1 = event.getX();
itemList[index].y1 = event.getY();
String input;
input = JOptionPane.showInputDialog("input:");
itemList[index].string = input;
itemList[index].x2 = valBold;
itemList[index].y2 = valItalic;
itemList[index].fonts = font;
itemList[index].stroke = stroke2;
index++;
currentChoice = 7;
createNewItem();
repaint();
}
else if (currentChoice == 6) {
Stack stack = new Stack();
px = itemList[index].x1 = itemList[index].x2 = event.getX();
py = itemList[index].y1 = itemList[index].y2 = event.getY();
Color color = drawArea.getPixleColor(px, py);
System.out.print(color + "\n");
Point seed1 = new Point(px, py);
index++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -