Interactive Programs and Graphics in Java



Similar documents
Basics of Java Programming Input and the Scanner class

Introduction to Java Applets (Deitel chapter 3)

Lab 5: Bank Account. Defining objects & classes

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

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.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

6.1. Example: A Tip Calculator 6-1

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Java applets. SwIG Jing He

Comp 248 Introduction to Programming

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Topic 11 Scanner object, conditional execution

Introduction to Java. CS 3: Computer Programming in Java

Building Java Programs

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Building Java Programs

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Chapter 2: Elements of Java

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

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

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

CS 106 Introduction to Computer Science I

Introduction to Java

Chapter 3. Input and output. 3.1 The System class

Using Files as Input/Output in Java 5.0 Applications

Line-based file processing

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Some Scanner Class Methods

An Overview of Java. overview-1

Install Java Development Kit (JDK) 1.8

Course Intro Instructor Intro Java Intro, Continued

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Lecture 5: Java Fundamentals III

Chapter 1 Java Program Design and Development

First Bytes Programming Lab 2

Building Java Programs

Introduction to Programming

Event-Driven Programming

AP Computer Science Static Methods, Strings, User Input

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

Programming Fundamentals I CS 110, Central Washington University. November 2015

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

CIS 467/602-01: Data Visualization

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

Sample CSE8A midterm Multiple Choice (circle one)

The Basic Java Applet and JApplet

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

Android Programming: 2D Drawing Part 1: Using ondraw

Lesson 10: Video-Out Interface

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Part 1 Foundations of object orientation

CS 121 Intro to Programming:Java - Lecture 11 Announcements

13 File Output and Input

CSC 551: Web Programming. Spring 2004

CS170 Lab 11 Abstract Data Types & Objects

JAVA.UTIL.SCANNER CLASS

Object-Oriented Programming in Java

Data Structures Lecture 1

Chapter 2 Introduction to Java programming

Chapter 1: Computer Systems

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

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

Homework/Program #5 Solutions

java Features Version April 19, 2013 by Thorsten Kracht

CS 106 Introduction to Computer Science I

Sample Turtle Programs

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

Reading Input From A File

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Hypercosm. Studio.

An evaluation of JavaFX as 2D game creation tool

Chapter 2 Elementary Programming

Fireworks CS4 Tutorial Part 1: Intro

12Planet Chat end-user manual

Creating a Resume Webpage with

WebSphere Business Monitor V6.2 KPI history and prediction lab

(Eng. Hayam Reda Seireg) Sheet Java

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

ICT 6012: Web Programming

CS506 Web Design and Development Solved Online Quiz No. 01

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

Mail Programming Topics

Graphics Module Reference

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

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Interactive Applications (CLI) and Math

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

ECE 122. Engineering Problem Solving with Java

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

JAVA DEVELOPER S GUIDE TO ASPRISE SCANNING & IMAGE CAPTURE SDK

Lecture 1 Introduction to Android

Transcription:

Interactive Programs and Graphics in Java Alark Joshi Slide credits: Sami Rollins

Announcements Lab 1 is due today Questions/concerns?

SVN - Subversion Versioning and revision control system 1. allows you to maintain a copy of your code on a server 2. keeps track of all previous versions 3. allows multiple developers to work on the same code though, we won't use that this semester I can retrieve the code that was submitted before the deadline rather than the latest code Popular options for version control are CVS, SVN, Git

Submitting via SVN A hard drive crash the hour before a deadline is NOT an excuse for not submitting anything Submit a working version of your code every time you make changes Accessing SVN via Eclipse http://tutoringcenter.cs.usfca.edu/resources/us ing-svn-via-eclipse-and-subclipse.html

Interactive Programs Programs generally need input on which to operate The Scanner class provides convenient methods for reading input values of various types A Scanner object can be set up to read input from various sources, including the user typing values on the keyboard Keyboard input is represented by the System.in object Copyright 2012 Pearson Education, Inc.

Reading Input The following line creates a Scanner object that reads from the keyboard: Scanner scan = new Scanner (System.in); The new operator creates the Scanner object Once created, the Scanner object can be used to invoke various input methods, such as: answer = scan.nextline(); Copyright 2012 Pearson Education, Inc.

Reading Input The Scanner class is part of the java.util class library, and must be imported into a program to be used The nextline method reads all of the input until the end of the line is found See Echo.java The details of object creation and class libraries are discussed further in Chapter 3 Copyright 2012 Pearson Education, Inc.

//******************************************************************** // Echo.java Author: Lewis/Loftus // // Demonstrates the use of the nextline method of the Scanner class // to read a string from the user. //******************************************************************** import java.util.scanner; public class Echo { //----------------------------------------------------------------- // Reads a character string from the user and prints it. //----------------------------------------------------------------- public static void main (String[] args) { String message; Scanner scan = new Scanner (System.in); System.out.println ("Enter a line of text:"); message = scan.nextline(); } } System.out.println ("You entered: \"" + message + "\""); Copyright 2012 Pearson Education, Inc.

Sample Run //******************************************************************** // Echo.java Author: Lewis/Loftus // Enter a line of text: // Demonstrates You want the fries use of the with nextline that? method of the Scanner class // to read a string from the user. You entered: "You want fries with that?" //******************************************************************** import java.util.scanner; public class Echo { //----------------------------------------------------------------- // Reads a character string from the user and prints it. //----------------------------------------------------------------- public static void main (String[] args) { String message; Scanner scan = new Scanner (System.in); System.out.println ("Enter a line of text:"); message = scan.nextline(); } } System.out.println ("You entered: \"" + message + "\""); Copyright 2012 Pearson Education, Inc.

Input Tokens Unless specified otherwise, white space is used to separate the elements (called tokens) of the input White space includes space characters, tabs, new line characters Copyright 2012 Pearson Education, Inc.

Input Tokens The next method of the Scanner class reads the next input token and returns it as a string Methods such as nextint and nextdouble read data of particular types See GasMileage.java Copyright 2012 Pearson Education, Inc.

//******************************************************************** // GasMileage.java Author: Lewis/Loftus // // Demonstrates the use of the Scanner class to read numeric data. //******************************************************************** import java.util.scanner; public class GasMileage { //----------------------------------------------------------------- // Calculates fuel efficiency based on values entered by the // user. //----------------------------------------------------------------- public static void main (String[] args) { int miles; double gallons, mpg; continue Scanner scan = new Scanner (System.in); Copyright 2012 Pearson Education, Inc.

continue System.out.print ("Enter the number of miles: "); miles = scan.nextint(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextdouble(); mpg = miles / gallons; } } System.out.println ("Miles Per Gallon: " + mpg); Copyright 2012 Pearson Education, Inc.

Sample Run Enter the number of miles: 328 Enter the gallons of fuel used: 11.2 Miles Per Gallon: 29.28571428571429 continue System.out.print ("Enter the number of miles: "); miles = scan.nextint(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextdouble(); mpg = miles / gallons; } } System.out.println ("Miles Per Gallon: " + mpg); Copyright 2012 Pearson Education, Inc.

Miles to Kilometers conversion Write a program to take input from the user in miles and display the corresponding kilometers 1 mile = 1.60934 kilometers

Introduction to Graphics A picture or drawing must be digitized for storage on a computer A picture is made up of pixels (picture elements), and each pixel is stored separately The number of pixels used to represent a picture is called the picture resolution The number of pixels that can be displayed by a monitor is called the monitor resolution Copyright 2012 Pearson Education, Inc.

Representing Images A digitized picture with a small portion magnified: Copyright 2012 Pearson Education, Inc.

Coordinate Systems Each pixel can be identified using a twodimensional coordinate system When referring to a pixel in a Java program, we use a coordinate system with the origin in the top-left corner (0, 0) 112 X 40 (112, 40) Y Copyright 2012 Pearson Education, Inc.

Representing Color A black and white picture could be stored using one bit per pixel (0 = white and 1 = black) A colored picture requires more information; there are several techniques for representing colors Every color can be represented as a mixture of the three additive primary colors Red, Green, and Blue Each color is represented by three numbers between 0 and 255 that collectively are called an RGB value Copyright 2012 Pearson Education, Inc.

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 Copyright 2012 Pearson Education, Inc.

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 Copyright 2012 Pearson Education, Inc.

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 Copyright 2012 Pearson Education, Inc.

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 drawrect() drawoval() drawline()

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 Copyright 2012 Pearson Education, Inc.

//******************************************************************** // 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); Copyright 2012 Pearson Education, Inc.

//******************************************************************** // 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); Copyright 2012 Pearson Education, Inc.

The HTML applet Tag An applet is embedded into an HTML file (applet 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> Copyright 2012 Pearson Education, Inc.

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 Copyright 2012 Pearson Education, Inc.

Drawing a Line 10 150 X 20 45 Y page.drawline (10, 20, 150, 45); or page.drawline (150, 45, 10, 20); Copyright 2012 Pearson Education, Inc.

Drawing a Rectangle 50 X 20 40 100 Y page.drawrect (50, 20, 100, 40); Copyright 2012 Pearson Education, Inc.

Drawing an Oval 175 X 20 80 Y bounding rectangle 50 page.drawoval (175, 20, 50, 80); Copyright 2012 Pearson Education, Inc.

Drawing an Arc An arc is defined by an oval, a start angle, and an arc angle: Copyright 2012 Pearson Education, Inc.

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 Copyright 2012 Pearson Education, Inc.

//******************************************************************** // 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 Copyright 2012 Pearson Education, Inc. 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 Copyright 2012 Pearson Education, Inc.

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 Copyright 2012 Pearson Education, Inc.