File "SelectShape.java"
Full Path: /home/analogde/www/Ebook/Informatique/JAVA/Source_TLS/Move/SelectShape.java
File size: 1.88 KB
MIME-type: text/x-c
Charset: utf-8
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SelectShape extends JFrame {
private final int CIRCLE = 0;
private final int SQUARE = 1;
private final int OVAL = 2;
private final int RECTANGLE = 3;
private JComboBox choice;
private int shape;
public SelectShape()
{
choice = new JComboBox();
choice.addItem( "Circle" );
choice.addItem( "Square" );
choice.addItem( "Oval" );
choice.addItem( "Rectangle" );
choice.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent e )
{
setShape( choice.getSelectedIndex() );
repaint();
}
}
);
getContentPane().add( choice, BorderLayout.SOUTH );
setSize( 300, 200 );
show();
}
public void paint( Graphics g )
{
for ( int k = 1; k <= 20; k ++ ) {
int w = ( int ) ( Math.random() * getSize().width );
int h = ( int ) ( Math.random() * getSize().height );
int x = ( int ) ( Math.random() * getSize().width );
int y = ( int ) ( Math.random() * getSize().height );
switch ( shape ) {
case CIRCLE:
g.drawOval( x, y, w, w );
break;
case SQUARE:
g.drawRect( x, y, w, w );
break;
case OVAL:
g.drawOval( x, y, w, h );
break;
case RECTANGLE:
g.drawRect( x, y, w, h );
break;
}
}
}
public void setShape( int s ) { shape = s; }
public static void main( String args[] )
{
SelectShape app = new SelectShape();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}