Fonts And Image. 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.

Slides:



Advertisements
Similar presentations
Preloading Images for better Performance Using the MediaTracker class.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Made with love, by Zachary Langley Applets The Graphics Presentation.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 3.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
COSC 4126 images - D Goforth Images, Buffering and Animation controlling full screen graphics.
Java Applets A First Program. Applet Example /* The world’s simplest program, repeated once more in another format in an attempt to turn all of you into.
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
Enahnced Digital Clock Applet Setting applet parameters in the web page. The applet tag in HTML:
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
23-Jun-15 Images. Drawing images Java can display.gif,.jpg (or.jpeg ), and.png images These are the only kinds it can display Displaying them is (relatively)
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
(c)2003 E.S.Boese1 Loops Chapter 13 - Student. (c)2003 E.S.Boese 2 Repetition Statements Repetition statements allow us to execute a statement ________.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
A Simple Applet.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
GUI Tutorial Images. Useful Info – not on final exam.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Java Applets Chapter 15. Overview A Java Applet allows us to run a Java program from a web browser. Usually, just the.class file is sent to the client.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Internet Software Development Applets Paul J Krause.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Applets.
JAPPLET.
PROGARMMING THROUGH JAVA Presentation on IMAGES Group No:3 Presented By: Anthony Narzary (DC2012MCA0002) Chandra Gupta Bora (DC2012MCA0009) Dipankar Saikia.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
 2000 Prentice Hall, Inc. All rights reserved. Java Multimedia: Images, Animation, Audio and Video Outline 30.1 Introduction 30.2Loading, Displaying and.
(c)2009 by E.S. Boese. All Rights Reserved. Classes Chapter 13 – Lecture Slides 1 Got class?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Creating Your Own Widgets CompSci 230 S Software Construction.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Features of Java CS 3331 Sections and 5.5.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
1 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
1 CSE 331 Design Patterns 2: Prototype, Factory slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer
EE2E1. JAVA Programming Lecture 5 Graphics programming and Swing.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
User Interface Programming In Java
Mobile Computing With Android ACST 4550 Bitmaps, Fonts and Gestures
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Java applets 1/3/2019.
Presentation transcript:

Fonts And Image

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);

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.

FontMetrics Basic terminology we need to know. –Baseline –Ascent –Descent –Leading –Height Some methods in FontMetrics class –stringWidth () –getWidth() –getHeight()

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. }

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); }

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.

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); }

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); }

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.

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); }