1 // Fig. 12.18: LinesRectsOvalsJPanel.java
2 // Drawing lines, rectangles and ovals.
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import javax.swing.JPanel;
6
7 public class LinesRectsOvalsJPanel extends JPanel
8 {
9 // display various lines, rectangles and ovals
10 public void paintComponent( Graphics g )
11 {
12 super.paintComponent( g ); // call superclass's paint method
13
14 this.setBackground( Color.WHITE );
15
16 g.setColor( Color.RED );
17 g.drawLine( 5, 30, 380, 30 );
18
19 g.setColor( Color.BLUE );
20 g.drawRect( 5, 40, 90, 55 );
21 g.fillRect( 100, 40, 90, 55 );
22
23 g.setColor( Color.CYAN );
24 g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
25 g.drawRoundRect( 290, 40, 90, 55, 20, 20 );
26
27 g.setColor( Color.YELLOW );
28 g.draw3DRect( 5, 100, 90, 55, true );
29 g.fill3DRect( 100, 100, 90, 55, false );
30
31 g.setColor( Color.MAGENTA );
32 g.drawOval( 195, 100, 90, 55 );
33 g.fillOval( 290, 100, 90, 55 );
34 } // end method paintComponent
35 } // end class LinesRectsOvalsJPanel
1 // Fig. 12.19: LinesRectsOvals.java
2 // Drawing lines, rectangles and ovals.
3 import java.awt.Color;
4 import javax.swing.JFrame;
5
6 public class LinesRectsOvals
7 {
8 // execute application
9 public static void main( String args[] )
10 {
11 // create frame for LinesRectsOvalsJPanel
12 JFrame frame =
13 new JFrame( "Drawing lines, rectangles and ovals" );
14 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
15
16 LinesRectsOvalsJPanel linesRectsOvalsJPanel =
17 new LinesRectsOvalsJPanel();
18 linesRectsOvalsJPanel.setBackground( Color.WHITE );
19 frame.add( linesRectsOvalsJPanel ); // add panel to frame
20 frame.setSize( 400, 210 ); // set frame size
21 frame.setVisible( true ); // display frame
22 } // end main
23 } // end class LinesRectsOvals

Tidak ada komentar:
Posting Komentar