Copy and compile the following codes in your local workstation.
JAVA GUI Slides
1 // Fig. 12.5: ShowColors.java
2 // Demonstrating Colors.
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class ShowColors extends JFrame {
7
8 // constructor sets window's title bar string and dimensions
9 public ShowColors()
10 {
11 super( "Using colors" );
12
13 setSize( 400, 130 );
14 setVisible( true );
15 }
16
17 // draw rectangles and Strings in different colors
18 public void paint( Graphics g )
19 {
20 // call superclass's paint method
21 super.paint( g );
22
23 // set new drawing color using integers
24 g.setColor( new Color( 255, 0, 0 ) );
25 g.fillRect( 25, 25, 100, 20 );
26 g.drawString( "Current RGB: " + g.getColor(), 130, 40 );
27
28 // set new drawing color using floats
29 g.setColor( new Color( 0.0f, 1.0f, 0.0f ) );
30 g.fillRect( 25, 50, 100, 20 );
31 g.drawString( "Current RGB: " + g.getColor(), 130, 65 );
32
33 // set new drawing color using static Color objects
34 g.setColor( Color.BLUE );
35 g.fillRect( 25, 75, 100, 20 );
36 g.drawString( "Current RGB: " + g.getColor(), 130, 90 );
37
38 // display individual RGB values
39 Color color = Color.MAGENTA;
40 g.setColor( color );
41 g.fillRect( 25, 100, 100, 20 );
42 g.drawString( "RGB values: " + color.getRed() + ", " +
43 color.getGreen() + ", " + color.getBlue(), 130, 115 );
44
45 } // end method paint
46
47 // execute application
48 public static void main( String args[] )
49 {
50 ShowColors application = new ShowColors();
51 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
52 }
53
54 } // end class ShowColors
====================================
1 // Fig. 12.6: ShowColors2.java
2 // Choosing colors with JColorChooser.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class ShowColors2 extends JFrame {
8 private JButton changeColorButton;
9 private Color color = Color.LIGHT_GRAY;
10 private Container container;
11
12 // set up GUI
13 public ShowColors2()
14 {
15 super( "Using JColorChooser" );
16
17 container = getContentPane();
18 container.setLayout( new FlowLayout() );
19
20 // set up changeColorButton and register its event handler
21 changeColorButton = new JButton( "Change Color" );
22 changeColorButton.addActionListener(
23
24 new ActionListener() { // anonymous inner class
25
26 // display JColorChooser when user clicks button
27 public void actionPerformed( ActionEvent event )
28 {
29 color = JColorChooser.showDialog(
30 ShowColors2.this, "Choose a color", color );
31
32 // set default color, if no color is returned
33 if ( color == null )
34 color = Color.LIGHT_GRAY;
35
36 // change content pane's background color
37 container.setBackground( color );
38 }
39
40 } // end anonymous inner class
41
42 ); // end call to addActionListener
43
44 container.add( changeColorButton );
45
46 setSize( 400, 130 );
47 setVisible( true );
48
49 } // end ShowColor2 constructor
50
51 // execute application
52 public static void main( String args[] )
53 {
54 ShowColors2 application = new ShowColors2();
55 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
56 }
57
58 } // end class ShowColors2
0 Comments