Download presentation
Presentation is loading. Please wait.
Published byOswin Malone Modified over 9 years ago
1
1 Graphics, Fonts and Color Chapter 9
2
2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs) l Fonts - creating and using l Color Color class (forground,background)
3
3 java.awt.Graphics l import java.awt.Graphics l public class myClass extends java.applet.Applet { l The usual start to an applet using any Graphics
4
4 Coordinates l The top Left is 0,0 l The bottom Right is x,x (x = the largest) l Graphics class includes: –public void paint(Graphics g) { –// how to draw a line –g.drawline(25,25,75,75); –// how to draw rectangles (filled and outline) –g.drawRect(30,30,60,60); –g.fillRect(40,40,120,120);
5
5 //remember public void //paint(Graphics g) { l // rounded edged rectangles –g.drawRoundRect(20,20,60,60,10,10); »//the last 10’s are how large an arc to cut corners –g.fillRoundRect(120,20,60,60,20,20); l // 3-D –g.draw3DRect(20,20,60,60,true); //raised –g.draw3DRect(120,20,60,60,false); //sunken
6
6 Polygons l drawPolygon( ) and fillPolygon( ) l an integer array of x coordinates l an integer array of y coordinates l integer of total number of points l Example: public void paint(Graphics g ) { –int xs[] = { 39,94,142,53,58,26); –int ys[] = { 33,74,36,70,108,80,106); –int pts = xs.length; // or ys.length both equal l g.drawPolygon(xs,ys,pts);
7
7 Assignment Chapter Nine l You are to create an applet with some of the shapes in this chapter. As an example Fig. 9.19 “The Lamp applet.” Only better... l continuing with polygons: –after: –int pts = xs.length; –Polygon poly = new Polygon(xs, ys, pts) –// also calls the same object l // new material poly.addPoint(20,35)
8
8 A filled Polygon l say you have the same lines as the first polygon l Polygon poly = new Polygon(xs, ys,pts); l g.fillPolygon(poly); l }
9
9 Ovals l public void paint (Graphics g) { –g.drawOval(20,20,60,60); –g.fillOval(120,20,100,60); } l What are the four coordinates? l 1-2. x,y top corner l 3-4. width & height of oval itself
10
10 Arcs: l Six arguments –1 and 2. starting corner - the first two –3. width –4. heighth//play with some arcs ! –5. angle 6. degrees l g.drawArc(20,20,60,60,90,180); l g.fillArc(120,20,60,60,90,180); l g.drawArc(10,20,150,50,25, -130); l g.drawArc(10,80,150,50,25, -130);
11
11 The Lamp Arc l import java.awt.*: // call it all... l public class Lamp extends java.applet.Applet { l public void paint(Graphics g) { l // lamp platform l g.fillRect(0,250,290,290); l // the lamp base l g.drawLine(125,250,125,160); l g.drawLine(175,250,175,160);
12
12 l // the lamp shade, top and bottom edges –g.drawArc(85,157,130,50,-65,312); –g.drawArc(85,87,130,50,62,58); l // lamp shade, sides –g.drawLine(85,177,119,89); –g.drawLine(215,177,181,89); l // dots on shade –g.fillArc(78,120,40,40,40,63,-174); –g=.fillOval(120,96,40,40); –g.fillArc(173,100,40,40,110,180); } }
13
13 Big Deal - Copying and Cleaning MOVING them around ! ! ! l g.copyArea(0,0,100,100,100,0); l // how it works... l Six arguments again: –1 and 2 x,y coordinates of upper left corner, –3 and 4 width - and height of that rectangle –5 distance in x and y directions l Start at 0, 0 move a 100 by 100 pixel l rectangle directly to the right (0)
14
14 Clear an area: l g.clearRect( 1,2,3,4) //arguments of rectangle before l g.clearRect(0,0,size( ).width, size( ) height); l What really happens is that the area is filled with the background color
15
15 Text and Fonts l font –name –style –pointsize l FontMetrics - info about actual height and width of a given character l Font f = new Font(“TimesRoman”, Font.BOLD, 24); //Font.BOLD + // FontITALIC is possible
16
16 Usually stick to TimesRoman, Helvetica or Courier l complete list l getFontList( )
17
17 Drawing Characters and Strings l Use setFont( ) first l drawChars( ) and drawString( ) l public void paint (Graphics g ) { –Font f = new Font(“TimesRoman”, Font.PLAIN, 72); –g.setFont(f); –g.drawString(“This is a big font. “, 10, 100); l }
18
18 l import java.awt.Font;// Fig 9.2 l import java.awt.Graphics; l public class ManyFonts extends java.applet.Applet { –public void paint(Graphics g) { –Font f = new Font(“TimesRoman”, Font.PLAIN, 18); –Font f b= new Font(“TimesRoman”, Font.BOLD, 18); –Font f i= new Font(“TimesRoman”, Font.ITALIC, 18); –Font f bi= new Font(“TimesRoman”, Font.BOLD +Font.ITALIC, 18);
19
19 l g.setFont(f); l g.drawString(“This is a plain font”, 10, 25); l g.setFont(fb); l // same idea for bold, italic –bold italic –See the Fig 9.20 for examples of the fonts
20
20 Finding Out Information about a Font l getFont( ) Graphics returns font object as previously set by setFont( ) l getName( ) Font name of font returned l getSize Font gets font size l getStyle Font stye 0 = plain, 1 = bold, 2 = italic, 3 = bold italic l isPlain Font true or false l isBold Font true or false l isItalic Font true or false
21
21 More detail - FontMetric l Font f = new Font(“TimesRoman”, Font.BOLD, 36); l FontMetrics fmetrics = getFontMetrics(f); l g.setfont(f); l stringWidth(string) = width of string l charWidth(char) = gets width of character l getAscent( ) = distant baseline and top l getDescent( ) = distant baseline and bottom l getLeading( ) space between lines of text l setHeight( ) total height of font
22
22 Centering a String - List 9.3 l import java,awt.Font; l import java.awt.Graphics; l import java.awt.FontMetrics; l public class Centered extends java.applet.Applet { l public void paint(Graphics g) { l Font f = new Font(“TimesRoman”, Font.PLAIN, 36);
23
23 List 9.3 Contd. l String s = “This is how the world ends.”; l int xstart = (size( ).width - fmstringWidth(s)) /2; l int ystart = (size( ).height + fm.getHeight( )) /2; l g.drawString(s, xstart, ystart); l }
24
24 Color l Java 24 bit color so 0 - 255 l Combination of Red, green and blue l 0,0,0 = Black l 255, 255, 255 = White l 192,192,192 = lightGray l 128,128,128 = gray // see pages 168- 169 l Millions Colors Possible BUT usually 256 are available on a platform // create NEW l Color c = new Color(140,140,140); l Color c = new Color(0.55,0.55,0.55)
25
25 Test and Set the Current Colors l g.setColor(Color.green); l // that is easy enough l setBackground(Color.white); l setForeground(Color.black); l // You can get the current Color by use of l getColor( ); getForeground( ); getBackground
26
26 A Single Color Example List 9.4 l import java.awt.Graphics; import java.awt.Color; l public class ColorBoxes extends java.applet.Applet { l int rval, gval, bval; l for(int j = 30; j < (size90.height -25); j+=30) –for(int i = 5; i<(size).width -25); i+=30) {
27
27 l rval = (int)Math.floor(Math.random( )* 256); l gval =(int)Math.floor(Math.random( )* 256); l bval =(int)Math.floor(Math.random( )* 256); l g.setColor(new Color(rval,gval,bval)); l g.fillRect(i, j, 25, 25); l g.setColor(Color.black); l g.drawRect(i-1, j-1, 25, 25); //Random Colors l } } } // See colors on page 171
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.