Programs with graphics output

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Advertisements

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
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.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
TEKS 8.6 (A,B) & 8.7 (A,D) This slide is meant to be a title page for the whole presentation and not an actual slide. 8.6 (A) Generate similar shapes using.
Pasewark & Pasewark Microsoft Office 2003 BASICS 1 MICROSOFT WORD Lesson 5 — Working with Graphics.
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Image Representation. Objectives  Bitmaps: resolution, colour depth and simple bitmap file calculations.  Vector graphics: drawing list – objects and.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
© A+ Computer Science - Chicken yeller = new Chicken();
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Introducing Templates and Graphics. 2 Creating Workbooks from Templates Choose a blank workbook or an existing workbook Search for templates at Office.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
CSC1401 Drawing in Java - 1. Goals Understand at a conceptual and practical level How to use the Turtle class to draw on a picture How to use the java.awt.Graphics.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
How Many Shapes make this design? How many Layers? What do you think is the 1 st layer (bottom layer), 2 nd layer, 3 rd layer, 4 th layer, and 5 th layer.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Introduction to Computer Program Design COMP T1 .
Topic one text Topic two text Topic three text Topic four text
Adapted from slides by Marty Stepp and Stuart Reges
Introduction to Computer Program Design COMP T1 .
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
First text statement positioned here at guide intersection
Basic Graphics Drawing Shapes 1.
Chapter 14 JavaFX Basics Dr. Clincy - Lecture.
Smart Graphic Layout TOPIC statement
به نام مهربانترین In the name of the most compassionate
Topic one text Topic two text Topic three text Topic four text
РОСІЙСЬКА МОВА Кількість завдань - 51 Час на виконання – 150 хв.
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
SSEA Computer Science: Track A
Empower Support Freedom Inspire Together
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Themes, Styles, & Graphics
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Programs with graphics output Write a program to draw a lollipop: Design What shapes can we draw? UI has methods to draw rectangles, ovals, lines, arcs,… ⇒ Draw one thick black line one red oval, How do we draw them? Need to set the color first (initially black) then call the draw/fill methods: must specify the positions and size rectangles/ovals: left, top, width, height lines: x and y of each end. (0,0) y x Shapes drawn on top of previous shapes Coordinates measured from left and top

Lollipop program Design: Must work out the coordinates: x (300) (200) x ≈ 700 Design: Method drawLollipop(): set line width to 10 draw line set line width back to 1 set color to red fill oval Must work out the coordinates: line: oval: y ≈500

Writing the program Need import statements Need a class (with a descriptive comment) Need a constructor Need a method (with a descriptive comment) import ecs100.*; import java.awt.Color; /** Draws little shapes on the graphics pane */ public class Drawer { /** Constructor: Set up interface */ public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw an red lollipop with a stick */ public void drawLollipop() { // actions Button that will call the drawLollipop method of this class Write the method body as comments first

Writing the program: using comments import ecs100.*; import java.awt.Color ; /** Draws little pictures on the graphics pane */ public class Drawer { public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw an orange face with a brimmed hat */ public void drawLollipop() { // set line width to 10 // draw line (300,200) to (300, 400) // set line width to 1 // set colour to red // fill oval @(260,160) 80x80 Now express each comment in Java (look up documentation as necessary)

Try it! Get the lollipop base project from the web site and try to complete it.

Writing the program import ecs100.*; import java.awt.Color ; /** Draws little pictures on the graphics pane */ public class Drawer { public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw a lollipop */ public void drawLollipop() { UI.setLineWidth(10); // set line width to 10 UI.drawLine(300, 200, 300, 400); // draw line UI.setLineWidth(1); // set line width back to 1 UI.setColor(Color.red); // set color to red UI.fillOval(260, 160, 80, 80); // draw blob Now compile and run the program

Improving the design This program is very inflexible: What if We want the lollipop to be in a different position? We want the lollipop to be bigger or smaller? We want the stick to be longer? …. We want to draw two of them? Current design is filled with literal values ⇒ difficult to understand ⇒ difficult to change (have to find all the places and redo all the arithmetic

Move or resize the Lollipop. import ecs100.*; import java.awt.Color ; /** Draws little pictures on the graphics pane */ public class Drawer { /** Constructor: Set up the interface with one button */ public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw a lollipop */ public void drawLollipop() { UI.setColor(Color.black); // set color to black UI.setLineWidth(10); // set line width to 10 UI.drawLine(300, 200, 300, 400); // draw line UI.setLineWidth(1); // set line width back to 1 UI.setColor(Color.red); // set color to red UI.fillOval(260, 160, 80, 80); // draw blob Move it left Move it down Change blob size

Improving the design Better design: Use named constants and variables ⇒ easier to write and easier to change ⇒ get the computer to do the arithmetic Use named constants for values that won’t change while the program is running.

Values to specify lollipop & stick size x stick

Improving the program: constants import ecs100.*; import java.awt.Color; /** Draw a lollipop with a stick */ public class Drawer { public static final double x = 300; // horizontal center of lollipop public static final double y = 180; // vertical center of lollipop public static final double size = 80; // diameter of lollipop public static final double stick = 200; // length of lollipop stick public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw a lollipop */ public void drawLollipop() { UI.setLineWidth(size/8); UI.drawLine(x, y, x, y+stick); UI.setLineWidth(1); UI.setColor(Color.red); UI.fillOval(x-size/2, y-size/2, size, size); Easy to change: one place! x size y stick

Syntax rules: Program structure 2nd version 〈import statements〉 public class 〈classname〉 { Defining a constant 〈constant definition〉 public static final 〈constructor description〉 〈type〉 〈name〉 = 〈value〉 ; 〈method description〉 }

Improving the program: more names ⋮ public static final double x = 300; // horizontal center of lollipop public static final double y = 180; // vertical center of lollipop public static final double size = 80; // diameter of lollipop public static final double stick = 200; // length of lollipop stick /** Constructor: Set up the interface with one button */ public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw a lollipop */ public void drawLollipop() { UI.setLineWidth(10); UI.drawLine(x, y, x, y+stick); UI.setLineWidth(1); UI.setColor(Color.red); UI.fillOval(x-size/2, y-size/2, size, size); Still have a problem: What do these expressions mean?

Values to specify lollipop & stick top left y size x stick bot Useful to have more named values!

Improving the program: variables ⋮ public static final double x = 300; // horizontal center of lollipop public static final double y = 180; // vertical center of lollipop public static final double size = 80; // diameter of lollipop public static final double stick = 200; // length of lollipop stick /** Draw a lollipop */ public void drawLollipop() { double left = x – size/2; // left of lollipop double top = y – size/2; // top of lollipop double bot = y + stick; // bottom of stick UI.setLineWidth(10); UI.drawLine(x, y, x, bot); UI.setLineWidth(1); UI.setColor(Color.red); UI.fillOval(left, top, size, size); }

Principle of good design Use well named constants or variables wherever possible, rather than literal values ⇒ easier to understand ⇒ easier to get right ⇒ much easier to modify Choosing the right constants or variables is an art!! why did I choose “x" instead of “left" ? why did I choose “y" instead of stick bottom? We have effectively parameterised the drawing Four values (x, y, size, stick) control the whole thing.