?? graphicsdemopane.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
public class GraphicsDemoPane extends JPanel
implements PropertyChangeListener {
public GraphicsDemoPane() {
super(true); // Double buffer
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.0;
c.weighty = 0.0;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.CENTER;
// Create and add the button panel
JPanel colorPanel = new JPanel();
colorPanel.setLayout(new GridLayout(0, 3, 2, 2));
colorButtons = new JButton[buttonColors.length];
for (int i = 0; i < buttonColors.length; i++) {
JButton button = new JButton(new ColorFillIcon(buttonColors[i]));
colorButtons[i] = button;
colorPanel.add(button);
button.putClientProperty(DRAW_COLOR, buttonColors[i]);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JButton b = (JButton)evt.getSource();
Color color = (Color)b.getClientProperty(DRAW_COLOR);
Icon g = b.getIcon();
colorLabel.setIcon(g);
graphicOps.setColor(color);
}
});
}
this.add(colorPanel, c);
// Create and add the shape selection panel
JPanel shapePanel = new JPanel();
shapePanel.setLayout(new GridLayout(0, 3, 2, 2));
shapeButtons = new JButton[shapeList.length];
for (int i = 0; i < shapeList.length; i++) {
JButton button = new JButton(new GraphicShapeIcon(shapeList[i]));
shapeButtons[i] = button;
shapePanel.add(button);
button.putClientProperty(DRAW_SHAPE, new Integer(shapeList[i]));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JButton b = (JButton)evt.getSource();
int shape = ((Integer)b.getClientProperty(DRAW_SHAPE)).intValue();
shapeLabel.setIcon(b.getIcon());
graphicOps.setShape(shape);
}
});
}
c.gridy++;
c.insets = new Insets(16, 0, 0, 0);
this.add(shapePanel, c);
// Add a dummy component that takes
// all the spare space in the
// right-hand panel
c.gridy++;
c.weighty = 1.0;
c.insets = new Insets(0, 0, 0, 0);
this.add(new JComponent(){}, c);
// Add the "Clear" and "Undo" buttons
c.gridy++;
c.weighty = 0.0;
JPanel buttonPanel = new JPanel();
clearButton = new JButton("Clear");
undoButton = new JButton("Undo");
printButton = new JButton("Print");
Font font = new Font("Serif", Font.BOLD, 12);
clearButton.setFont(font);
undoButton.setFont(font);
printButton.setFont(font);
buttonPanel.add(clearButton);
buttonPanel.add(undoButton);
buttonPanel.add(printButton);
this.add(buttonPanel, c);
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
graphicOps.clearCanvas();
}
});
undoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
graphicOps.removeLast();
}
});
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
graphicOps.printCanvas();
}
});
// Place labels with the currently selected
// color and shapes underneath the color panel
c.gridy++;
c.weighty = 0.0;
c.weightx = 1.0;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(8, 0, 0, 0);
JPanel lowerPanel = new JPanel();
this.add(lowerPanel, c);
lowerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 4, 4));
colorLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
colorLabel.setHorizontalTextPosition(SwingConstants.CENTER);
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
colorLabel.setVerticalAlignment(SwingConstants.BOTTOM);
lowerPanel.add(colorLabel);
shapeLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
shapeLabel.setHorizontalTextPosition(SwingConstants.CENTER);
shapeLabel.setHorizontalAlignment(SwingConstants.CENTER);
shapeLabel.setVerticalAlignment(SwingConstants.BOTTOM);
lowerPanel.add(shapeLabel);
// Disable clear/undo/print buttons
clearButton.setEnabled(false);
undoButton.setEnabled(false);
printButton.setEnabled(false);
}
// Associate drawing area
public void setDrawingArea(GraphicOps graphicOps) {
this.graphicOps = graphicOps;
}
// Select initial color and shape
public void selectTools() {
colorButtons[0].doClick(); // Draw with black
shapeButtons[0].doClick(); // Draw a line
}
// PropertyChangeListener Interface
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(GraphicOps.SHAPE_PROPERTY)) {
int count = ((Integer)evt.getNewValue()).intValue();
boolean state = (count > 0);
clearButton.setEnabled(state);
undoButton.setEnabled(state);
printButton.setEnabled(state);
}
}
// Labels showing the current color and current shape
protected JLabel colorLabel = new JLabel("Color");
protected JLabel shapeLabel = new JLabel("Shape");
// Clear, undo and print buttons
protected JButton clearButton;
protected JButton undoButton;
protected JButton printButton;
// Coloring buttons
private static Color[] buttonColors = {
// Color.black, Color.blue,
// Color.cyan, Color.darkGray,
// Color.gray, Color.green,
// Color.lightGray, Color.magenta,
// Color.orange, Color.pink,
// Color.red, Color.yellow,
// Color.white };
new Color(0),Color.lightGray,
new Color(204,0,0),new Color(102,204,204),
new Color(102,153,102),Color.blue,
new Color(153,0,204),new Color(16762880),
new Color(16756655),new Color(153,0,0),
new Color(255,153,204),new Color(51,153,204),
Color.WHITE,new Color(204,204,204),
Color.RED,Color.cyan,
new Color(65280),new Color(102,153,255),
new Color(16711935),new Color(16776960),
new Color(204,153,153),new Color(255,0,204),
new Color(255,153,255),new Color(153,0,255),
};
private JButton[] colorButtons;
// Shape buttons
private static int[] shapeList = {
GraphicOps.DRAWN_LINE,
GraphicOps.DRAWN_RECT,
GraphicOps.DRAWN_ROUND_RECT,
GraphicOps.FILLED_RECT,
GraphicOps.FILLED_ROUND_RECT,
GraphicOps.DRAWN_OVAL,
GraphicOps.FILLED_OVAL };
private JButton[] shapeButtons;
// Connection to drawing area
private GraphicOps graphicOps;
// Property names
private static final String DRAW_COLOR = "Draw_color";
private static final String DRAW_SHAPE = "Draw_shape";
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -