Drawing Lines, Rectangles and Ovals
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
67
public class LinesRectsOvals extends JFrame {
8 private String s = "Using drawString!";
9
10 public LinesRectsOvals()
11 {
12 super( "Drawing lines, rectangles and ovals" );
13
14 setSize( 400, 165 );
15 show();
16 }
17
18 public void paint( Graphics g )
19 {
20 g.setColor( Color.red );
21 g.drawLine( 5, 30, 350, 30 );
22
23 g.setColor( Color.blue );
24 g.drawRect( 5, 40, 90, 55 );
25 g.fillRect( 100, 40, 90, 55 );
26
27 g.setColor( Color.cyan );
28 g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
29 g.drawRoundRect( 290, 40, 90, 55, 20, 20 );
30
31 g.setColor( Color.yellow );
32 g.draw3DRect( 5, 100, 90, 55, true );
33 g.fill3DRect( 100, 100, 90, 55, false );
34
35 g.setColor( Color.magenta );
36 g.drawOval( 195, 100, 90, 55 );
37 g.fillOval( 290, 100, 90, 55 );
38 }
39
40 public static void main( String args[] )
41 {
42 LinesRectsOvals app = new LinesRectsOvals();
43
44 app.addWindowListener(
45 new WindowAdapter() {
46 public void windowClosing( WindowEvent e )
47 {
48 System.exit( 0 );
49 }
50 }
51 );
52 }
53 }
Buy It Now
- This section presents a variety of Graphics methods for drawing lines, rectangles and
ovals. - For each drawing method that requires a width and height parameter, the width and height must be
nonnegative values. - Otherwise, the shape will not display.
- public void drawLine( int x1, int y1, int x2, int y2 ) Draws a line between the point (x1, y1) and the point (x2, y2).
- public void drawRect( int x, int y, int width, int height ) Draws a rectangle of the specified width and height. The top-left corner of the rectangle has the coordinates (x, y).
- public void fillRect( int x, int y, int width, int height ) Draws a solid rectangle with the specified width and height. The top-left corner of the rectangle has the coordinate (x, y).
- public void clearRect( int x, int y, int width, int height ) Draws a solid rectangle with the specified width and height in the current background color. The top-left corner of the rectangle has the coordinate (x, y).
- public void drawRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight )
- Draws a rectangle with rounded corners in the current color with the specifiedwidth and height. The arcWidth and arcHeight determine the rounding of the corners
- public void fillRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight )
- Draws a solid rectangle with rounded corners in the current color with the specified width and height. The arcWidth and arcHeight determine the rounding of the corners
- public void draw3DRect( int x, int y, int width, int height, boolean b ) Draws a three-dimensional rectangle in the current color with the specified width and height. The top-left corner of the rectangle has the coordinates (x, y). The rectangle appears raised when b is true and is lowered when b is false.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
67
public class LinesRectsOvals extends JFrame {
8 private String s = "Using drawString!";
9
10 public LinesRectsOvals()
11 {
12 super( "Drawing lines, rectangles and ovals" );
13
14 setSize( 400, 165 );
15 show();
16 }
17
18 public void paint( Graphics g )
19 {
20 g.setColor( Color.red );
21 g.drawLine( 5, 30, 350, 30 );
22
23 g.setColor( Color.blue );
24 g.drawRect( 5, 40, 90, 55 );
25 g.fillRect( 100, 40, 90, 55 );
26
27 g.setColor( Color.cyan );
28 g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
29 g.drawRoundRect( 290, 40, 90, 55, 20, 20 );
30
31 g.setColor( Color.yellow );
32 g.draw3DRect( 5, 100, 90, 55, true );
33 g.fill3DRect( 100, 100, 90, 55, false );
34
35 g.setColor( Color.magenta );
36 g.drawOval( 195, 100, 90, 55 );
37 g.fillOval( 290, 100, 90, 55 );
38 }
39
40 public static void main( String args[] )
41 {
42 LinesRectsOvals app = new LinesRectsOvals();
43
44 app.addWindowListener(
45 new WindowAdapter() {
46 public void windowClosing( WindowEvent e )
47 {
48 System.exit( 0 );
49 }
50 }
51 );
52 }
53 }
Buy It Now
0 comments:
Post a Comment