You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
7.5 KiB
Java

EventDemo
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class EventDemo extends Applet implements ActionListener, ItemListener, AdjustmentListener {
// An applet that displays a shape and some text. Color of
// shape and text is controlled by a vertical scroll bar.
// Color of background is controlled by a horizontal scroll bar.
// Text to be displayed can be entered in a TextField.
// Shape to be displayed can be selected from a Choice componnet.
// Bright or dim colors can be selected using a Chackbox.
// The display area is implemented as a ColorCanvas; the
// ColorCanvas class is defined later in this file.
ColorCanvas display; // display area
Choice shapeChoice; // for selecting which shape to display
Checkbox brightColors;// for selecting bright or dim colors
TextField text; // for entering the text to be displayed
Scrollbar hScroll; // horizontal scroll bar
Scrollbar vScroll; // vertical scroll bar
public void init() { // set up contents of applet
setBackground(Color.red); // background for applet
Panel displayPanel = new Panel(); // to hold display and scroll bars
displayPanel.setLayout(new BorderLayout());
display = new ColorCanvas("Hello World",ColorCanvas.RECT);
displayPanel.add(display, BorderLayout.CENTER);
hScroll = new Scrollbar(Scrollbar.HORIZONTAL,50,1,0,100);
displayPanel.add(hScroll, BorderLayout.SOUTH);
vScroll = new Scrollbar(Scrollbar.VERTICAL,0,1,0,100);
displayPanel.add(vScroll, BorderLayout.EAST);
Panel topPanel = new Panel(); // for controls
topPanel.setBackground(Color.white);
topPanel.setLayout(new GridLayout(1,3,5,5));
shapeChoice = new Choice();
shapeChoice.add("Rectangle");
shapeChoice.add("Oval");
shapeChoice.add("RoundRect");
topPanel.add(shapeChoice);
brightColors = new Checkbox("Bright Colors");
topPanel.add(brightColors);
text = new TextField("Hello World");
topPanel.add(text);
setLayout(new BorderLayout(1,1)); // applies to applet itself
add("Center", displayPanel);
add("North", topPanel);
setDisplayColors(); // defined below
text.addActionListener(this); // set this applet to listen for events
hScroll.addAdjustmentListener(this);
vScroll.addAdjustmentListener(this);
shapeChoice.addItemListener(this);
brightColors.addItemListener(this);
} // end of init()
public Insets getInsets() { // leave border around edge of applet
return new Insets(1,1,1,1);
}
void setDisplayColors() {
// Set foreground and background colors of display,
// depending on values of scroll bars and
// on state of the checkbox. (Colors are made
// using Color.getHSBColor(float,float,float),
// which creates a color given a hue, a satuation,
// and a brightness. The parameters must be between
// 0.0 and 1.0.)
float backgroundBrightness = hScroll.getValue() / 100.0F;
float foregroundHue = vScroll.getValue() / 100.0F;
float saturation = 1.0F;
float brightness;
if (brightColors.getState())
brightness = 1.0F;
else
brightness = 0.6F;
Color backgroundColor =
Color.getHSBColor(0.0F,0.0F,backgroundBrightness); // A grayscale color
Color foregroundColor =
Color.getHSBColor(foregroundHue,1.0F,brightness);
display.setBackground(backgroundColor);
display.setForeground(foregroundColor);
} // end of setDisplayColors()
public void itemStateChanged(ItemEvent evt) { // from the ItemListener interface
// An item event could have been generated by shapeChoice or brightColors.
Object source = evt.getSource(); // object that generated the event
if (source == shapeChoice) {
// user has selected a shape; set the shape
// that is shown in the display
switch (shapeChoice.getSelectedIndex()) {
case 0:
display.setShape(ColorCanvas.RECT);
break;
case 1:
display.setShape(ColorCanvas.OVAL);
break;
case 2:
display.setShape(ColorCanvas.ROUNDED);
break;
}
}
else if (source == brightColors) {
// user has changed the state of the checkbox;
// reset the colors for the display,
// and ask the system to redraw the display
setDisplayColors();
display.repaint();
}
} // end itemStateChanged()
public void actionPerformed(ActionEvent evt) { // from the ActionListener interface
// This can only be generated by the TextField, because the user has pressed
// return. Set the display text to the contents of the TextField, text.
display.setText(text.getText());
} // end of actionPerformed()
public void adjustmentValueChanged(AdjustmentEvent evt) {
// User has changed the value of one of the scroll bars.
// In thisprogram, it doesn't matter which one. Respond
// by setting the display colors.
setDisplayColors();
display.repaint();
} // end of adjustmentValueChanged()
} // end of class Event Demo
class ColorCanvas extends Canvas {
// Display a shape and some text.
// The canvas's setForeground() and setBackground()
// methods should be called to set the colors to
// be used for drawing.
private String text; // text to be displayed
private int shape; // code for shape to be displayed;
private Font font; // used for displaying the text
public final static int RECT = 0; // shape code for a rectangle
public final static int OVAL = 1; // shape code for an oval
public final static int ROUNDED = 2; // shape code for an round rect
public ColorCanvas(String text, int shape) {
this.text = text;
this.shape = shape;
font = new Font("Serif",Font.PLAIN,18);
}
public void setText(String text) {
this.text = text;
repaint();
}
public void setShape(int shape) {
this.shape = shape;
repaint();
}
public void paint(Graphics g) {
int width = getSize().width; // get size of canvas
int height = getSize().height;
int shape_left = width / 9; // compute position and size of shape
int shape_top = height / 3;
int shape_width = (7*width / 9);
int shape_height = (5*height / 9);
switch (shape) { // draw the shape
case RECT:
g.fillRect(shape_left,shape_top,shape_width,shape_height);
break;
case OVAL:
g.fillOval(shape_left,shape_top,shape_width,shape_height);
break;
case ROUNDED:
g.fillRoundRect(shape_left,shape_top,shape_width,shape_height,24,24);
break;
}
g.setFont(font);
g.drawString(text,width/9,2*height/9); // draw the text
}
} // end of class ColorCanvas