Drawing Shapes and Graphics. Copyright 2012 Pearson Education, Inc.

Similar documents
Interactive Programs and Graphics in Java

Introduction to Java Applets (Deitel chapter 3)

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

Java applets. SwIG Jing He

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Lab 7 Keyboard Event Handling Mouse Event Handling

How To Write A Program For The Web In Java (Java)

Sample Turtle Programs

CSC 551: Web Programming. Spring 2004

The Basic Java Applet and JApplet

Fachbereich Informatik und Elektrotechnik Java Applets. Programming in Java. Java Applets. Programming in Java, Helmut Dispert

Chapter 1 Java Program Design and Development

WEEK 2 DAY 14. Writing Java Applets and Java Web Start Applications

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Mail Programming Topics

STATGRAPHICS Online. Statistical Analysis and Data Visualization System. Revised 6/21/2012. Copyright 2012 by StatPoint Technologies, Inc.

There are some important differences between an applet and a standalone Java application, including the following:

An Overview of Java. overview-1

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Creating a 2D Geometry Model

Graphics Module Reference

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

Chapter 1: Computer Systems

Part 1 Foundations of object orientation

Where On Earth Will Three Different Satellites Provide Simultaneous Coverage?

Java History. Java History (cont'd)

Fireworks CS4 Tutorial Part 1: Intro

Creating a Logo in CorelDRAW

CS 209 Programming in Java #1

How to Convert an Application into an Applet.

Java SE 6 Update 10. la piattaforma Java per le RIA. Corrado De Bari. Sun Microsystems Italia Spa. Software & Java Ambassador

Contents. Java - An Introduction. Java Milestones. Java and its Evolution

Image Processing. In this chapter: ImageObserver ColorModel ImageProducer ImageConsumer ImageFilter

What s New V 11. Preferences: Parameters: Layout/ Modifications: Reverse mouse scroll wheel zoom direction

Java vs. Java Script

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

Android Programming: 2D Drawing Part 1: Using ondraw

Understand the Sketcher workbench of CATIA V5.

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

Hypercosm. Studio.

Lecture 1 Introduction to Android

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

SE 450 Object-Oriented Software Development. Requirements. Topics. Textbooks. Prerequisite: CSC 416

Essentials of the Java(TM) Programming Language, Part 1

How to build text and objects in the Titler

Basic Math for the Small Public Water Systems Operator

Worksheets for Teachers. The Lowry

The Web Web page Links 16-3

Inductive Reasoning Free Sample Test 1

16 Circles and Cylinders

Airangel s WiFi Portal Best Practice Guidelines Get the most out of your WiFi infrastructure

Two-Dimensional Arrays Java Programming 2 Lesson 12

CS506 Web Design and Development Solved Online Quiz No. 01

How to create buttons and navigation bars

An evaluation of JavaFX as 2D game creation tool

Working With Animation: Introduction to Flash

Illustration 1: An applet showing constructed responses in an intuitive mode. Note the misprint!

Using Microsoft Word. Working With Objects

Applets, RMI, JDBC Exam Review

Load testing with. WAPT Cloud. Quick Start Guide

User s Guide to the PGF Package, Version

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

BUSINESS SOLUTIONS ASSOCIATION. Banner Ad Guidelines BSA Guideline 08-13

11. Applets, normal window applications, packaging and sharing your work

Table of Contents. I. Banner Design Studio Overview II. Banner Creation Methods III. User Interface... 8

Instant Interactive SAS Log Window Analyzer

SAS/GRAPH 9.2 ODS Graphics Editor. User s Guide

1. Overview of the Java Language

Crop and Frame Your Photos

Adobe InDesign Creative Cloud

Running a Program on an AVD

Periodontology. Digital Art Guidelines JOURNAL OF. Monochrome Combination Halftones (grayscale or color images with text and/or line art)

ClarisWorks 5.0. Graphics

Activation of your SeKA account

understand how image maps can enhance a design and make a site more interactive know how to create an image map easily with Dreamweaver

Smart Board Notebook Software A guide for new Smart Board users

Google Drive Create, Share and Edit Documents Online

Native code in Android Quad Detection

Epson Brightlink Interactive Board and Pen Training. Step One: Install the Brightlink Easy Interactive Driver

C# and Other Languages

Session 12 Evolving IT Architectures: From Mainframes to Client-Server to Network Computing

OCS Training Workshop LAB13. Ethernet FTP and HTTP servers

If you see "Skip installation of the current version and test the currently installed version of Java" then select that hyperlink.

Lions Clubs International e-district House Content Management System (CMS) Training Guide

Dŵr y Felin Comprehensive School. Perimeter, Area and Volume Methodology Booklet

ADMINISTRATORS GUIDE EPISUITE 6

ANDROID TRAINING COURSE MODULES. Module-I: Introduction to Android. Introducing Android. Installing Development Tools. Using the Emulator.

Lesson 10: Video-Out Interface

Transcription:

Drawing Shapes and Graphics

Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) A Java applet is a program that is intended to be transported over the Web and executed using a web browser An applet also can be executed using the appletviewer tool of the Java SDK An applet doesn't have a main method Instead, there are several special methods that serve specific purposes

Applets The paint method is executed automatically whenever the applet s contents are drawn The paint method accepts a parameter that is an object of the Graphics class A Graphics object defines a graphics context on which we can draw shapes and text The Graphics class has several methods for drawing shapes

Applets We create an applet by extending the JApplet class The JApplet class is part of the javax.swing package This makes use of inheritance, which is explored in more detail in Chapter 8 See Einstein.java

//******************************************************************** // Einstein.java Author: Lewis/Loftus // // Demonstrates a basic applet. //******************************************************************** import javax.swing.japplet; import java.awt.*; public class Einstein extends JApplet { //----------------------------------------------------------------- // Draws a quotation by Albert Einstein among some shapes. //----------------------------------------------------------------- public void paint (Graphics page) { page.drawrect (50, 50, 40, 40); // square page.drawrect (60, 80, 225, 30); // rectangle page.drawoval (75, 65, 20, 20); // circle page.drawline (35, 60, 100, 120); // line } } page.drawstring ("Out of clutter, find simplicity.", 110, 70); page.drawstring ("-- Albert Einstein", 130, 100);

//******************************************************************** // Einstein.java Author: Lewis/Loftus // // Demonstrates a basic applet. //******************************************************************** import javax.swing.japplet; import java.awt.*; public class Einstein extends JApplet { //----------------------------------------------------------------- // Draws a quotation by Albert Einstein among some shapes. //----------------------------------------------------------------- public void paint (Graphics page) { page.drawrect (50, 50, 40, 40); // square page.drawrect (60, 80, 225, 30); // rectangle page.drawoval (75, 65, 20, 20); // circle page.drawline (35, 60, 100, 120); // line } } page.drawstring ("Out of clutter, find simplicity.", 110, 70); page.drawstring ("-- Albert Einstein", 130, 100);

The HTML applet Tag An applet is embedded into an HTML file using a tag that references the bytecode file of the applet The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser <html> <head> <title>the Einstein Applet</title> </head> <body> <applet code="einstein.class" width=350 height=175> </applet> </body> </html>

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Drawing Shapes Let's explore some of the methods of the Graphics class that draw shapes in more detail A shape can be filled or unfilled, depending on which method is invoked The method parameters specify coordinates and sizes Shapes with curves, like an oval, are usually drawn by specifying the shape s bounding rectangle An arc can be thought of as a section of an oval

Drawing a Line 10 150 X 20 45 Y page.drawline (10, 20, 150, 45); or page.drawline (150, 45, 10, 20);

Drawing a Rectangle 50 X 20 40 100 Y page.drawrect (50, 20, 100, 40);

Drawing an Oval 175 X 20 80 Y bounding rectangle 50 page.drawoval (175, 20, 50, 80);

Drawing an Arc An arc is defined by an oval, a start angle, and an arc angle:

DO NOW LOGIN TO YOUR COMPUTER. As it is booting up think about the following What is the most important thing to remember about placing shapes in an Applet? Open Eclipse and create a class called Snowman, if we did not do that last week. Be prepared to share. WE WILL HAVE YOUR TEST STARTING ON FRIDAY!

Drawing Shapes Every drawing surface has a background color Every graphics context has a current foreground color Both can be set explicitly See Snowman.java

The Color Class A color is defined in a Java program using an object created from the Color class Object The Color class also contains several static predefined colors, including: RGB Value Object RGB Value Color.black Color.blue Color.cyan Color.orange Color.white Color.yellow 0, 0, 0 0, 0, 255 0, 255, 255 255, 200, 0 255, 255, 255 255, 255, 0 Color.darkGray 64, 64, 64 Color.lightGray 192, 192, 192 Color.gray 128,128,128 Color.green 0, 255, 0 Color.magenta 255, 0, 255 Color.pink 255, 175, 175 Color.red 255, 0, 0 16

//******************************************************************** // Snowman.java Author: Lewis/Loftus // // Demonstrates basic drawing methods and the use of color. //******************************************************************** import javax.swing.japplet; import java.awt.*; public class Snowman extends JApplet { //----------------------------------------------------------------- // Draws a snowman. //----------------------------------------------------------------- public void paint (Graphics page) { final int MID = 150; final int TOP = 50; continued setbackground (Color.cyan); page.setcolor (Color.blue); page.fillrect (0, 175, 300, 50); // ground page.setcolor (Color.yellow); page.filloval (-40, -40, 80, 80); // sun

continued page.setcolor (Color.white); page.filloval (MID-20, TOP, 40, 40); // head page.filloval (MID-35, TOP+35, 70, 50); // upper torso page.filloval (MID-50, TOP+80, 100, 60); // lower torso page.setcolor (Color.black); page.filloval (MID-10, TOP+10, 5, 5); // left eye page.filloval (MID+5, TOP+10, 5, 5); // right eye page.drawarc (MID-10, TOP+20, 20, 10, 190, 160); // smile page.drawline (MID-25, TOP+60, MID-50, TOP+40); // left arm page.drawline (MID+25, TOP+60, MID+55, TOP+60); // right arm } } page.drawline (MID-20, TOP+5, MID+20, TOP+5); // brim of hat page.fillrect (MID-15, TOP-20, 30, 25); // top of hat

continued page.setcolor (Color.white); page.filloval (MID-20, TOP, 40, 40); // head page.filloval (MID-35, TOP+35, 70, 50); // upper torso page.filloval (MID-50, TOP+80, 100, 60); // lower torso page.setcolor (Color.black); page.filloval (MID-10, TOP+10, 5, 5); // left eye page.filloval (MID+5, TOP+10, 5, 5); // right eye page.drawarc (MID-10, TOP+20, 20, 10, 190, 160); // smile page.drawline (MID-25, TOP+60, MID-50, TOP+40); // left arm page.drawline (MID+25, TOP+60, MID+55, TOP+60); // right arm } } page.drawline (MID-20, TOP+5, MID+20, TOP+5); // brim of hat page.fillrect (MID-15, TOP-20, 30, 25); // top of hat

Practice Create a revised version of the Snowman applet with the following modifications: Add two red buttons to the upper torso Make the snowman frown instead of smile Move the sun to the upper-right corner of the picture Display your name in the upper-left corner of the picture Shift the entire snowman 20 pixels to the right. Inert it into the Snowman HTML webpage

Independent Practice Write and applet that draws a house with a door (and doorknob), windows, and a chimney. Add some smoke coming out of the chimney and some clouds in the sky.