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/

Slides:



Advertisements
Similar presentations
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.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Outline Graphics Applets Drawing Shapes Components and Containers Images.
© 2004 Pearson Addison-Wesley. All rights reserved2-1 Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics.
2 Data and Expressions Software Solutions Lewis & Loftus java
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
©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.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday.
Chapter Day 4. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 4 Return Signed Contracts Questions from last Class?? Problem set 1 Posted.
©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.
A Simple Applet.
CSC Computing with Images1 Image encodings CSC 1040.
Chapter 5 Graphics. Topics Applets Classes used for graphics –Graphics –Point –Dimension –Color.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
NSF North Mississippi GK-8 Program ‘Til You Drop Brenteria Travis NSF North Mississippi GK-8.
©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.
© 2004 Pearson Addison-Wesley. All rights reserved Lecture 2  Chapter 2 CS0401: Intermediate Java Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 M.A. Papalaskari, Villanova University Data Representation and Applets CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java,
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
CSC Computing with Images
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
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.
1 Objects and Primitive Data  Now we can explore some more fundamental programming concepts  Chapter 2 focuses on: predefined objects primitive data.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Chapter 2 Data and Expressions Part Two. 2-2/30 Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Graphics & Applets CSC 1051 – Data Structures and Algorithms I
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Chapter 2 Data and Expressions. Chapter 2 2 Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: character.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 2 Data and Expressions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Intro to Graphics from Chapter 2 of Java Software Solutions
Chapter 02 Data and Expressions.
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
title = "Java Software Solutions";
Chapter 2 Data and Expressions.
Chapter 2 Data and Expressions
Graphics -- Introduction
Data and Expressions Chapter 2.
Chapter 2 Data and Expressions.
Chapter 2 Data and Expressions
Chapter 2 Data and Expressions
Data and Expressions Part Two
Outline Character Strings Variables and Assignment
Drawing Shapes (Graphics Class) Creating Objects Packages
Presentation transcript:

CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: CSC 1051 M.A. Papalaskari, Villanova University Graphics & Applets

Back to Chapter 2! Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes CSC 1051 M.A. Papalaskari, Villanova University

Today Image representation Java Graphics class Java applets CSC 1051 M.A. Papalaskari, Villanova University

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

CSC 1051 M.A. Papalaskari, Villanova University Some Pixel encodings Bitmap 1 bit Grayscale 8 bits RGB Color 3 colors: red, green, blue 8 bits/color 24 bits

CSC 1051 M.A. Papalaskari, Villanova University 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 KB

CSC 1051 M.A. Papalaskari, Villanova University 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

CSC 1051 M.A. Papalaskari, Villanova University Additive/Subtractive Color Additive primaries - combining light Red Green Blue We choose 3 primary colors that can be combined to produce all the visible colors: Animation: Subtractive primaries - combining ink, thus subtracting light Cyan Yellow Magenta

CSC 1051 M.A. Papalaskari, Villanova University 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

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

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

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, , 200, 0 255, 255, , 255, 0 CSC 1051 M.A. Papalaskari, Villanova University

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University

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.javaSee Einstein.java CSC 1051 M.A. Papalaskari, Villanova University

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

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // 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); }

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // 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 CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University

Drawing a Line X Y page.drawLine (10, 20, 150, 45); page.drawLine (150, 45, 10, 20); or CSC 1051 M.A. Papalaskari, Villanova University Start x y Start x y End x y End x y

Drawing a Rectangle X Y page.drawRect (50, 20, 100, 40); CSC 1051 M.A. Papalaskari, Villanova University Start x y Start x y Width Height

Drawing an Oval X Y page.drawOval (175, 20, 50, 80); bounding rectangle CSC 1051 M.A. Papalaskari, Villanova University

Drawing an Arc An arc is defined by an oval, a start angle, and an arc angle: CSC 1051 M.A. Papalaskari, Villanova University

Drawing Shapes Every drawing surface has a background color Every graphics context has a current foreground color Both can be set explicitly See Snowman.javaSee Snowman.java See also Snowman applet on a webpage CSC 1051 M.A. Papalaskari, Villanova University

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

CSC 1051 M.A. Papalaskari, Villanova University 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 }

CSC 1051 M.A. Papalaskari, Villanova University 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 }

The HTML applet Tag The Einstein Applet CSC 1051 M.A. Papalaskari, Villanova University 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.

Homework Read Sections –Always do all self-review exercises when you review material Do Exercises EX CSC 1051 M.A. Papalaskari, Villanova University 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.