1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);

Slides:



Advertisements
Similar presentations
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
Advertisements

AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
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?
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Graphics in Java Part II. Lecture Objectives Learn how to use Graphics in Java.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Enahnced Digital Clock Applet Setting applet parameters in the web page. The applet tag in HTML:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 12.1 Introduction 12.2 Graphics Contexts and Graphics Objects 12.3 Color Control 12.4 Font Control.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
Chapter 11 - Graphics and Java 2D Outline 11.1 Introduction 11.2 Graphics Contexts and Graphics Objects 11.3 Color Control 11.4 Font Control 11.5 Drawing.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 11 - Graphics and Java 2D Outline 11.1 Introduction 11.2 Graphics Contexts and Graphics Objects.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
© 2007 Lawrenceville Press Slide 1 Chapter 2 HTML An example HTML document Hello world!
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.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Veri Transferi Kes ve Yapıştır 1. import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; /** * This program demonstrates how to add.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 9.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Chapter 6: Graphics Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Graphics.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Graphics in Java Applications The Graphics Class The Canvas Class The MouseListener.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
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.
COMP 110: Spring Announcements Quiz Wednesday No Class Friday Assignment 5 Due next Monday Q&A Review Session Monday.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 - Graphics and Java2D Outline 11.1Introduction 11.2Graphics Contexts and Graphics Objects 11.3Color.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Features of Java CS 3331 Sections and 5.5.
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,
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
Introductory Graphics Chapter Three. Computer Graphics §Full motion pictures (“Star Wars”) l animated films §Virtual reality §Games §Simulators (air craft.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
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)
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.
Lec 15 Writing an Applet Class. Agenda Writing an Applet class Java Graphics class.
Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Java Graphics and Java2D
User Interface Programming In Java
Chapter 8 Graphics.
Object Oriented Programming
Applets In Java Visit for more Learning Resources 1.
Graphics Applets By Mr. Dave Clausen
Chapter 10 Graphics.
Java Applets.
APPLET.
Printing in Java Printing in Java 1.1 is implemented through the new PrintJob class and PrintGraphics interface When a PrintJob object is created, the.
Chapter 8 Graphics.
Presentation transcript:

1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title); // set frame title. this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); this.setFont(new Font("SansSerif", Font.PLAIN, 12)); } public static void main(String[] args) { Frame f = new wpexample("AWT Demo"); f.pack(); f.show(); }

3 Applet example import java.applet.*; import java.awt.*; /** * This applet lets the user scribble with the mouse. It demonstrates * the Java 1.0 event model. **/ public class Scribble extends Applet { private int last_x = 0, last_y = 0; // Fields to store a point in. // Called when the user clicks. public boolean mouseDown(Event e, int x, int y) { last_x = x; last_y = y; // Remember the location of the click. return true; } // Called when the mouse moves with the button down public boolean mouseDrag(Event e, int x, int y) { Graphics g = getGraphics(); // Get a Graphics to draw with. g.drawLine(last_x, last_y, x, y); // Draw a line from last point to this. last_x = x; last_y = y; // And update the saved location. return true; }

5 Graphics import java.applet.*; import java.awt.*; /** * An applet that demonstrates most of the graphics primitives in java.awt.Graphics. **/ public class GraphicsSampler extends Applet { Image image; Image background; // Initialize the applet public void init() { this.setBackground(Color.lightGray); image = this.getImage(this.getDocumentBase(), "tiger.gif"); background = this.getImage(this.getDocumentBase(), "background.gif"); }

6 Graphics // Draw the applet whenever necessary public void paint(Graphics g) { Color fill = new Color(200, 200, 200); Color outline = Color.blue; Color textcolor = Color.red; Font font = new Font("sansserif", Font.BOLD, 14); g.setFont(font); // get a background image and tile it tile(g, this, background); // Draw a line g.setColor(outline); g.drawLine(25, 10, 150, 80); centerText("drawLine()", null, g, textcolor, 25, 10, 150, 80); // Draw an arc g.setColor(fill); g.fillArc(225, 10, 150, 80, 90, 135); g.setColor(outline); g.drawArc(225, 10, 150, 80, 90, 135); centerText("fillArc()", "drawArc()", g, textcolor, 225, 10, 150, 80);

7 Graphics // Draw a rectangle g.setColor(fill); g.fillRect(25, 110, 150, 80); g.setColor(outline); g.drawRect(25, 110, 150, 80); centerText("fillRect()", "drawRect()", g, textcolor, 25, 110, 150, 80); // Draw a rounded rectangle g.setColor(fill); g.fillRoundRect(225, 110, 150, 80, 20, 20); g.setColor(outline); g.drawRoundRect(225, 110, 150, 80, 20, 20); centerText("fillRoundRect()", "drawRoundRect()", g, textcolor, 225, 110, 150, 80); // Draw a 3D rectangle (clear an area for it first) g.setColor(fill); g.clearRect(20, 205, 160, 90); g.draw3DRect(25, 210, 150, 80, true); g.draw3DRect(26, 211, 148, 78, true); g.draw3DRect(27, 212, 146, 76, true); centerText("draw3DRect()", "x 3", g, textcolor, 25, 210, 150, 80);

8 Graphics // Draw an oval g.setColor(fill); g.fillOval(225, 210, 150, 80); g.setColor(outline); g.drawOval(225, 210, 150, 80); centerText("fillOval()", "drawOval()", g, textcolor, 225, 210, 150, 80); // Draw a polygon int numpoints = 9; int[] xpoints = new int[numpoints+1]; int[] ypoints = new int[numpoints+1]; for(int i=0; i < numpoints; i++) { double angle = 2*Math.PI * i / numpoints; xpoints[i] = (int)( *Math.cos(angle)); ypoints[i] = (int)( *Math.sin(angle)); } g.setColor(fill); g.fillPolygon(xpoints, ypoints, numpoints); g.setColor(outline); xpoints[numpoints] = xpoints[0]; ypoints[numpoints] = ypoints[0]; g.drawPolygon(xpoints, ypoints, numpoints+1); centerText("fillPolygon()", "drawPolygon()", g, textcolor, 25, 310, 150, 80);

9 Graphics // Center and draw an image int w = image.getWidth(this); int h = image.getHeight(this); g.drawImage(image, (150-w)/2, (80-h)/2, this); centerText("drawImage()", null, g, textcolor, 225, 310, 150, 80); } // Utility method to tile an image on the background protected void tile(Graphics g, Component c, Image i) { Rectangle r = c.getBounds(); // use c.bounds() in Java 1.0.x int iw = i.getWidth(c); int ih = i.getHeight(c); if ((iw <= 0) || (ih <= 0)) return; for(int x=0; x < r.width; x += iw) for(int y=0; y < r.height; y += ih) g.drawImage(i, x, y, c); }

10 Graphics // Utility method to center text in a rectangle protected void centerText(String s1, String s2, Graphics g, Color c, int x, int y, int w, int h) { Font f = g.getFont(); FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f); int ascent = fm.getAscent(); int height = fm.getHeight(); int width1=0, width2 = 0, x0=0, x1=0, y0=0, y1=0; width1 = fm.stringWidth(s1); if (s2 != null) width2 = fm.stringWidth(s2); x0 = x + (w - width1)/2; x1 = x + (w - width2)/2; if (s2 == null) y0 = y + (h - height)/2 + ascent; else { y0 = y + (h - (int)(height * 2.2))/2 + ascent; y1 = y0 + (int)(height * 1.2); } g.setColor(c); g.drawString(s1, x0, y0); if (s2 != null) g.drawString(s2, x1, y1); }