OOP&M - laboratory lectures1 OOP&M – LAB3 LABtres: drawing.

Slides:



Advertisements
Similar presentations
Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
Advertisements

Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
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.
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
Topic 9 more graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
CS 280 Data Structures Professor John Peterson. Quiz 4 Recap Consider the following array: {2, 6, 7, 3, 4, 1, 5, 9}. Draw this in tree form and then show.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
C++ Graphics Primitives April 15th. void clearscreen(int c) –clear the screen to background color c –If c = 1 screen black.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
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.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
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.
Agenda For Feb Discussion (Assignments & Rubric) 2. PowerPoint Presentation (Drawing Basic Shapes). 4.Read Unit 3 carefully (pages ) then.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
1 Loops and Branches Ch 21 and Ch18 And how you can use them to draw cool pictures!
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
// Java0601.java // This program demonstrates that the methods of a class are not always // accessible, like they were with the class. In this case an.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Object Oriented Programming Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
DAAD project “Joint Course on OOP using Java” Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
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.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns.
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.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
17/3/00SEM107 © Kamin & Reddy Class 13 - Animation 1 Class 13 - Animation r Summary of loops m while-do m for m do-while r while loop examples r for loop.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public.
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.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
Chapter 7 Introduction to High-Level Language Programming.
Review of Math Class Methods abssqrt minmax powround ceilfloor.
CSE / ENGR 142 Programming I
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Chapter 6 Loops (iteration).
Building Java Programs
Programming for Artists
Lecture 7: Introduction to Processing
Building Java Programs
Building Java Programs
Topic 9 More Graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
APPLET PROGRAMMING.
Presentation transcript:

OOP&M - laboratory lectures1 OOP&M – LAB3 LABtres: drawing

OOP&M - laboratory lectures2 OOP&M – LAB3 Objectives The basic aim of this laboratory session is to learn how to draw basic forms and how to animate them In a first set of exercises the students should learn how to draw lines directly and taking the data from an array The students should learn the use of some mathematical functions like sin(), cos(), random(), round() and others After the lines, the students will learn how to make simple animation processes by moving forms throw the applet surface Two different movements are presented: linear and circular

OOP&M - laboratory lectures3 OOP&M – LAB3 LABtres: drawing Lines

OOP&M - laboratory lectures4 OOP&M – LAB3 – lines EXERCISE A.1 – Just a line According to the slides of the theory class try to write a simple applet that shows a line [Hint: use *.drawLine(), *.setColor() … ]

OOP&M - laboratory lectures5 OOP&M – LAB3 – lines EXERCISE A.2 – A line ‘n’ friends Using an array and a loop draw four lines on the screen [Hint: use an array of integers in the init() method, and a loop in the paint() method] [Note: arrays of integers are declared like this: int[] my_array = new int[number_of_elements] ]

OOP&M - laboratory lectures6 OOP&M – LAB3 – lines EXERCISE A.3 – Random Lines Draw a set of four lines whose positions are random [Hint: use Math.random(), Math.round() and create the array of the exercise A.2 within a loop  for() ] [Note: java is very restrictive with type conversions use the following line data[i]= (int) Math.round(300*Math.random()) ] Type conversion of long to int

OOP&M - laboratory lectures7 OOP&M – LAB3 – lines EXERCISE A.4 – Lots of lines Now make random the number of lines on the screen and, after implementing the program, answer to the following questions: A. what happens if you restart the applet? B. what happens if you reload the applet? [Hint: declare the number of lines as a random variable outside the init() method and outside the paint() method]

OOP&M - laboratory lectures8 OOP&M – LAB3 LABtres: drawing Circles

OOP&M - laboratory lectures9 OOP&M – LAB3 – circles EXERCISE B.1 – The Oval and the circle Now try to draw another form, this time let’s try with a circle. Could you explain what the following program is doing? import java.applet.*; import java.awt.*; public class circles extends Applet { public void paint(Graphics g) { for(int i=0; i<10; i+=2) g.drawOval(30-i/2, 30-i/2, 10+i, 10+i); } [Hint: too easy]

OOP&M - laboratory lectures10 OOP&M – LAB3 – circles EXERCISE B.2 – Moving the circle Draw a white rectangle as a background as big as the applet, use: getSize().width and getSize().height Try now to draw a red circle with *.fillOval() and move it from position (30,30) to position (30,100) following a straight line [Hint: for moving the circle you have to paint it, show it during a period of time, delete it and repaint it again in another position] [Use the following, it stops the program during a little period of time allowing you to see the whole process try { Thread.sleep(500);} catch (InterruptedException e) {} ] Time that the program stops in miliseconds

OOP&M - laboratory lectures11 EXERCISE B.2 – Moving the circle The process of moving a form looks like the following To delete an object is the same thing than to paint it with the background color!!! Calculate coordinates Place form Show for a while Delete form OOP&M – LAB3 – circles

OOP&M - laboratory lectures12 OOP&M – LAB3 – circles EXERCISE B.3 – Moving the circle Take B.2 and do the following: 1 st write a method called “ pause() ” where the special code for stopping the program is contained 2 nd declare a global variable called “ speed ” that represents the number of miliseconds that the method *.sleep() should take as parameter Experiment with different values for the “ speed ” variable and check how it works [Note: now your code should look more clear and ready for being used in the next exercises]

OOP&M - laboratory lectures13 OOP&M – LAB3 LABtres: drawing Spirals

OOP&M - laboratory lectures14 OOP&M – LAB3 – spirals EXERCISE C.1 – Spinning around Now we will try to describe a more complex movement with the circle … we are going to make it move in circles!! Adapt B.3 for making your circle move in a circle with a radius of 100 pixels and make 10 rounds [Note: attend to the explanation]  x y x = R*sin() y = R*cos() R

OOP&M - laboratory lectures15 OOP&M – LAB3 – spirals EXERCISE C.1 – Spinning around If we want to make an object move in circles, we need to consider the characteristics of the movement We localize objects on the applet by its coordinates x and y This means that we need a relationship between the coordinates x,y and the form of a circle It is quite easy to see this relationship if we attend to the picture in the previous slide So, the process is the same as before, but with more complicated operations in phase one: Calculate coordinates Place circle Show for a while Delete circle

OOP&M - laboratory lectures16 OOP&M – LAB3 – spirals EXERCISE C.1 – Spinning around The code for describing the circular movement looks like: double phase = j*Math.PI/100; int x = x0 + (int) Math.round(radius * Math.sin(phase)); int y = y0 + (int) Math.round(radius * Math.cos(phase)); x0 and y0 are the initial position of the circle on the screen, it should be the center (150,150), radius should be 100, j is the variable that counts the number of times that the drawing process must be repeated

OOP&M - laboratory lectures17 OOP&M – LAB3 – spirals EXERCISE C.2 – To the center in ten rounds Modify the code of C.1 and make the circle spin into an spiral It must reach the center in the same 10 rounds Extra question: Why does the circle not maintain the velocity? How would you change it? [HINT: the trick here is to change the radius of the movement]

OOP&M - laboratory lectures18 OOP&M – LAB3 – spirals EXERCISE C.3 – Drawing the Spiral Use the *.drawLine() method and let the circle of C.2 draw an spiral while it moves [HINT: you need to save the previous position of the circle in some variables]