Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances.

Slides:



Advertisements
Similar presentations
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
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.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
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.
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
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.
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.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Classes. Preparation Scene so far has been background material and experience –Computing systems and problem solving –Variables –Types –Input and output.
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.
Building Java Programs Graphics Reading: Supplement 3G.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Midterm: Question 1 (35%) (30 minutes) Write an assembly program to draw a rectangle. – (5%) Show a message to ask for information of the rectangle – (5%)
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
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.
Lazy Evaluation Computer Science 3 Gerb Objective: Understand lazy evaluation in Java.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
CSC111 Quick Revision.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Java Programming: Guided Learning with Early Objects
Topic 8 graphics "What makes the situation worse is that the highest level CS course I've ever taken is cs4, and quotes from the graphics group startup.
Building Java Programs
slides created by Alyssa Harding
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Concepts for this lecture: Class Instance of a class, (an object)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Outline Software Development Activities
Just Enough Java 17-May-19.
Building Java Programs
Making a Smile Face Pepper.
Graphics Reading: Supplement 3G
Presentation transcript:

Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances

if statement if ( ) else

Boolean expressions u evaluated just like arithmetic expressions u evaluate to either TRUE or FALSE exp1 exp2 exp1 || exp2 exp && exp2 !exp1 true true false false true false

Examples int count = 0; int limit = 10; if ( (count == 0) && (limit < 20) )... if ( count == 0 && limit < 20 )... if ( (limit > 20) || (count < 5) )... if ( !(count == 12) )... if ( (count == 1) && (x < y) )... if ( (count < 10) || (x < y) )...

Examples if ( !( ((count = 0) ) )... if ( ((limit/count) > 7) || (limit < 20) )... if ( (limit 7) )... if ( ((limit/count) > 7) && (limit < 0) )... if ( (limit 7) )...

Operator precedence u unary operators: +, -, ++, --, and ! u *, /, % u +, - u, = u ==, != u && u ||

Conditionals: Examples int a = 5;int b = -6;int c = 15; if (a > b) {c = 2; a = 4;} if (a <= b) {c = 2; a = 4;} if (a <= b) c = 2; a = 4;

if (a > b) c=2; a=4; if (a > 5) a=7; else a = 6; b = 7; if (a == 5) a=7; else a = 6; b = 7;

public class Coordinate { public int x; public int y; // Set field x to p*p public void setX(int p) { x= p*p; } // Set field y to q public void setY(int q) { y= q; } // Return the sum of the squares of the fields public int sumSquares() { return x*x + y*y; } Class Coordinate

“main” program public class Example1 { public static void main(String args[]) { Coordinate c = new Coordinate(); c.setX(3); c.setY(4); }

New program: boxes

import java.awt.*; // An instance of class Box is a rectangle. public class Box { public int xUpperLeft;// The box has an upper left corner at public int yUpperLeft;// position (xUpperLeft, yUpperLeft). public int width;// width of box public int height;// height of box public Color color;// color of box Box.java

// Draw the box in designated color using Graphics g public void drawBox (Graphics g) { // save original color in variable oldColor Color oldColor = g.getColor (); // set new color and draw the rectangle g.setColor (color); g.fillRect (xUpperLeft, yUpperLeft, width, height); // restore original color g.setColor (oldColor); }

CUCSDrawing Class // Class CUCSDrawing: a simple graphics window. public class CUCSDrawing extends Frame { public void paint(Graphics g) { g.setColor(Color.black); g.drawOval(15,30,30,30); g.drawRect(77,30,40,30); g.setColor(Color.red); g.drawString("circle",15,80); g.drawString("rectangle",75,80); }

“main” program // Main program for simple Graphics Applications. The program creates the // drawing window and sets its size and title. The actual drawing is done // by the paint method in class Drawing. The drawing window is moved down // the screen slightly so it won't overlap the System.in window if used. public class CUCSGraphicsApplication { public static void main(String args[]) { CUCSDrawing d = new CUCSDrawing(); d.resize(200,150); d.move(0,75); d.setTitle("Drawing"); d.show(); d.toFront(); }

new paint method // Draw 2 Boxes and print their locations public void paint(Graphics g) { Box b1;// first box Box b2;// second box // Create the first box and initialize its fields. b1 = new Box(); b1.xUpperLeft = 50; b1.yUpperLeft = 70; b1.width = 60; b1.height = 20;

// Create the second box and initialize its fields. b2 = new Box(); b2.xUpperLeft = 150; b2.yUpperLeft = 150; b2.width = 20; b2.height = 30; // Draw the boxes b1.drawBox(g); b2.drawBox(g); }