Docsity

Graficos y Java 2D - Apuntes - Programación - Informática - Parte 2, Apuntes de Informática

Toggle sidebar
bg1
28
Graphics methods that draw lines, rectangles
and ovals
Method
Description
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 lowered when b is false.
public void fill3DRect( int x, int y, int width, int height, boolean b )
Draws a filled 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 lowered
when b is false.
public void drawOval( int x, int y, int width, int height )
Draws an oval in the current color with the specified width and height. The
bounding rectangle’s top-left corner is at the coordinates (x, y). The oval
touches all four sides of the bounding rectangle at the center of each side (see
Fig. 12.16).
public void fillOval( int x, int y, int width, int height )
Draws a filled oval in the current color with the specified width and height.
The bounding rectangle’s top-left corner is at the coordinates (x, y). The oval
touches all four sides of the bounding rectangle at the center of each side (see
Fig. 12.16).
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Vista previa parcial del texto

¡Descarga Graficos y Java 2D - Apuntes - Programación - Informática - Parte 2 y más Apuntes en PDF de Informática solo en Docsity!

Graphics methods that draw lines, rectangles

and ovals

Method Description 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 lowered when b is false.

public void fill3DRect( int x, int y, int width, int height, boolean b )

Draws a filled 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 lowered

when b is false.

public void drawOval( int x, int y, int width, int height )

Draws an oval in the current color with the specified width and height. The

bounding rectangle’s top-left corner is at the coordinates (x, y). The oval

touches all four sides of the bounding rectangle at the center of each side (see

Fig. 12.16).

public void fillOval( int x, int y, int width, int height )

Draws a filled oval in the current color with the specified width and height.

The bounding rectangle’s top-left corner is at the coordinates (x, y). The oval

touches all four sides of the bounding rectangle at the center of each side (see

Fig. 12.16).

Láminas LinesRectsOvals .java 1 // Fig. 12.14: LinesRectsOvals.java 2 // Drawing lines, rectangles and ovals. 3 import java.awt.; 4 import javax.swing.; 5 6 public class LinesRectsOvals extends JFrame { 7 8 // set window's title bar String and dimensions 9 public LinesRectsOvals() 10 { 11 super( "Drawing lines, rectangles and ovals" ); 12 13 setSize( 400, 165 ); 14 setVisible( true ); 15 } 16 17 // display various lines, rectangles and ovals 18 public void paint( Graphics g ) 19 { 20 super.paint( g ); // call superclass's paint method 21 22 g.setColor( Color.RED ); 23 g.drawLine( 5, 30, 350, 30 ); 24 25 g.setColor( Color.BLUE ); 26 g.drawRect( 5, 40, 90, 55 ); 27 g.fillRect( 100, 40, 90, 55 );

Arc width and arc height for rounded rectangles

x

y

arc height

a rc width

w idth

height

Oval bounded by a rectangle

(x, y)

height

w id th

Positive and negative arc angles

Positive angles Negative angles

Fig. 12.18 Graphics methods for drawing

arcs

Method Description

public void drawArc( int x, int y, int width, int height, int startAngle,

int arcAngle )

Draws an arc relative to the bounding rectangle’s top-left coordinates ( x , y )

with the specified width and height. The arc segment is drawn starting at startAngle and sweeps arcAngle degrees.

public void fillArc( int x, int y, int width, int height, int startAngle,

int arcAngle )

Draws a solid arc (i.e., a sector) relative to the bounding rectangle’s top-left coordinates ( x , y ) with the specified width and height. The arc segment is drawn starting at startAngle and sweeps arcAngle degrees.

Láminas DrawArcs.java Lines 30- Lines 36- Line 41 Line 44 Line 47

28 // start at 0 and sweep 110 degrees 29 g.setColor( Color.YELLOW ); 30 g.drawRect( 100, 35, 80, 80 ); 31 g.setColor( Color.BLACK ); 32 g.drawArc( 100, 35, 80, 80, 0, 110 ); 33 34 // start at 0 and sweep -270 degrees 35 g.setColor( Color.YELLOW ); 36 g.drawRect( 185, 35, 80, 80 ); 37 g.setColor( Color.BLACK ); 38 g.drawArc( 185, 35, 80, 80, 0, -270 ); 39 40 // start at 0 and sweep 360 degrees 41 g.fillArc( 15, 120, 80, 40, 0, 360 ); 42 43 // start at 270 and sweep -90 degrees 44 g.fillArc( 100, 120, 80, 40, 270, -90 ); 45 46 // start at 0 and sweep -270 degrees 47 g.fillArc( 185, 120, 80, 40, 0, -270 ); 48 49 } // end method paint 50 Draw second arc that sweeps 110 degrees and is contained in rectangle Draw third arc that sweeps -270 degrees and is contained in rectangle Draw fourth arc that is filled, has starting angle 0 and sweeps 360 degrees Draw fifth arc that is filled, has starting angle 270 and sweeps -90 degrees Draw sixth arc that is filled, has starting angle 0 and sweeps -270 degrees

Láminas DrawArcs.java 51 // execute application 52 public static void main( String args[] ) 53 { 54 DrawArcs application = new DrawArcs(); 55 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 56 } 57 58 } // end class DrawArcs

Graphics methods for drawing polygons and

class Polygon methods

Method Description Graphics methods for drawing polygons public void drawPolygon( int xPoints[], int yPoints[], int points ) Draws a polygon. The x - coordinate of each point is specified in the xPoints array and the y - coordinate of each point is specified in the yPoints array. The last argument specifies the number of points. This method draws a closed polygon. If the last point is different from the first point, the polygon is closed by a line that connects the last point to the first point. public void drawPolyline( int xPoints[], int yPoints[], int points ) Draws a sequence of connected lines. The x - coordinate of each point is specified in the xPoints array and the y - coordinate of each point is specified in the yPoints array. The last argument specifies the number of points. If the last point is different from the first point, the polyline is not closed. public void drawPolygon( Polygon p ) Draws the specified polygon. public void fillPolygon( int xPoints[], int yPoints[], int points ) Draws a solid polygon. The x - coordinate of each point is specified in the xPoints array and the y - coordinate of each point is specified in the yPoints array. The last argument specifies the number of points. This method draws a closed polygon. If the last point is different from the first point, the polygon is closed by a line that connects the last point to the first point. public void fillPolygon( Polygon p ) Draws the specified solid polygon. The polygon is closed.

Graphics methods for drawing polygons and

class Polygon methods

Method Description Polygon constructors and methods public Polygon() Constructs a new polygon object. The polygon does not contain any points. public Polygon( int xValues[], int yValues[], int numberOfPoints ) Constructs a new polygon object. The polygon has numberOfPoints sides, with each point consisting of an x - coordinate from xValues and a y - coordinate from yValues. public void addPoint( int x, int y ) Adds pairs of x- and y - coordinates to the Polygon.

Láminas DrawPolygons.ja va Lines 28- Line 31 Lines 33- Line 39 28 int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 }; 29 int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 }; 30 31 g.drawPolyline( xValues2, yValues2, 7 ); 32 33 int xValues3[] = { 120, 140, 150, 190 }; 34 int yValues3[] = { 40, 70, 80, 60 }; 35 36 g.fillPolygon( xValues3, yValues3, 4 ); 37 38 Polygon polygon2 = new Polygon(); 39 polygon2.addPoint( 165, 135 ); 40 polygon2.addPoint( 175, 150 ); 41 polygon2.addPoint( 270, 200 ); 42 polygon2.addPoint( 200, 220 ); 43 polygon2.addPoint( 130, 180 ); 44 45 g.fillPolygon( polygon2 ); 46 47 } // end method paint 48 49 // execute application 50 public static void main( String args[] ) 51 { 52 DrawPolygons application = new DrawPolygons(); 53 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 54 } 55 56 } // end class DrawPolygons int arrays specifying Polyline points Draw Polyline to screen Specify points and draw (filled) Polygon to screen Method addPoint adds pairs of x-y coordinates to a Polygon

Láminas DrawPolygons.ja va

Java2D API

• Formas (shapes) Java 2D

– Package java.awt.geom

• Ellipse2D.Double

• Rectangle2D.Double

• RoundRectangle2D.Double

• Arc3D.Double

• Lines2D.Double

Láminas Shapes.java 1 // Fig. 12.22: Shapes.java 2 // Demonstrating some Java2D shapes. 3 import java.awt.; 4 import java.awt.geom.; 5 import java.awt.image.; 6 import javax.swing.; 7 8 public class Shapes extends JFrame { 9 10 // set window's title bar String and dimensions 11 public Shapes() 12 { 13 super( "Drawing 2D shapes" ); 14 15 setSize( 425, 160 ); 16 setVisible( true ); 17 } 18 19 // draw shapes with Java2D API 20 public void paint( Graphics g ) 21 { 22 super.paint( g ); // call superclass's paint method 23 24 Graphics2D g2d = ( Graphics2D ) g; // cast g to Graphics2D 25