A+ Computer Science METHODS.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
Web Design & Development Lecture 19. Java Graphics 2.
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.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
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.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
© A+ Computer Science - Chicken yeller = new Chicken();
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.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Clicker quick questions 10/17/13 CSE 1102 Fall 2013.
Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;
Lecture 3.1 Using Graphics with JFrame. © 2006 Pearson Addison-Wesley. All rights reserved javax.swing.JFrame - int x - int y - int width - int.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Introducing, the JFrame Gives us a work area beside System.out.println.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
A structured walkthrough
OOP Powerpoint slides from A+ Computer Science
Chapter 3 Syntax, Errors, and Debugging
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
GUI AND GRAPHICS.
Java Programming: Guided Learning with Early Objects
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Lesson A4 – Object Behavior
Graphics -- Introduction
A+ Computer Science INPUT.
UNIT-5.
© A+ Computer Science - GridWorld © A+ Computer Science -
CSE 1020:Programming by Delegation
Using PARAMETERS In Java.
A+ Computer Science METHODS.
© A+ Computer Science - OOP © A+ Computer Science -
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
A+ Computer Science INPUT.
© A+ Computer Science - GridWorld The GridWorld case study provides a graphical environment where visual objects inhabit and interact.
Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.
© A+ Computer Science - GridWorld © A+ Computer Science -
A+ Computer Science PARAMETERS
© A+ Computer Science - GridWorld © A+ Computer Science -
Lecture 4: Standard Java Graphics
Presentation transcript:

A+ Computer Science METHODS

© A+ Computer Science - www.apluscompsci.com Instantiation AplusBug dude = new AplusBug(); dude is an AplusBug reference. new AplusBug() creates a new AplusBug Object out in memory. dude stores the location of that new AplusBug Object. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Instantiation AplusBug dude = new AplusBug(); dude 0x234 0x234 AplusBug dude is an AplusBug reference. new AplusBug() creates a new AplusBug Object out in memory. dude stores the location of that new AplusBug Object. dude is a reference variable that refers to an AplusBug object. © A+ Computer Science - www.apluscompsci.com

Methods

© A+ Computer Science - www.apluscompsci.com What is a method? A method is a storage location for related program statements. When called, a method usually performs a specific task. System.out.println( ) Methods store commands / program statements. When called, the code inside the method is activated. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Common Methods Math.random() keyboard.nextInt( ) System.out.println( ) © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Methods public void speak() { out.println("chirp-chirp"); } OUTPUT chirp-chirp The speak method shown above contains a single println command. The speak method would print out chirp-chirp on the console window. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Methods access return type name params code public void speak( ) { System.out.println("chirp-chirp"); } A method has a signature. The signature provides information about the method. The name is most used and recognizable part of the signature. The method shown above is named speak. The return type states what the method will return. Method speak has a return type of void which means the method does not return a value. The access of method speak is public. This states that the method speak can be called from any location. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Public All members with public access can be accessed or modified inside and outside of the class where they are defined. Public access simply means the member can be used anywhere inside or outside of the class. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com public class Turkey { public void speak() out.println("gobble-gobble"); } public void sayName() out.println("big bird"); //code in the main of another class Turkey bird = new Turkey(); bird.speak(); bird.sayName(); Turkey OUTPUT gobble-gobble big bird gobble-gobble big bird gobble-gobble In the Turkey example, speak is called which prints out gobble-gobble. sayName is called which prints out big bird. Then, speak is called again to print out gobble-gobble followed by a call to sayName to print big bird again. Last, speak is called to print out gobble-gobble. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com public class Turkey { public void speak() out.println("gobble-gobble"); } public void sayName() out.println("big bird"); speak(); //code in the main of another class Turkey bird = new Turkey(); bird.speak(); bird.sayName(); Turkey OUTPUT gobble-gobble big bird © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com turkey.java turkeyrunner.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Work on Programs! Crank Some Code! © A+ Computer Science - www.apluscompsci.com

Graphics and Constructors

© A+ Computer Science - www.apluscompsci.com Constructors Constructors always have the same name as the class. GraphOne test = new GraphOne(); AplusBug rob = new AplusBug(); Constructors are used to initialize all of the data/properties inside the class. Constructors ensure that the Object is ready for use. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors reference variable Scanner keyboard = new Scanner(System.in); Scanner is a class which must be instantiated before it can be used. In other words, you must make a new Scanner if you want to use Scanner. A reference must be used to store the location in memory of the Scanner object created. System.in is the parameter passed to the Scanner constructor so that Java will know to connect the new Scanner to the keyboard. keyboard is a reference that will store the location of newly created Scanner object. object instantiation / constructor call © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors public class GraphicsRunner extends JFrame { private static final int WIDTH = 640; private static final int HEIGHT = 480; public GraphicsRunner() setSize(WIDTH,HEIGHT); getContentPane().add( new Circles() ); setVisible(true); } public static void main( String args[] ) GraphicsRunner run = new GraphicsRunner(); the constructor When a GraphicsRunner class is instantiated, the size of the JFrame is set and the visibility is also set. The setSize() method sets the width and height of the JFrame. The setSize() method tells the simply to either show the JFrame or hide the Frame. The add() method adds a Component to the JFrame. A new Circles() Object is being instantiated and added to the JFrame. constructor call © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Frame Frame / JFrame Canvas / JPanel The Frame is used to hold up / display a Canvas or Panel. Frame / JFrame Objects are used to hold up / display Canvas and JPanel Objects. All drawing occurs on the Canvas / JPanel. The JFrame simply provides a place to show Canvas / JPanel after the drawing has occurred. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Paint public class Circles extends Canvas { //constructors public void paint( Graphics window ) window.setColor(Color.BLACK); window.drawString("Circles", 50, 50); window.setColor(Color.BLUE); window.drawOval(500,300,40,40); } //other methods paint paint() is called automatically when you instantiate the class containing the paint method. When an event is triggered that requires a redraw, paint is called again. To call paint() without a Graphics parameter, you can use the repaint() method. paint() is the method typically used to draw Graphics on the window. There are other methods that could be used, but paint() is used most frequently. paint() is called when the window needs to be redrawn. If an event occurs that requires the window be updated, the system will call paint(). paint() can be called without a Graphics parameter by simply using the repaint() method. paintComponent() is another method used for drawing / redrawing the window. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com graphicsrunner.java circles.java © A+ Computer Science - www.apluscompsci.com

Graphics and Methods © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com Graphics frequently used methods Name Use setColor(x) sets the current drawing color to x drawString(s,x,y) draws String s at spot x,y drawOval(x,y,w,h) draws an unfilled oval at spot x,y that is w wide and h tall fillOval(x,y,w,h) draws a filled oval at spot x,y that is w wide and h tall The Java Graphics class has many useful methods. The chart above lists the most common methods we will be using. import java.awt.Graphics; import java.awt.Color; import javax.swing.JFrame; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing Parameters A parameter/argument is a channel used to pass information to a method. setColor() is a method of the Graphics class the receives a Color. void setColor(Color theColor) window.setColor( Color.RED ); Most, if not all, of the Graphics class methods require parameters. The parameters communicate to the Graphics methods information about what needs to be done. The setColor() method changes the current drawing color to the color passed in. setColor() cannot be called without a color parameter. method call with parameter © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing Parameters void fillRect (int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); method call with parameters The fillRect() method requires four pieces of information. fillRect() needs an x value, a y value, a width, and a height. fillRect() will draw a filled rectangle on the window at x,y with height and width as stated by the parameters. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing Parameters void fillRect(int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); The call to fillRect would draw a rectangle at position 10,50 with a width of 30 and a height of 70. The fillRect() method requires four pieces of information. fillRect() needs an x value, a y value, a width, and a height. fillRect() will draw a filled rectangle on the window at x,y with height and width as stated by the parameters. © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com Graphics frequently used methods Name Use drawLine(a,b,c,d) draws a line starting at point a,b and going to point c,d drawRect(x,y,w,h) draws an unfilled rectangle at spot x,y that is w wide and h tall fillRect(x,y,w,h) draws a filled rectangle at spot x,y that is w wide and h tall import java.awt.Graphics; import java.awt.Color; import javax.swing.JFrame; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Graphics X goes across 0,0 Y goes down Notice the Graphics screen being used with Graphics class does not use Cartesian coordinates. X goes across and Y goes down. X starts at 0 and goes to MAXX which in this case is 640. Y starts at 0 and goes down to MAXY which in this case is 479. 639,479 window.fillRect( 10, 50, 30, 70 ); © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing Parameters X goes across 0,0 X=100 y=100 Y goes down width=50 height=50 window.fillOval( 100, 100, 50, 50 ); © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Rectangles public void paint( Graphics window ) { window.setColor(Color.BLUE); window.fillRect(150, 300, 100, 20); window.setColor(Color.GRAY); window.drawRect(200,80,50,50); } The paint() method is typically doing the most drawing. Other methods may be called from paint() as well. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com rectangles.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com lines.java © A+ Computer Science - www.apluscompsci.com

Graphics frequently used methods Name Use drawArc(x,y,w,h,startAngle,arcAngle) draws an arc at spot x,y that is w wide and h tall fillArc(x,y,w,h,startAngle,arcAngle) draws a filled arc at spot x,y that is w wide and h tall startAngle specifies the start of the arc arcAngle specifies the length of the arc import java.awt.Graphics; import java.awt.Color; import javax.swing.JFrame; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com arcs.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com fonts.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com colors.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Work on Programs! Crank Some Code! © A+ Computer Science - www.apluscompsci.com

A+ Computer Science METHODS