1 / 18

Review of Applets & Compute Graphics GUI

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.

lholland
Download Presentation

Review of Applets & Compute Graphics GUI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. • Coming Next: GUI Programming (Part I). Review of Applets and Graphics

  2. Writing Applets • Always extends theJAppletclass, which is a subclass of Applet for Swing components. • Overrideinit(), start(), stop(), anddestroy() if necessary. By default, these methods are empty. • Add your own methods and data if necessary. • Applets are always embedded in anHTML page. Review of Applets and Graphics

  3. The <applet> 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] > <param name=param_name1 value=param_value1> </applet> Review of Applets and Graphics

  4. 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); Review of Applets and Graphics

  5. 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); } Review of Applets and Graphics

  6. Drawing Geometric Figures Review of Applets and Graphics

  7. Java Support for Graphics: Common Classes java.awt Font Graphics Polygon Color FontMetrics Component 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: 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 and constants for manipulating fonts contains methods for creating polygons Review of Applets and Graphics

  8. Using Applet to draw a rectangle Default color: Black 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); } // Endof paint() method} // End of RectangleApplet class Review of Applets and Graphics

  9. Experience with Graphics: Example 1 Here is an example of simple plain text on a frame. 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 FrameTest1class MyFrame extends Frame { public MyFrame() { final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 300; setSize(FRAME_WIDTH, FRAME_HEIGHT); } // End of MyFrame() constructorpublic void paint(Graphics g) { g.drawString("Hello world!",100,100); } // End of paint() method} // End of MyFrame class Review of Applets and Graphics

  10. Experience with Graphics: Example 2 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() constructorpublic 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 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 Review of Applets and Graphics

  11. 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 Review of Applets and Graphics

  12. 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 } Review of Applets and Graphics

  13. 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 Review of Applets and Graphics

  14. Java Support for Graphics: Component Class public void paint(Graphics g);This methodmust 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. Review of Applets and Graphics

  15. 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. Review of Applets and Graphics

  16. Example Displaying Images Review of Applets and Graphics

  17. Example Displaying Image(Cont.) Review of Applets and Graphics

  18. Example Displaying Image(Cont.) Review of Applets and Graphics

More Related