Applets and Graphics.

Slides:



Advertisements
Similar presentations
3 hr 5 hr 8 hr Hours worked Charge
Advertisements

Chapter 13 Graphics.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Custom Painting Gestione della Grafica customizzata Vedi anche:
1 Applets Programming Enabling Application Delivery Via the Web.
Ozone Level ppb (parts per billion)
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.
Copyright 2010 by Pearson Education Building Java Programs More Graphics reading: Supplement 3G.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
Lesson One: The Beginning
Life Cycle of an Applet, along with response to mouse events and other simple features. (ps:only two possible examples for life circle of an Applet)
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.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,
Polymorphism Method overriding Method overloading Dynamic binding 1.
Chapter 2: Using Objects Part 2. Assume you wish to test the behaviour of some method. This is accomplished by providing a tester class: Supply a main.
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original.
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
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?
Cosc 5/4730 Blackberry Drawing. Screen size With Blackberry devices, they have a known screen size in pixels. If you are programming for specific device.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
Draw Shapes Introduction to simple graphics. What is a graphics context? An instance of the Graphics class Graphics is ABSTRACT! You can extend Graphics.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
1 L37 Graphics and Java 2D™ (2). 2 OBJECTIVES To use methods of class Graphics to draw lines,  rectangles,  rectangles with rounded corners,  three-dimensional.
©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.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
©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.
Agenda For Feb Discussion (Assignments & Rubric) 2. PowerPoint Presentation (Drawing Basic Shapes). 4.Read Unit 3 carefully (pages ) then.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
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.
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Introductory Graphics Chapter Three. Computer Graphics §Full motion pictures (“Star Wars”) l animated films §Virtual reality §Games §Simulators (air craft.
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.
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Chapter 7 Introduction to High-Level Language Programming.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Graphics Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
Intro to Graphics from Chapter 2 of Java Software Solutions
12 Graphics and Java 2D™.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Graphics Applets By Mr. Dave Clausen
JAVA 2 Design and programming of GUI
Java Applets.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Presentation transcript:

Applets and Graphics

Introduction One of the most important features of Java is its ability to draw graphics. We can write Java applets that can draw lines, figures of different shapes, images, and text in different fonts, styles, and colours. Every applet has its own area on the screen known as canvas, where it creates display. Java coordinate system has the origin (0,0) in the upper-left corner. Positive x values are to the right and +ve y values to the bottom. The values of (x,y) are in pixels.

Graphics Class: Methods include drawArc() – draws a hollow arc drawLine() – draws a straight line g.drawLine(x1,y1,x2,y2) drawOval() - draws a hollow oval g.drawLine(x,y,width, height) If width and height are same, it draws a circle drawPolygon() - draws a hollow polygon drawRect() - draws a hollow rectangle g.drawLine(x,y,width,height) drawRoundRect() - draws a hollow round cornered rectangle drawString() – display a text string fillArc() - draw a filled arc fillOval() fillPolygon() fillRect() fillRoundRect() getColor – retrieve the current drawing colour getFont setColor – sets the drawing color setFont More at http://java.sun.com/products/jdk/1.2/docs/api/java/awt/Graphics.html

Drawing Lines and Rectagles import java.awt.*; import java.applet.*; public class LineRect extends Applet { public void paint(Graphics g) g.drawLine(10,10,50,50); g.drawRect(10,60,40,30); g.fillRect(60,10,30,80); g.drawRoundRect(10,100,80,50,10,10); g.fillRoundRect(20,110,60,30,5,5); g.drawLine(100,10,230,140); g.drawLine(100,140,230,10); }

Output of LineRect applet

Drawing arc. drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) Draws the outline of a circular or elliptical arc covering the specified rectangle. Java considers 3 O’clock as 0 degree position and degree increases in anti-clock wise direction. 90 180 180

Drawing a Happy Face Applet import java.awt.*; import java.applet.*; public class Face extends Applet { public void paint(Graphics g) g.drawOval(40,40,120,150); //Head g.drawOval(57,75,30,20); //Left eye g.drawOval(110,75,30,20); //Right eye g.fillOval(68,81,10,10); //Pupil (left) g.fillOval(121,81,10,10); //Pupil (right) g.drawOval(85,100,30,30); //Nose g.fillArc(60,125,80,40,180,180); //Mouth g.drawOval(25,92,15,30); //Left ear g.drawOval(160,92,15,30); //Right ear }

 Output!

Drawing Polygons Polygons are shapes with many sides. A polygons may be defined as a set of connected lines. The end of first line is the beginning of second line, and so on, Syntax: drawPolygon(int[] xPoints, int[] yPoints, int nPoints) Draws a closed polygon defined by arrays of x and y coordinates.

Polygon example code import java.awt.*; import java.applet.*; public class Poly extends Applet { int x1[]={20,120,220,20}; int y1[]={20,120,20,20}; int n1=4; int x2[]={120,220,220,120}; int y2[]={120,20,220,120}; int n2=4; public void paint(Graphics g) g.drawPolygon(x1,y1,n1); g.fillPolygon(x2,y2,n2); }

Polygon output

Drawing Bar Charts and Reading Parameters passed via HTML Applets can be designed to display bar charts, which are commonly used in comparative analysis of data. For example, the Table below shows annual turnover of a company during the period 2000-2003. These values can be passed via HTML file as PARAM attributes. Year 2000 2001 2002 2003 Turnover 110 150 100 170

Bar chart applet program… import java.awt.*; import java.applet.*; public class BarChart extends Applet { int n=0; String label[]; int value[]; public void init() try n=Integer.parseInt(getParameter("columns")); label=new String[n]; value=new int[n]; label[0]=getParameter("label1"); label[1]=getParameter("label2"); label[2]=getParameter("label3"); label[3]=getParameter("label4"); value[0]=Integer.parseInt(getParameter("c1")); value[1]=Integer.parseInt(getParameter("c2")); value[2]=Integer.parseInt(getParameter("c3")); value[3]=Integer.parseInt(getParameter("c4")); }catch(NumberFormatException e){} }

Bar chart applet program. public void paint(Graphics g) { for(int i=0;i<n;i++) g.setColor(Color.red); g.drawString(label[i],20,i*50+30); g.fillRect(50,i*50+10,value[i],40); }

HTML file – BarChar.html <APPLET CODE=BarChart.class WIDTH=300 HEIGHT=250> <PARAM NAME="columns" VALUE="4"> <PARAM NAME="c1" VALUE="110"> <PARAM NAME="c2" VALUE="150"> <PARAM NAME="c3" VALUE="100"> <PARAM NAME="c4" VALUE="170"> <PARAM NAME="label1" VALUE=“2000"> <PARAM NAME="label2" VALUE=" 2001"> <PARAM NAME="label3" VALUE=" 2002"> <PARAM NAME="label4" VALUE=" 2003"> </APPLET> </HTML>

Output

Summary Java’s Graphics class supports many methods that enable us to draw many types of shapes. These methods can be used put together visual display and graphical illustrations. Java provide many more capabilities (such as Swings), we are not able cover all of them. For more info, please refer to Sun Java 2 document. Future lectures will mostly focus on UML!