Download presentation
Presentation is loading. Please wait.
1
Fonts And Image
2
Fonts To display text, we choose a font. Recall that we are dealing with pixels in graphics programming. To choose a font, we need to provide three information. –Font family name (Ex. Hevetica) –Optional Suffix (Ex. Italic) –Size (Ex. 12) Font font = new Font(“Helvetica”,Font.BOLD,14); After you decide with font, let Graphics know it. g.setFont(font); Now whatever you draw should be written in “Hevetica, Bold, 14”. g.drawString(“Hello World”,12,50);
3
Mixture of fonts What if you want to use more than one fonts for a string? Obviously you need to call drawString() more than once. And each time you need to set different font with setFont() Now the problem is we need to give a specific position of start of substring with drawString(). In other words, we need to know how much space (in terms of pixels) each substring takes up. To solve this problem, Java supports FontMetrics class, which encapsulates a lot of information about a font. It has methods that let you measure the size of the string.
4
FontMetrics Basic terminology we need to know. –Baseline –Ascent –Descent –Leading –Height Some methods in FontMetrics class –stringWidth () –getWidth() –getHeight()
5
import java.awt.*; import javax.swing.*; public class FontDemo extends JApplet { private Font sf; private Font sif; private FontMetrics sfm,sifm; String s1 = "Good Morning "; String s2 = "Pullman."; private void setFont(Graphics g) { if ( sf != null ) return; sf = new Font("SansSerif",Font.BOLD,20); sif = new Font("Helvetica",Font.BOLD+Font.ITALIC,20); sfm = g.getFontMetrics(sf); sifm = g.getFontMetrics(sif); } public void paint(Graphics g) { // next slide. }
6
public void paint(Graphics g) { setFont(g); int w = sfm.stringWidth(s1) + sifm.stringWidth(s2); Dimension d = getSize(); int x = (d.width - w)/2; int y = (d.height - sfm.getHeight())/2 + sfm.getAscent(); g.setFont(sf); g.setColor(Color.red); g.drawString(s1,x,y); x += sfm.stringWidth(s1); g.setFont(sif); g.setColor(Color.blue); g.drawString(s2,x,y); }
7
Image So far we have learned how to build a image using various drawing methods in Graphics class. Now let’s look at how to display pre-built images, which are usually stored in files. (gif, jpeg, etc.) Displaying an image is two step process. –Get an image file and convert it to Image object. –Display Image object using drawImage() method in Graphics getImage() method is provided in applet class to get an image file from originating source. For application, use getImage() in Toolkit Class.
8
import java.awt.*; import javax.swing.*; public class ImageDisplay extends JApplet { Image image; public void init() { image = getImage(getCodeBase(),"cab.gif"); } public void paint(Graphics g) { g.drawImage(image,0,0,this); }
9
import java.awt.*; import javax.swing.*; public class ImageDisplay extends JApplet { Image image; public void init() { image = getImage(getCodeBase(),"cab.gif"); } public void paint(Graphics g) { g.drawImage(image,0,0,Color.red,this); }
10
MediaTracker Class What happens when you call getImage is not what you might expect. The program actually continues to the next instruction without checking whether the image is loaded or not. Almost all the time, the image is not ready to be displayed when the control is returned from getImage. This causes lots of trouble for animation. (detailed later) To make sure whether the image (or set of images) is loaded, we use MediaTracker class.
11
public class ImageDisplay extends JApplet { Image image; int width,height; public void init() { image = getImage(getCodeBase(),"cab.gif"); loadImage(); width = image.getWidth(this); height = image.getHeight(this); } public void loadImage() { MediaTracker tracker = new MediaTracker(this); tracker.addImage(image,0); try { tracker.waitForID(0); } catch (InterruptedException e) {} } public void paint(Graphics g) { g.drawImage(image,0,0,this); g.drawImage(image,width+20,0,width,height/2,this); g.drawImage(image,0,height+20,width,height*2,this); g.drawImage(image,width+20,height+20,width/2,height/2,this); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.