1 Review of applets & Computer Graphis GUILecture 6 Review of Applets & Compute Graphics GUI Overview  Introduction to Graphics. Applets: a quick review.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
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.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
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.
Chapter 10 Getting Started with GUI Programming F GUI Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames F Layout.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Web Design & Development Lecture 18. Java Graphics.
CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
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.
JAPPLET.
Chapter 10 Getting Started with GUI Programming F GUI Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames F Layout.
GUI programming Graphical user interface-based programming.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Chapter 8: Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames The relationship between frame and.
1 Chapter 10 Getting Started with Graphics Programming F Graphics Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames.
Applets Session 8. Java Simplified / Session 8 / 2 of 31 Review The Abstract Windowing Toolkit (AWT) is a set of classes that allow us to create a graphical.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
Objectives of This Session
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.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Applets and Multimedia.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
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)
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Chapter 8 Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames  Creating & centering frames, adding.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Chapter 10 Getting Started with Graphics Programming
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

1 Review of applets & Computer Graphis GUILecture 6 Review of Applets & Compute Graphics GUI Overview  Introduction to Graphics. Applets: a quick review.  Some Classes for Graphics in Java.  Drawing Geometric Figures »Lines, Rectangles, Ovals, Arcs  Coloring Graphics Shapes.  Applications with Many Frames  Example Displaying Images 

2 Review of applets & Computer Graphis GUILecture 6 Writing Applets Always extends the JApplet class, which is a subclass of Applet for Swing components. Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. l Add your own methods and data if necessary. l Applets are always embedded in an HTML page.

3 Review of applets & Computer Graphis GUILecture 6 The HTML Tag < applet code=classfilename.class width=applet_viewing_width_in_pixels height=applet_viewing_height_in_pixels [archive=archivefile] [codebase=applet_url] [vspace=vertical_margin] [hspace=horizontal_margin] [align=applet_alignment] [alt=alternative_text] >

4 Review of applets & Computer Graphis GUILecture 6 The Color Class Color c = new Color(r, g, b); r, g, and b specify a color by its red, green, and blue components. Example: Color c = new Color(128, 100, 100); You can use the following methods to set the component’s background and foreground colors: setBackground(Color c) setForeground(Color c) Example: setBackground(Color.yellow); setForeground(Color.red);

5 Review of applets & Computer Graphis GUILecture 6 The Font Class Font myFont = Font(name, style, size); Example: Font myFont = new Font("SansSerif ", Font.BOLD, 16); Font myFont = new Font("Serif", Font.BOLD+Font.ITALIC, 12); public void paint(Graphics g) { Font myFont = new Font("Times", Font.BOLD, 16); g.setFont(myFont); g.drawString("Welcome to Java", 20, 40); //set a new font g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 12)); g.drawString("Welcome to Java", 20, 70); }

6 Review of applets & Computer Graphis GUILecture 6 Drawing Geometric Figures Drawing Lines drawLine(x1, y1, x2, y2); Drawing Rectangles drawRect(x, y, w, h); fillRect(x, y, w, h); Rounded Rectangles drawRoundRect(x, y, w, h, aw, ah); fillRoundRect(x, y, w, h, aw, ah); Drawing Arcs drawArc(x, y, w, h, angle1, angle2); fillArc(x, y, w, h, angle1, angle2);

7 Review of applets & Computer Graphis GUILecture 6 Java Support for Graphics: Common Classes As shown in the previous applet example, Java AWT (Abstract Windowing Toolkit) package provides a collection of classes your programs can use to perform graphics operations. Some of the common classes are: java.awt Color Component Font FontMetrics GraphicsPolygon contains methods and constants for manipulating fonts contains methods and constants for obtaining font information contains methods for drawing strings, lines, rectangles and other shapes contains methods and constants for manipulating colors contains methods for creating polygons

8 Review of applets & Computer Graphis GUILecture 6 import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; public class RectangleApplet extends Applet { public void paint (Graphics g) { Graphics2D g2 = (Graphics2D)g; Rectangle rectangle = new Rectangle(10,10,50,50); g2.draw(rectangle); } // End of paint() method } // End of RectangleApplet class Using Applet to draw a rectangle Default color: Black

9 Review of applets & Computer Graphis GUILecture 6 Experience with Graphics: Example 1 import java.awt.*; public class FrameTest1 { public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setTitle("Graphics Using Frames"); frame.show(); } // End of main method } // End of class FrameTest1 class MyFrame extends Frame { public MyFrame() { final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 300; setSize(FRAME_WIDTH, FRAME_HEIGHT); } // End of MyFrame() constructor public void paint(Graphics g) { g.drawString("Hello world!",100,100); } // End of paint() method } // End of MyFrame class Here is an example of simple plain text on a frame.

10 Review of applets & Computer Graphis GUILecture 6 Experience with Graphics: Example 2 public class FrameTest1 { public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setTitle("Graphics Using Frames"); frame.show(); } // End of main method } // End of class FrameTest1 Now, let us change the font size and color of the text. import java.awt.*; class MyFrame extends Frame { public MyFrame() { final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 300; setSize(FRAME_WIDTH, FRAME_HEIGHT); } // End of MyFrame() constructor public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; final int size = 48; Color myColor = new Color(0.9F, 0.3F, 0.5F); Font myFont = new Font("Times", Font.BOLD, size); g2.setColor(myColor); g2.setFont(myFont); g2.drawString("Hello World!",60,150); } // End of paint() method } // End of MyFrame class

11 Review of applets & Computer Graphis GUILecture 6 Applications with Many Frames one can develop standalone graphical applications with more than one window frame as shown in the example below: class MyFrame1 extends Frame { public MyFrame1() { final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 300; setSize(FRAME_WIDTH, FRAME_HEIGHT); } // End of MyFrame() constructor public void paint(Graphics g) { g.drawString("Hello world!",100,100); } // End of paint() method } // End of MyFrame1 class

12 Review of applets & Computer Graphis GUILecture 6 Applications with Many Frames (Cont’d) class MyFrame2 extends Frame {public MyFrame2() { final int FRAME_WIDTH = 500; final int FRAME_HEIGHT = 300; setSize(FRAME_WIDTH, FRAME_HEIGHT); } // End of MyFrame() constructor public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; final int size = 48; Color myColor = new Color(0.9F, 0.3F, 0.5F); Font myFont = new Font("Times", Font.BOLD, size); g2.setColor(myColor); g2.setFont(myFont); g2.drawString("Hello World!",60,150); } // End of paint() method }

13 Review of applets & Computer Graphis GUILecture 6 Applications with Many Frames (Cont’d) import java.awt.*; import java.applet.*; public class MultiFrameTest1 { public static void main(String[] args) { MyFrame1 frame1 = new MyFrame1(); frame1.setTitle("Graphics Using Frame 1"); frame1.setSize(300, 300); frame1.show(); MyFrame2 frame2 = new MyFrame2(); frame2.setTitle("Graphics Using Frame 2"); frame2.setSize(300, 300); frame2.show(); } // End of main method } // End of class MultiFrameTest1

14 Review of applets & Computer Graphis GUILecture 6 Java Support for Graphics: Component Class public void paint(Graphics g); This method must be overridden by the programmer to produce some desired effect. Note that this is the same paint() method that was used in the previous applet example and the Applet class inherited it from Component class. When you make a change to the data, your drawing is not automatically updated. You must tell the window manager that the data has changed, by calling the repaint method.  When any graphical t object is displayed, the paint() method is called automatically.  To explicitly call paint() method, repaint() method of the same object must called.  Then, repaint() method calls update() method which in turn calls paint().  Note that repaint() method performs some system-dependent tasks and should thus NOT be overridden.  The update() method is often called directly and can be overridden to enable “smoothening” of animations in multimedia applications.

15 Review of applets & Computer Graphis GUILecture 6 Displaying Images Java currently supports two image formats: GIF (Graphics Interchange Format) and JPEG(Joint Photographic Expert Group). Image filename for each of these types end with.gif and jpg. To display an image, you need to perform the following steps 1- Retrieve the image from a file or from an Internet source 2-Draw the image. To load an image from a local file or download it from an internet source you need to use getImage() method in the Applet class. To load image from the specified URL: public Image getImage(URL url) You need to draw an image in a graphics context. The Graphics class has the drawImage()method for displaying an image drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) Observer is the object on which the image is displayed.

16 Review of applets & Computer Graphis GUILecture 6 Example Displaying Images import java.applet.*; import java.awt.*; import java.awt.event.*; public class DisplayImageApplet extends Applet implements ActionListener { private ImageCanvas c; //the canvas for displaying image private TextField tfFilename; //the name of the image file private Button btShow; //the "Show" button public void init() { //create Panel p1 to hold a text field and a button Panel p1 = new Panel(); p1.setLayout(new FlowLayout()); p1.add(new Label("Filename")); p1.add(tfFilename = new TextField(" ", 10)); p1.add(btShow = new Button("Show")) // place an ImageCanvas object and p1 in the frame setLayout(new BorderLayout()); add("Center", c = new ImageCanvas()); add("South", p1); c.setBackground(Color.gray); //register listener btShow.addActionListener(this); tfFilename.addActionListener(this); } //handling the "Show" button public void actionPerformed(ActionEvent e) { if ((e.getSource() instanceof Button) || (e.getSource() instanceof TextField)) displayImage(); }

17 Review of applets & Computer Graphis GUILecture 6 Example Displaying Image(Cont.) private void displayImage() { //retrieving image Image image = getImage (getCodeBase(), tfFilename.getText().trim()); //show image in the canvas c.showImage(image); } //define the canvas for showing an image class ImageCanvas extends Canvas { private String filename; private Image image = null; public ImageCanvas() { } //set image public void showImage(Image image) { this.image = image; repaint(); } public void paint(Graphics g) { if (image != null) g. drawImage (image, 0, 0, getSize().width, getSize().height, this); }

18 Review of applets & Computer Graphis GUILecture 6 Example Displaying Image(Cont.)