drawapplet.java
来自「Java the UML Way 书中所有源码」· Java 代码 · 共 159 行
JAVA
159 行
/*
* DrawApplet.java E.L. 2001-08-18
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawApplet extends JApplet {
/* These ellipse attributes will be used the first time the ellipse is drawn */
private static final String x = "70";
private static final String y = "50";
private static final String width = "50";
private static final String height = "100";
/*
* The following variables will be used in the init() method,
* in the listener class, and in the UserInputPanel class.
*/
private Container guiContainer;
private Drawing theDrawing;
private JTextField xField = new JTextField(x);
private JTextField yField = new JTextField(y);
private JTextField widthField = new JTextField(width);
private JTextField heightField = new JTextField(height);
public void init() {
guiContainer = getContentPane();
UserInputPanel north = new UserInputPanel();
guiContainer.add(north, BorderLayout.NORTH);
theDrawing = new Drawing(x, y, width, height);
guiContainer.add(theDrawing, BorderLayout.CENTER);
ButtonPanel south = new ButtonPanel();
guiContainer.add(south, BorderLayout.SOUTH);
}
/* Listens for button pushes */
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
/* Gets new ellipse attributes and sends them to the drawing object */
theDrawing.setX(xField.getText());
theDrawing.setY(yField.getText());
theDrawing.setWidth(widthField.getText());
theDrawing.setHeight(heightField.getText());
/* Gets the color of the button */
String colorName = event.getActionCommand();
Color color;
if (colorName.equals("Red")) color = Color.red;
else if (colorName.equals("Blue")) color = Color.blue;
else color = Color.cyan;
/* Sets the foreground color for the drawing */
theDrawing.setForeground(color);
/* Repaints the whole window */
guiContainer.repaint();
}
}
/* This class describes the northern panel. */
private class UserInputPanel extends JPanel {
public UserInputPanel() {
/* The Gridlayout constructor takes the following arguments, in order:
No. of rows, no. of columns, horizontal and vertical gap between the cells. */
setLayout(new GridLayout(4, 2, 5, 5));
JLabel label = new JLabel("X-value:", JLabel.RIGHT);
add(label);
add(xField);
label = new JLabel("Y-value:", JLabel.RIGHT);
add(label);
add(yField);
label = new JLabel("Width:", JLabel.RIGHT);
add(label);
add(widthField);
label = new JLabel("Height:", JLabel.RIGHT);
add(label);
add(heightField);
}
}
/* This class describes the southern panel. */
private class ButtonPanel extends JPanel {
public ButtonPanel() {
ButtonListener theButtonListener = new ButtonListener();
JButton redButton = new JButton("Red");
redButton.setBackground(Color.red);
redButton.addActionListener(theButtonListener);
add(redButton);
JButton blueButton = new JButton("Blue");
blueButton.setBackground(Color.blue);
blueButton.addActionListener(theButtonListener);
add(blueButton);
JButton cyanButton = new JButton("Cyan");
cyanButton.setBackground(Color.cyan);
cyanButton.addActionListener(theButtonListener);
add(cyanButton);
}
}
}
/*
* This class describes the drawing of an ellipse where the ellipse attributes
* may be changed by the user. The class receives the attributes as texts.
* If it is not possible to convert the text into a number, the corresponding
* attribute gets the value 0. A litte message is sent to the console.
*/
class Drawing extends JPanel {
private int x;
private int y;
private int width;
private int height;
public Drawing(String initX, String initY, String initWidth, String initHeight) {
x = convertText(initX);
y = convertText(initY);
width = convertText(initWidth);
height = convertText(initHeight);
}
public void setX(String newX) {
x = convertText(newX);
}
public void setY(String newY) {
y = convertText(newY);
}
public void setWidth(String newWidth) {
width = convertText(newWidth);
}
public void setHeight(String newHeight) {
height = convertText(newHeight);
}
public void paintComponent(Graphics window) {
window.fillOval(x, y, width, height);
}
private int convertText(String text) {
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
System.out.println("It's not possible to convert " + text + " into integer.");
return 0;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?