import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class exam_92 extends JFrame {
private int topX, topY, width, fontSize,
height, bottomX, bottomY, shape;
private boolean clear, textOn, filled;
private Color drawingColor;
private String font;
private JTextField text;
private JPanel panel1, panel2, panel3, panel4;
private JRadioButton ovalBox, rectBox, lineBox;
private ButtonGroup shapeGroup;
private JCheckBox fillBox;
private JComboBox colorList, fontList, sizeList;
private JButton clearButton;
private String colorNames[] = { "Black", "Green", "Blue",
"Red", "Cyan" };
private Color colors[] = { Color.black, Color.green, Color.blue,
Color.red, Color.cyan };
private String fontNames[] = { "Serif", "SansSerif", "Monospaced" };
private String sizeNames[] = { "9", "10", "22", "72" };
private int sizes[] = { 9, 10, 22, 72 };
private final int OVAL = 1, LINE = 2, RECT = 3;
private ToolWindow tools;
public exam_92()
{
super( "exam_92" );
addMouseListener( new MouseHandler() );
drawingColor = Color.black;
shape = OVAL;
font = "Serif";
fontSize = 9;
setDefaultCloseOperation( EXIT_ON_CLOSE );
setBackground( Color.white );
setSize( 300, 300 );
show();
tools = new ToolWindow();
}
public void paint( Graphics g )
{
g.setColor( drawingColor );
if ( textOn ) {
g.setFont( new Font( font, Font.PLAIN, fontSize ) );
g.drawString( text.getText(), topX, topY );
textOn = false;
return;
}
if ( shape != LINE ) {
topX = Math.min( topX, bottomX );
topY = Math.min( topY, bottomY );
}
if ( filled && shape != LINE )
switch ( shape ) {
case OVAL:
g.fillOval( topX, topY, width, height );
break;
case RECT:
g.fillRect( topX, topY, width, height );
break;
}
else
switch ( shape ) {
case OVAL:
g.drawOval( topX, topY, width, height );
break;
case LINE:
g.drawLine( topX, topY, bottomX, bottomY );
break;
case RECT:
g.drawRect( topX, topY, width, height );
break;
}
if ( clear == true ) {
g.setColor( Color.white );
g.fillArea( 0, 0, getSize().width, getSize().height );
clear = false;
}
}
private class ToolWindow extends JFrame {
public ToolWindow()
{
super( "控制窗口" );
text = new JTextField( "Text", 25 );
text.addActionListener(
new ActionListener () {
public void actionPerformed( ActionEvent event )
{
textOn = true;
repaint();
}
}
);
fontList = new JComboBox( fontNames );
fontList.setMaximumRowCount( 3 );
fontList.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent event )
{
font =
fontNames[ fontList.getSelectedIndex() ];
}
}
);
sizeList = new JComboBox( sizeNames );
sizeList.setMaximumRowCount( 3 );
sizeList.addItemListener(
new ItemListener() {
public void itemStatedChanged( ItemEvent event )
{
fontSize =
sizes[ sizeList.getSelectedIndex() ];
}
}
);
colorList = new JComboBox( colorNames );
colorList.setMaximumRowCount( 3 );
colorList.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent event )
{
drawingColor =
colors[ colorList.getSelectedIndex() ];
}
}
);
clearButton = new JButton( "Clear" );
clearButton.addActionListener(
new ClearButtonHandler() );
fillBox = new JCheckBox( "Filled" );
FillBoxHandler fillHandle = new FillBoxHandler();
fillBox.addItemListener( fillHandle );
ovalBox = new JRadioButton( "Oval", true );
lineBox = new JRadioButton( "Line", false );
rectBox = new JRadioButton( "Rectangle", false );
RadioButtonHandler handler = new RadioButtonHandler();
ovalBox.addItemListener( handler );
lineBox.addItemListener( handler );
rectBox.addItemListener( handler );
shapeGroup = new ButtonGroup();
shapeGroup.add(ovalBox);
shapeGroup.add(lineBox);
shapeGroup.add(rectBox);
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel1.setLayout( new GridLayout( 1, 4 ) );
panel2.setLayout( new GridLayout( 1, 3 ) );
panel3.setLayout( new FlowLayout() );
panel4.setLayout( new FlowLayout() );
panel1.add( ovalBox );
panel1.add( lineBox );
panel1.add( rectBox );
panel1.add( fillBox );
panel2.add( new JScrollPane( colorList ) );
panel2.add( new JScrollPane( fontList ) );
panel2.add( new JScrollPane( sizeList ) );
panel3.add( text );
panel4.add( clearButton );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( panel1 );
container.add( panel2 );
container.add( panel3 );
container.add( panel4 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setSize( 350, 175 );
setLocation( 300, 0 );
setVisible( true );
}
}
private class MouseHandler extends mouseAdapter {
public void mousePressed( MouseEvent event )
{
topX = event.getX();
topY = event.getY();
}
public void mouseReleased( MouseEvent event )
{
bottomX = event.getX();
bottomY = event.getY();
width = Math.abs( topX - bottomX );
height = Math.abs( topY - bottomY );
repaint();
}
}
private class ClearButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent event )
{
clear = true;
repaint();
}
}
private class RadioButtonHandler implements ItemListener {
public void itemStateChanged( ItemEvent event )
{
if ( event.getSource() == ovalBox )
shape = OVAL;
else if ( event.getSource() == lineBox )
shape = LINE;
else if ( event.getSource() == rectBox )
shape = RECT;
}
}
private class FillBoxHandler implements ItemListener {
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
filled = true;
else
filled = false;
}
}
public static void main( String args[] )
{
exam_92 application = new exam_92();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}