📄 flagmakergui.java
字号:
import java.awt.*;
import java.awt.event.*;
public class FlagMakerGUI extends Frame
implements ActionListener, ItemListener {
/** General Flag designer program J M Bishop May 2000
* =====================
*
* Presents a simple GUI for selecting one of
* three styles of flag, plus three motifs and
* seven colours for each bar or stripe.
*
* Illustrates inherited classes and GUI components
* particularly Choices, Checkboxes and Buttons.
*
**/
static final int none = 0, circle = 1, star = 2, starOfDavid = 3;
static final int stripes = 0, bars = 1, motif = 2;
static final Color listedColours [] = {Color.red, Color.blue, Color.white,
Color.black, Color.green, Color.orange, Color.yellow};
// GUI variables
// *************
CheckboxGroup flagParts;
Checkbox [] colourCheck = new Checkbox[4];
Choice colourChoice;
Choice flagChoice;
Choice motifChoice;
TextField nameField;
Button drawButton;
public FlagMakerGUI() {
// Laying out the GUI
// ******************
setLayout(new BorderLayout());
Panel intro = new Panel();
intro.add(new Label("Design a Flag"));
add("North",intro);
Panel middle = new Panel();
middle.setLayout(new FlowLayout());
flagParts = new CheckboxGroup();
for (int i=0; i<3; i++)
colourCheck[i] = new Checkbox("Colour "+(i+1),i==1,flagParts);
colourCheck[3] = new Checkbox("Motif colour",false,flagParts);
for (int i=0; i<4; i++) {
colourCheck[i].addItemListener(this);
middle.add(colourCheck[i]);
}
colourChoice = new Choice ();
colourChoice.addItem("Red");
colourChoice.addItem("Blue");
colourChoice.addItem("White");
colourChoice.addItem("Black");
colourChoice.addItem("Green");
colourChoice.addItem("Orange");
colourChoice.addItem("Yellow");
colourChoice.addItemListener(this);
middle.add(colourChoice);
flagChoice = new Choice ();
flagChoice.addItem("Striped");
flagChoice.addItem("Bars");
flagChoice.addItem("Striped with motif");
flagChoice.addItemListener(this);
middle.add(flagChoice);
motifChoice = new Choice();
motifChoice.addItem("none");
motifChoice.addItem("Circle");
motifChoice.addItem("Star");
motifChoice.addItem("Star Of David");
motifChoice.addItemListener(this);
middle.add(motifChoice);
nameField = new TextField(" A Flag ",20);
nameField.addActionListener(this);
middle.add(nameField);
drawButton = new Button("Draw it");
drawButton.addActionListener(this);
middle.add(drawButton);
add("Center",middle);
setTitle("Flag Designer");
setSize(250,200);
//pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// Variables for the event handlers
int flagPicked = 0;
int checkBoxPicked = 0; // colour 1
int motifPicked = circle;
Color [ ] colour = {Color.red, Color.white, Color.blue, Color.black};
String name = "A Flag";
public void actionPerformed (ActionEvent e) {
if (e.getSource() == drawButton) {
switch (flagPicked) {
case stripes : makeFlag (
new FlagCanvas (name, colour[0], colour[1], colour[2]));
break;
case bars : makeFlag (
new FlagCanvasVerti (name, colour[0], colour[1], colour[2]));
break;
case motif : makeFlag (
new FlagCanvasMotif (name, colour[0], colour[1], colour[2],
motifPicked, colour[3]));
}}
else if (e.getSource() == nameField)
name = nameField.getText();
}
public void itemStateChanged(ItemEvent e) {
Object picked = e.getSource();
// Distinguish between the four components
// Colour choice
if (picked == colourChoice) {
int selectedColour = ((Choice) picked).getSelectedIndex();
colour[checkBoxPicked] = listedColours[selectedColour];
}
// Flag choice
else if (picked == flagChoice)
flagPicked = ((Choice) picked).getSelectedIndex();
// Motif choice
else if (picked == motifChoice)
motifPicked = ((Choice) picked).getSelectedIndex();
else
// Check boxes (by default)
for (int i=0; i<4; i++) {
if (picked == colourCheck[i]) {
checkBoxPicked = i;
}
}
}
public static void main(String [] args) {
new FlagMakerGUI();
}
public static void makeFlag (FlagCanvas canvas) {
Frame f = new Frame();
f.add(canvas);
f.setTitle(canvas.country);
f.setSize(300,250);
f.setLocation(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
}
});
}
}
//===========================
class FlagCanvas extends Canvas {
String country;
Color bar1, bar2, bar3;
FlagCanvas (String c, Color b1, Color b2, Color b3) {
country = c;
bar1 = b1;
bar2 = b2;
bar3 = b3;
}
public void paint (Graphics g) {
// Draw the flag from coloured rectangles
g.setColor (bar1);
g.fillRect (40,40,200,40);
g.setColor (bar2);
g.fillRect (40,80,200,40);
g.setColor (bar3);
g.fillRect (40,120,200,40);
// Label the drawing
g.setColor (Color.black);
g.drawString(country,100,180);
}
}
class FlagCanvasVerti extends FlagCanvas {
FlagCanvasVerti (String c, Color b1, Color b2, Color b3) {
super (c,b1,b2,b3);
}
public void paint (Graphics g) {
// Draw the flag from coloured rectangles
g.setColor (bar1);
g.fillRect (40,40,60,120);
g.setColor (bar2);
g.fillRect (100,40,60,120);
g.setColor (bar3);
g.fillRect (160,40,60,120);
// Label the drawing
g.setColor (Color.black);
g.drawString(country,100,180);
}
}
class FlagCanvasMotif extends FlagCanvas {
int motif;
Color motifColor;
FlagCanvasMotif (String c, Color b1, Color b2, Color b3, int m, Color mc) {
super (c,b1,b2,b3);
motif = m;
motifColor = mc;
}
public void paint (Graphics g) {
int [] xpoints = {120,150,122,135,142,120};
int [] ypoints = {90,90,115,80,115,90};
// Draw the flag from coloured rectangles
g.setColor (bar1);
g.fillRect (40,40,200,40);
g.setColor (bar2);
g.fillRect (40,80,200,40);
g.setColor (bar3);
g.fillRect (40,120,200,40);
// Label the drawing
g.setColor (Color.black);
g.drawString(country,100,180);
if (motif != 0) {
g.setColor (motifColor);
switch (motif) {
case 1: g.fillOval(125,90,20,20); break;
case 2: g.fillPolygon(xpoints,ypoints,6); break;
case 3: g.drawPolygon(xpoints,ypoints,6); break;
default: break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -