CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Similar documents
Applets and the Graphics class

Data Representation and Applets

Data Representation and Applets

Data Representation and Applets

Data Representation and Applets

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY


Graphical User Interfaces

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Appendix F: Java Graphics

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Appendix F: Java Graphics

Designing Classes Part 2

Graphical User Interfaces

CS1004: Intro to CS in Java, Spring 2005

Graphical User Interfaces

Preparation: Get to know the Java coordinate system. 3. How is the background color of the window specified? Copy the exact code:

Chapter 2 Exercise Solutions

Graphics Applets. By Mr. Dave Clausen

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

Graphics Applets. By Mr. Dave Clausen

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

@Override public void start(stage primarystage) throws Exception { Group root = new Group(); Scene scene = new Scene(root);

CSC System Development with Java Introduction to Java Applets Budditha Hettige

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

CSC 1051 Data Structures and Algorithms I

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

Objects and Primitive Data

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

CSC 1051 Data Structures and Algorithms I

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Introduction to Computer Science I

CSC 1051 Data Structures and Algorithms I

255, 255, 0 0, 255, 255 XHTML:

Using Classes and Objects

Building Java Programs

Lab 3: Work with data (IV)

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

TWO-DIMENSIONAL FIGURES

Chapter 3 - Introduction to Java Applets

Building Java Programs

Building Java Programs

Garfield AP CS. Graphics

CISC 1600 Lecture 3.1 Introduction to Processing

Using Graphics. Building Java Programs Supplement 3G

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Selection Statements and operators

Advanced Internet Programming CSY3020

Lecture Static Methods and Variables. Static Methods

Individual Readiness Assessment Test (irat) 11 questions ~ 11 minutes. Group Readiness Assessment Test (grat)

Selection Statements and operators

Arrays, Part 3: Multidimensional arrays

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

(0,0) (600, 400) CS109. PictureBox and Timer Controls

Higher National Diploma in Information Technology First Year, Second Semester Examination 2015

Introduction to Arrays

Using Classes and Objects

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Topic 8 graphics. -mgrimes, Graphics problem report 134

Lecture 3: Variables and assignment

Java TM Applets. Rex Jaeschke

An Introduction to Processing

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

Graphical User Interfaces

Topic 8 Graphics. Margaret Hamilton

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

Building Java Programs

Chapter 12 Advanced GUIs and Graphics

Exercise NMCGJ: Animated Graphics

Software Concepts. 2-8 Explain the process of translating and executing a Java program. How might the World Wide Web be involved?

Summary. 962 Chapter 23 Applets and Java Web Start

8/23/2011. Chapter Goals

2/9/2012. Chapter Two: Using Objects. Chapter Goals

Using Classes and Objects

Java Programming Lecture 6

Iterators and File Input

Chapter 7 Exercise Solutions

Chapter 2 Using Objects

Java Applet Basics. Life cycle of an applet:

2: Basics of Java Programming

Selection and Repetition Revisited

Expressions, Statements, and Control Structures

Lecture Static Methods and Variables. Static Methods

AP CS Unit 12: Drawing and Mouse Events

EXAMINATIONS 2016 TRIMESTER 2

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

Selection and Repetition Revisited

PROGRAMMING LANGUAGE 2

Paint/Draw Tools. Foreground color. Free-form select. Select. Eraser/Color Eraser. Fill Color. Color Picker. Magnify. Pencil. Brush.

Chapter 2: Data and Expressions. Prelab Exercises. Print and println. Area and Circumference of a Circle. Integers and Floating point

Here is a list of a few of the components located in the AWT and Swing packages:

CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random. Adapted from slides by Marty Stepp and Stuart Reges

Transcription:

Graphics & Applets CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

Back to Chapter 2! Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Today Image representation Java Graphics class Java applets

What s a picture? programs represent pictures as grids of picture elements or pixels Stephanos with his eraser collection

Some Pixel encodings Bitmap 1 bit Grayscale 8 bits RGB Color 3 colors: red, green, blue 8 bits/color 24 bits

Comparing file sizes for 300 x 200 image: Bitmap 1 bit Grayscale 8 bits (1 byte) RGB Color 3 colors: red, green, blue 24 bits (3 bytes) 7.3 KB 58.6 KB 175.8 KB

Is that enough? We re representing color in 24 (3 * 8) bits. That s 16,777,216 (2 24 ) possible colors Our eye can discern millions of colors - so probably pretty close

Additive/Subtractive Color We choose 3 primary colors that can be combined to produce all the visible colors: Additive primaries - combining light Red Green Blue Animation: http://dx.sheridan.com/advisor/cmyk_color.html Subtractive primaries - combining ink, thus subtracting light Cyan Yellow Magenta

Encoding RGB Each component color (red, green, and blue) is encoded as a single byte Colors go from (0,0,0) to (255,255,255) If all three components are the same, the color is in greyscale (50,50,50) at (2,2) (0,0,0) (at position (1,2) in example) is black (255,255,255) is white

y = 9 Position: (12,9) x = 12 CSC 1051 M.A. Papalaskari, Villanova University

y = 9 red=108 green=86 blue= Color:(108,86,142) Position: (12,9) x = 12 CSC 1051 M.A. Papalaskari, Villanova University

The Color Class A color in a Java program is represented as an object created from the Color class The Color class also contains several predefined colors, including the following: Object Color.black Color.blue Color.cyan Color.orange Color.white Color.yellow RGB Value 0, 0, 0 0, 0, 255 0, 255, 255 255, 200, 0 255, 255, 255 255, 255, 0

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

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);

//******************************************************************** // 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);

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

Drawing Shapes Shapes can be filled or unfilled, depending on which method is invoked The method parameters specify coordinates and sizes For curves and ovals we specify the shape s bounding rectangle

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

Drawing a Rectangle 50 X 20 40 100 Y Start x y Width Height 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:

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 See also Snowman applet on a webpage

//******************************************************************** // 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); page.filloval (MID+5, TOP+10, 5, 5); // left eye // 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); page.filloval (MID+5, TOP+10, 5, 5); // left eye // 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

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>

Homework Read Sections 2.7-2.9 Always do all self-review exercises when you review material Do Exercises EX 2.13-2.18 Many of the slides in this presentation are adapted from the slides accompanying Java Software solutions, 7 th edition by Lewis and Loftus. Copyright 2012 Pearson Education, Inc.