CPS 001 15.1 Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.

Slides:



Advertisements
Similar presentations
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
©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.
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.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
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.
©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.
Agenda For Feb Discussion (Assignments & Rubric) 2. PowerPoint Presentation (Drawing Basic Shapes). 4.Read Unit 3 carefully (pages ) then.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
User-defined parameters can be passed to an applet using the tags.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Graphics in Java Applications The Graphics Class The Canvas Class The MouseListener.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. Outline  Game of Life  Painting.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
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.
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.
Today’s topics Java Input More Syntax Upcoming Decision Trees More formal treatment of grammers Reading Great Ideas, Chapter 2.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
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 Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
Today’s topics Java Upcoming Reading Recursion in Graphics
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
A note on Programming Assignment
Example: Card Game Create a class called “Card”
Building Java Programs
Today’s topics Java Applications Upcoming Reading Graphics
Java Applets.
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
Building Java Programs
CSE 142 Lecture Notes Graphics with DrawingPanel Chapter 3
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Making a Smile Face Pepper.
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5

CPS Graphics  Java comes with drawing package  Fairly limited  Not hard to use  Use several new classes  Canvas Class  Graphics Class  Basics ideas  Pixels  Coordinates

CPS Coordinate System  Origin at top left!  Coordinates specified by giving x and y values  x is to the right  y is downward X Y 0,0 ( x,y ).

CPS Setting up Graphics  Declare Graphics and Canvas Graphics g; Canvas c;  Create Objects in init() c = new Canvas(); c.setSize(200, 200); add(c); g = c.getGraphics();  Can now create graphical figures g.drawLine(50,50,150,100); draws line from (50,50) to (150,100) : a diagonal line

CPS Specifying Location and Size  A (straight) line is specified by its endpoints  A rectangle is specified by its top left corner and its width and height g.drawRect(50, 100, 100, 50);  An oval is specified by its bounding box  This is a rectangle into which an oval fits tightly  The bounding box is specified like a rectangle g.drawOval(50, 50, 100, 50); (50,50)

CPS Graphics Demo public class GDemo extends java.applet.Applet implements ActionListener { Graphics g; Canvas c; Button b1, b2, b3, b4; public void init() { c = new Canvas(); c.setSize(200, 200); add(c); g = c.getGraphics(); b1 = new Button("Start"); b2 = new Button("Line"); b3 = new Button("Oval"); b4 = new Button("Rectangle");

CPS Graphics Demo.2 b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); add(b1); add(b2); add(b3); add(b4); } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == b1) { g.setColor(Color.white); // Color the whole canvas white g.fillRect(0, 0, 200, 200); }

CPS Graphics Demo.3 if (cause == b2) { g.setColor(Color.blue); g.drawLine(50, 50, 150, 100); // Demonstrate drawLine } if (cause == b3) { g.setColor(Color.green); g.drawOval(50, 50, 100, 50); // Demonstrate drawOval } if (cause == b4) { g.setColor(Color.red); g.drawRect(50, 100, 100, 50); // Demonstrate drawRect }

CPS The Graphics Methods  Used some additional methods  Summary (method headers or signatures ):  Basic: void drawLine(int x1, int y1, int x2, int y2) void drawRect(int x, int y, int width, int height) void drawOval(int x, int y, int width, int height)  Set color (before use): void setColor(Color c) oColor.white, Color.red, Color.blue, Color.green  Versions filled with color: void fillRect(int x, int y, int width, int height) void fillOval(int x, int y, int width, int height)

CPS Flexibility  Everything was fixed in first example  Could (Should!) use variables  Allow Coordinates to be read in  Allow (pre-programmed) shift for each button press  Shown in next program  Could do much more…  Buttons and input could also control: )  Color  Dimensions (Height and Width)  Filled or Not

CPS Graphics with offsets public class GDemo2 extends java.applet.Applet implements ActionListener { Graphics g; Canvas c; IntField iX, iY; Button b1, b2, b3, b4, b5; int x, y; public void init() { c = new Canvas(); c.setSize(200, 200); add(c); g = c.getGraphics(); b1 = new Button("Start"); b2 = new Button("Oval"); b3 = new Button("Rectangle"); b4 = new Button("SetCorner"); b5 = new Button("Shift"); iX = new IntField(20); iY = new IntField(20);

CPS Graphics with offsets.2 b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b5.addActionListener(this); add(iX); add(iY); add(b1); add(b2); add(b3); add(b4); add(b5); } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == b1) { g.setColor(Color.white); // Color the whole canvas white g.fillRect(0, 0, 200, 200); }

CPS Graphics with offsets.3 if (cause == b2) { g.setColor(Color.green); g.drawOval(x, y, 100, 50); // Demonstrate drawOval } if (cause == b3) { g.setColor(Color.red); g.drawRect(x, y, 100, 50); // Demonstrate drawRect } if (cause == b4) { x = iX.getInt(); y = iY.getInt(); } if (cause == b5) { x = x + 5; y = y + 5; }

CPS Array Defined Graphical Figure public class GDemo3 extends java.applet.Applet implements ActionListener { Graphics g; Canvas c; Button b1, b2; int x[] = {45, 95, 130, 130, 95, 45, 10, 10}; int y[] = {10, 10, 45, 95, 130, 130, 95, 45}; int n; public void init() { c = new Canvas(); c.setSize(200, 200); add(c); g = c.getGraphics(); b1 = new Button("Start"); b2 = new Button("StringFig");

CPS Array Defined Graphical Figure.2 b1.addActionListener(this); b2.addActionListener(this); add(b1); add(b2); n = 8; } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == b1) { g.setColor(Color.white); // Color the whole canvas white g.fillRect(0, 0, 200, 200); }

CPS Array Defined Graphical Figure.3 if (cause == b2) { int k, j; k = 0; g.setColor(Color.blue); while (k < n) { j = 0; while (j < n) { g.drawLine(x[k], y[k], x[j], y[j]); j = j + 1; } k = k + 1; }