Computer - Graphics 2
Computer - Graphics 2
Introduction.
CSE 201 Graphics Contexts and Graphics Objects.
Color Control.
INTRODUCTION TO COMPUTING II Font Control.
Drawing Lines, Rectangles and Ovals.
CHAPTER – 18 Drawing Arcs.
Drawing Polygons and Polylines.
GRAPHICS IN JAVA
The Java 2D API (Application Programming Interface).
Java 2D Shapes.
1 2
Introduction to Computer Graphics Cont…
• Computer graphics: generating 2D images of a 3D world Applications of
represented in a computer. Computer Graphics
1. Entertainment: Entertainment
• Main tasks in Graphics:
15 16
Font Control Cont…
• Methods
• Class Font • isPlain()
• getStyle()
The Font class states fonts, which are used to render text in a • Returns the style of this Font. • Indicates whether or not this Font object's
visible way. style is PLAIN.
• getSize()
Constructor: creates a new Font from the specified font, and it • isBold()
• Returns the point size of this Font.
takes three arguments: • Indicates whether or not this Font object's
public Font( String name, int style, int size) • getName() style is BOLD.
• name: any font supported by system (Serif, Monospaced) • Returns the logical name of this Font.
• isItalic()
• style: constants FONT.PLAIN, FONT.ITALIC, FONT.BOLD • getFamily() • Indicates whether or not this Font object's
– Combinations: FONT.ITALIC + FONT.BOLD • Returns the family name of this Font. style is ITALIC.
• size: measured in points (1/72 of an inch) • setFont(Font f)
• getFont()
Use similar to Color • Is used to set the graphics current font to
Returns a Font object from the system
g.setFont(fontObject); properties list.
the specified font.
17 18
Cont… Cont…
Example-4: Using fonts Outline:
Outline: // set current font to SansSerif (Helvetica),
import java.awt.*; // plain, 14pt and draw a string
import javax.swing.*; import g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) ); define paint()
import java.awt.event.*; g.drawString( "SansSerif 14 point plain.", 20, 90 );
public class Fonts extends JFrame { // set current font to Serif (times), bold/italic,
public Fonts() { // 18pt and draw a string
super( "Using fonts" ); g.setColor( Color.red );
setSize( 420, 125 );
Class definition g.setFont(new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) );
setVisible(true); // show(); g.drawString( g.getFont().getName() + " " +
} g.getFont().getSize() + " point bold italic.", 20, 110 );
}
public void paint( Graphics g ) {
// set current font to Serif (Times), bold, 12pt public static void main( String args[] ) {
Fonts app = new Fonts();
main() method
// and draw a string
}
g.setFont( new Font( "Serif", Font.BOLD, 12 ) );
g.drawString( "Serif 12 point bold.", 20, 50 );
define paint() }
Xy1 ascent
baseline
descent
• Gets the standard height of a line of text in this font.
• getFontMetrics()
• Gets the font metrics of the current font.
• getFontMetrics(Font f)
• ascent is the distance this string extends above the baseline. • Gets the font metrics for the specified font.
• descent is the distance this string descends below the baseline. 21 22
Cont… Cont…
Example-5: Using FontMetrics Outline: Font font = new Font( "Serif", Font.ITALIC, 14 ); Outline:
fm = g.getFontMetrics( font );
import java.awt.*;
import
g.setFont( font ); define paint()
import java.awt.event.*; g.drawString( "Current font: " + font, 10, 130 );
import javax.swing.*; g.drawString( "Ascent: " + fm.getAscent(), 10, 145 );
g.drawString( "Descent: " + fm.getDescent(), 10, 160 );
public class Metrics extends JFrame { g.drawString( "Height: " + fm.getHeight(), 10, 175 );
public Metrics() { g.drawString( "Leading: " + fm.getLeading(), 10, 190 );
super( "Demonstrating FontMetrics" ); Class definition }
setSize( 510, 210 );
setVisible(true); // show(); public static void main( String args[] ) {
} Metrics app = new Metrics(); main() method
}
public void paint( Graphics g ) { }
g.setFont( new Font( "SansSerif", Font.BOLD, 12 ) );
FontMetrics fm = g.getFontMetrics();
g.drawString( "Current font: " + g.getFont(), 10, 40 ); define paint()
g.drawString( "Ascent: " + fm.getAscent(), 10, 55 );
g.drawString( "Descent: " + fm.getDescent(), 10, 70 ); Program Output
g.drawString( "Height: " + fm.getHeight(), 10, 85 );
g.drawString( "Leading: " + fm.getLeading(), 10, 100 );
23 24
Note: Program Output may differ on different computers
Drawing Lines, Rectangles and Ovals Cont…
• Graphics methods for drawing shapes: • Graphics methods for drawing shapes (Cont.)
• drawLine(x1, y1, x2, y2) fill3DRect(x, y, width, height, raised)
• Draws a Line from (x1, y1) to (x2, y2). • Paints a 3-D highlighted rectangle filled with the current color.
• drawRect(x1, y1, width, height) drawRoundRect(x, y, width, height, arcWidth, arcHeight)
• Draws a rectangle with upper left corner (x1,y1).
• Draws rectangle with rounded corners. See diagram next slide.
• fillRect(x1, y1, width, height)
fillRoundRect(x, y, width, height, arcWidth, arcHeight)
• As above, except fills rectangle with current color.
• Fills the specified rounded corner rectangle with the current color.
• clearRect(x1, y1, width, height) drawOval(x, y, width, height)
• As above, except fills rectangle with background color • Draws oval in bounding rectangle (see diagram).
• draw3DRect(x1, y1, width, height, isRaised) • Touches rectangle at midpoint of each side.
• Draws 3D rectangle, raised if isRaised is true, else lowered.
fillOval(x, y, width, height)
• Fills an oval bounded by the specified rectangle with the current color.
25 26
Cont… Cont…
Example-6: Drawing Lines, Rectangles and Ovals Outline:
(x, y) import java.awt.*;
drawRoundRect() parameters import java.awt.event.*; import
import javax.swing.*;
arc height
public class LinesRectsOvals extends JFrame {
arc width
private String s = "Using drawString!";
height Class definition
public LinesRectsOvals() {
super( "Drawing lines, rectangles and ovals" );
setSize( 400, 165 );
setVisible(true); // show();
}
width define paint()
(x, y) public void paint( Graphics g ) {
g.setColor( Color.red );
g.drawLine( 5, 35, 350, 35 );
drawOval() parameters
g.setColor( Color.blue );
height g.drawRect( 5, 40, 90, 55 );
g.fillRect( 100, 40, 90, 55 );
g.setColor( Color.cyan );
g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
g.drawRoundRect( 290, 40, 90, 55, 20, 20 );
width 27 28
Cont… Drawing Arcs
•Arc
g.setColor( Color.yellow );
Outline:
g.draw3DRect( 5, 100, 90, 55, true );
g.fill3DRect( 100, 100, 90, 55, false ); define paint() • Portion of an oval.
g.setColor( Color.magenta ); • Measured in degrees.
g.drawOval( 195, 100, 90, 55 );
g.fillOval( 290, 100, 90, 55 ); • Starts at a starting angle and sweeps the number of degrees
} specified by arc angle.
public static void main( String args[] ) {
main() method • Positive – counterclockwise.
}
LinesRectsOvals app = new LinesRectsOvals(); • Negative – clockwise.
}
• When drawing an arc, specify bounding rectangle for
an oval.
Program Output • drawArc(x, y, width, height, startAngle, arcAngle)
- is used to draw a circular or elliptical arc.
• fillArc(x, y, width, height, startAngle, arcAngle)
- as above but draws a solid arc (sector).
29 30
Cont… Cont…
Example-7: Drawing Arcs Outline: // start at 0 and sweep -270 degrees
import java.awt.*; g.setColor( Color.yellow );
Outline:
import javax.swing.*; g.drawRect( 185, 35, 80, 80 );
import
import java.awt.event.*; g.setColor( Color.black ); define paint()
g.drawArc( 185, 35, 80, 80, 0, -270 );
public class DrawArcs extends JFrame { // start at 0 and sweep 360 degrees
public DrawArcs() { g.fillArc( 15, 120, 80, 40, 0, 360 );
super( "Drawing Arcs" ); Class definition // start at 270 and sweep -90 degrees
setSize( 300, 170 ); g.fillArc( 100, 120, 80, 40, 270, -90 );
setVisible(true); // show(); // start at 0 and sweep -270 degrees
} g.fillArc( 185, 120, 80, 40, 0, -270 );
}
public void paint( Graphics g ) {
// start at 0 and sweep 360 degrees public static void main( String args[] ) {
g.setColor( Color.yellow );
define paint() DrawArcs app = new DrawArcs(); main() method
g.drawRect( 15, 35, 80, 80 ); }
g.setColor( Color.black ); }
g.drawArc( 15, 35, 80, 80, 0, 360 );
// start at 0 and sweep 110 degrees
g.setColor( Color.yellow ); Program Output
g.drawRect( 100, 35, 80, 80 );
g.setColor( Color.black );
g.drawArc( 100, 35, 80, 80, 0, 110 );
31 32
Drawing Polygons and Polylines Cont…
• Polygon - multisided shape. • Methods of class Polygon (Cont.):
• drawPolygon(myPolygon)
• Polyline - series of connected points.
• Draws specified closed polygon.
• Methods of class Polygon: • fillPolygon(xPoints[], yPoints[], points)
• drawPolygon(xPoints[], yPoints[], points)
• Draws a polygon, with x and y points specified in arrays. Last
• Draws a solid polygon.
argument specifies number of points. • fillPolygon(myPolygon)
• Closed polygon, even if last point different from first.
• Draws specified solid polygon.
• drawPolyline(xPoints[], yPoints[], points)
• Polygon(xValues[], yValues[], numberOfPoints)
• As above but draws a polyline.
• Open polyline. • Constructs a new polygon object.
• myPolygon.addPoint(x, y)
• Add pairs of x-y coordinates to polygon object.
33 34
Cont… Cont…
Example-8: Drawing Polygons and Polylines Outline: Outline:
Polygon poly2 = new Polygon();
import java.awt.*; poly2.addPoint( 165, 135 );
Import poly2.addPoint( 175, 150 );
define paint()
import java.awt.event.*;
import javax.swing.*; poly2.addPoint( 270, 200 );
poly2.addPoint( 200, 220 );
public class DrawPolygons extends JFrame { Class definition poly2.addPoint( 130, 180 );
public DrawPolygons() {
super( "Drawing Polygons" ); g.fillPolygon( poly2 );
setSize( 275, 230 ); }
setVisible(true); // show();
} public static void main( String args[] ) { main() method
DrawPolygons app = new DrawPolygons();
public void paint( Graphics g ) { }
int xValues[ ] = { 20, 40, 50, 30, 20, 15 }; define paint() }
int yValues[ ] = { 50, 50, 60, 80, 80, 60 };
Polygon poly1 = new Polygon( xValues, yValues, 6 ); Program Output
g.drawPolygon( poly1 );
int xValues2[ ] = { 70, 90, 100, 80, 70, 65, 60 };
int yValues2[ ] = { 100, 100, 110, 110, 130, 110, 90 };
g.drawPolyline( xValues2, yValues2, 7 );
int xValues3[ ] = { 120, 140, 150, 190 };
int yValues3[ ] = { 40, 70, 80, 60 };
g.fillPolygon( xValues3, yValues3, 4 );
35 36