📄 paintapp.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* @author aassd
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class PaintApp extends Frame {
Panel commandPanel = new Panel();
Panel colorPanel = new Panel();
Panel shapePanel = new Panel();
Button btnRed = new Button("Red");
Button btnGreen = new Button("Green");
Button btnBlue = new Button("Blue");
Button btnLine = new Button("Line");
Button btnRectangle = new Button("Rectangle");
Button btnCircle = new Button("Circle");
Button btnUndo = new Button("Undo");
Button btnRedo = new Button("Redo");
Button btnExit = new Button("Exit");
PaintBoard paintBoard = new PaintBoard();
public PaintApp() {
this.setLayout(new BorderLayout());
btnLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnLine_actionPerformed(e);
}
});
btnRectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnRectangle_actionPerformed(e);
}
});
btnCircle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnCircle_actionPerformed(e);
}
});
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnExit_actionPerformed(e);
}
});
btnUndo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnUndo_actionPerformed(e);
}
});
btnRedo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnRedo_actionPerformed(e);
}
});
shapePanel.setLayout(new FlowLayout());
shapePanel.add(btnLine, null);
shapePanel.add(btnRectangle, null);
shapePanel.add(btnCircle, null);
shapePanel.add(btnUndo, null);
shapePanel.add(btnRedo, null);
shapePanel.add(btnExit,null);
btnRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnRed_actionPerformed(e);
}
});
btnGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnGreen_actionPerformed(e);
}
});
btnBlue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnBlue_actionPerformed(e);
}
});
colorPanel.setLayout(new FlowLayout());
colorPanel.add(btnRed, null);
colorPanel.add(btnGreen, null);
colorPanel.add(btnBlue, null);
commandPanel.setLayout(new BorderLayout());
commandPanel.add(shapePanel, BorderLayout.NORTH);
commandPanel.add(colorPanel, BorderLayout.CENTER);
this.add(commandPanel, BorderLayout.SOUTH);
this.add(paintBoard, BorderLayout.CENTER);
///////////////////////////////////////////////////////////////////////////
btnLine.setForeground(Color.red);
paintBoard.setCommand(Command.LINE);
btnRed.setForeground(Color.red);
paintBoard.setColor(Color.red);
///////////////////////////////////////////////////////////////////////////
}
void btnExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
void btnUndo_actionPerformed(ActionEvent e) {
paintBoard.undo();
}
void btnRedo_actionPerformed(ActionEvent e) {
paintBoard.redo();
}
void btnLine_actionPerformed(ActionEvent e) {
btnLine.setForeground(Color.red);
btnRectangle.setForeground(Color.black);
btnCircle.setForeground(Color.black);
paintBoard.setCommand(Command.LINE);
}
void btnRectangle_actionPerformed(ActionEvent e) {
btnLine.setForeground(Color.black);
btnRectangle.setForeground(Color.red);
btnCircle.setForeground(Color.black);
paintBoard.setCommand(Command.RECTANGLE);
}
void btnCircle_actionPerformed(ActionEvent e) {
btnLine.setForeground(Color.black);
btnRectangle.setForeground(Color.black);
btnCircle.setForeground(Color.red);
paintBoard.setCommand(Command.CIRCLE);
}
void btnRed_actionPerformed(ActionEvent e) {
btnRed.setForeground(Color.red);
btnGreen.setForeground(Color.black);
btnBlue.setForeground(Color.black);
paintBoard.setColor(Color.red);
}
void btnGreen_actionPerformed(ActionEvent e) {
btnRed.setForeground(Color.black);
btnGreen.setForeground(Color.red);
btnBlue.setForeground(Color.black);
paintBoard.setColor(Color.green);
}
void btnBlue_actionPerformed(ActionEvent e) {
btnRed.setForeground(Color.black);
btnGreen.setForeground(Color.black);
btnBlue.setForeground(Color.red);
paintBoard.setColor(Color.blue);
}
public static void main(String[] args) {
PaintApp paintApp = new PaintApp();
paintApp.setSize(600, 500);
paintApp.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -