Monday 26th May Java Lecture 10 Overview

Slides:



Advertisements
Similar presentations
Topic 9 more graphics Based on slides bu Marty Stepp and Stuart Reges from
Advertisements

Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
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.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
A Simple Applet.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
Programming Task: Task 1 Controlled Assessment Practice.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
© 2007 Lawrenceville Press Slide 1 Chapter 2 HTML An example HTML document Hello world!
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
JAPPLET.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
CPSC1301 Computer Science 1 Chapter 14 Creating and Modifying Movies part 2.
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.
PHY281 Scientific Java Programming LoopsSlide 1 Loops In this section we will learn how to repeat a series of instructions using loops and to use this.
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Drawing rectangles and ovals in the Applet window Displaying Text in the Java Console Window Demo of the HelloAgain program Arithmetic expressions Examples.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
SC Introduction to Programming Lecture 1: 5 th May 2003.
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
Building Java Programs Graphics Reading: Supplement 3G.
IT11 Agenda for Feb Golden Rule Reminder. 2. PowerPoint demonstration on the structure of Java Applet programs. Source Files HTML Files Class Files.
Lec 15 Writing an Applet Class. Agenda Writing an Applet class Java Graphics class.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
User Interface Programming In Java
Decisions - if and switch
Object Oriented Programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Example: Card Game Create a class called “Card”
Today’s topics Java Applications Upcoming Reading Graphics
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Building Java Programs
Introduction to Applet, Application and JDK
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Building Java Programs
The first number is posted telling what random number was selected, I did this for testing purposes, in the real thing it would not be there. Since the.
Applet in Java.
Building Java Programs
Building Java Programs
Building Java Programs
Topic 9 More Graphics Based on slides bu Marty Stepp and Stuart Reges from
Handout-14 Applets and Graphics
Building Java Programs
Building Java Programs
PowerPoint Presentation Authors of Exposure Java
Building Java Programs
Making a Smile Face Pepper.
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

415.111 Monday 26th May Java Lecture 10 Overview Interactive Graphics Examples Demos Explorer’s Guide handout: Chapter 8 to end.

Bentley’s Programming Pearls If code and comments disagree, then both are probably wrong.

Bentley’s Programming Pearls Testing can show the presence of bugs, but cannot prove their absence.

Making graphics programs more interactive We can now extend our graphics programs by making them more interactive. We can get the x and y coordinates of the point in the Applet Window where the mouse was clicked by using: Mouse.getX() Mouse.getY().

Loops and Mouse Techniques p 177 import java.awt.*; import java.applet.*; import java.awt.event.*; public class DisplayClickPoints extends Applet { public void init() { new Mouse(this); } public void paint(Graphics g) { // * * * * * * * * * * * * * * * * * * * * * * * * * *

// Draw the coordinates of the mouse click at the click point int h, v; h = Mouse.getX(); v = Mouse.getY(); g.drawString("(" + h + ", " + v + ")", h, v); // * * * * * * * * * * * * * * * * * * * * * * * * * *

// Draw smiley faces at up to 10 click points g.drawString( "Smiley", 30, 30 ); int x,y; int counter; counter = 0; do { x = Mouse.getX(); y = Mouse.getY(); // Draw a Smiley centred at the point (x, y) g.drawOval( x-20, y-20, 40, 40 ); g.drawOval( x - 10, y, 2, 2 ); g.drawOval( x+ 10, y, 2, 2 ); g.drawOval( x, y + 10, 4, 3 ); counter = counter + 1; } while (counter != 10);

Introducing Buttons p Introducing Buttons p 178 int top, left, width, height; int x, y; // initialize left = 20; top = 20; width = 40; height = 20; // Draw a Rectangle and put HI in it g.drawRect(left, top, width, height); g.drawString( "HI", left + 5, top + height - 5 );

Introducing Buttons p Buttons contd // Get the mouse coordinates x = Mouse.getX(); y = Mouse.getY(); // if the mouse is inside the rectangle display BYE if (x > left && x < (left + width) && y > top && y < (top + height)) { g.clearRect(left, top, width, height); g.drawRect(left, top, width, height); g.drawString( "BYE", left + 5, top + height - 5 ); }

Exercise 1 p179 Write a program that displays 3 buttons as shown in the Applet Window below: When the user clicks in one of the buttons a price is displayed. (It is your job to think of appropriate prices in this age of inflation!)

Introducing Buttons p GuessIt3 p180 // program to play a simple guessing game int x, y, randomNbr, userNbr; g.drawString( "Guess it", 30, 30 ); int left1,width, height, top, counter, left; left1 = 30; width = 40; height = 20; top = 40;

GuessIt3 contd Introducing Buttons p counter = 1; left = left1; // Display the buttons (1, 2, and 3) do { g.setColor(Color.blue); g.fillRect(left, top, width, height); g.setColor(Color.black); g.drawString("" + counter, left + width / 2, top + height / 2); left = left + 2 * width; counter = counter + 1; } while (counter <= 3);

Introducing Buttons p GuessIt3 contd // Get a random number between 1 and 3 inclusive randomNbr = (int) (3 * Math.random() + 1); // Get the mouse coordinates x = Mouse.getX(); y = Mouse.getY();

Introducing Buttons p GuessIt3 contd userNbr = 0; if (x > left1 && x < left1 + width && y > top && y < top + height) userNbr = 1; else if (x > left1 + 2* width && x < left1 + 3 * width && y > top && y < top + height) userNbr = 2; else if (x > left1 + 4 * width && x < left1 + 5 * width && y > top && y < top + height)` userNbr = 3;

GuessIt3 contd if (userNbr == randomNbr) { g.setColor(Color.red); g.fillRect(100, 80, 100, 40); g.setColor(Color.black); g.drawString("You got it!", 120, 100); }

Exercise 2 p 181 Write a program segment to simulate the game Rock, Scissors, and Paper. In this game for two players each makes a choice of Rock / Scissors / Paper (and indicate their choice by simultaneously displaying appropriate hand signs). Rock wins over Scissors Scissors wins over Paper Paper wins over Rock

Write a program segment to simulate throwing a coin. Exercise 3 p 183 Write a program segment to simulate throwing a coin.

Modify the program so that the computer generates the tosses Exercise 3 Extension Modify the program so that the computer generates the tosses and the user indicates his "guess" by pressing one of the buttons. Indicate incorrect guesses by colouring the circles grey. Note: The Ex3Template file for you to place your program segment in is located in the Chapter 8-progs folder.

Homework Explorer’s guide: WHOLE BOOK (in prep for exam)