Drawing Shapes (Graphics Class) Creating Objects Packages

Slides:



Advertisements
Similar presentations
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.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
© 2004 Pearson Addison-Wesley. All rights reserved2-1 Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
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.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
Chapter Day 4. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 4 Return Signed Contracts Questions from last Class?? Problem set 1 Posted.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
A Simple Applet.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
© 2004 Pearson Addison-Wesley. All rights reserved Lecture 2  Chapter 2 CS0401: Intermediate Java Programming.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java,
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
Intro to Applets August 19, 2008 Mrs. C. Furman. Java Applets vs. Java Applications Java Applet: a program that is intended for use on the web. Java Applet:
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Graphics & Applets CSC 1051 – Data Structures and Algorithms I
Lecture 15.1 Static Methods and Variables Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSE 1201 Object Oriented Programming Using Classes and Objects.
Java 3 More Java Then Java. Libraries & Packages Java standard class library is composed of useful classes that help us to achieve our programming goals.
Creating a Java Application and Applet
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Intro to Graphics from Chapter 2 of Java Software Solutions
Chapter 02 Data and Expressions.
Variables in Java A variable holds either
CSC 1051 – Data Structures and Algorithms I
Using Classes and Objects
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Packages & Random and Math Classes
Outline Character Strings Variables and Assignment
Lecture 9 Using Objects.
APPLET PROGRAMMING.
Presentation transcript:

Drawing Shapes (Graphics Class) Creating Objects Packages Coordinate System Representing Color Applets Drawing Shapes (Graphics Class) Creating Objects Packages

Graphics A picture, like other information stored on a computer must be digitized by breaking the information into pieces and representing those pieces as numbers. We break the picture into pixels. A pixel is not an absolute unit of length. The size of a pixel can be different on different screens. You can think of your computer screen as being covered by small squares. You cannot show anything smaller than these squares. A pixel is one of these squares. 8/23/2019 CS110-Spring 2005, Lecture 5

Graphics So a pixel is the smallest length your screen is capable of showing. The number of pixels used to represent the picture is called picture resolution and the number of pixels that can be displayed by the monitor is called the monitor resolution. 8/23/2019 CS110-Spring 2005, Lecture 5

Coordinate System positive X direction (0,0) 50 pixels Y axis (100, 50) 100 pixels (0,0) X axis positive Y direction 8/23/2019 CS110-Spring 2005, Lecture 5

Coordinate System drawLine(x1,y1,x2,y2) drawLine(100,50,200,100) (0,0) 50 pixels (100, 50) 100 pixels drawLine(x1,y1,x2,y2) drawLine(100,50,200,100) 100 pixels (200,100) 200 pixels 8/23/2019 CS110-Spring 2005, Lecture 5

Coordinate System drawRect(x,y,width,height) drawRect(100,50,200,100) (0,0) 50 pixels (x+width,y) (300,50) (100, 50) 200 drawRect(x,y,width,height) drawRect(100,50,200,100) 100 pixels 100 (100,150 (300, 150) (x, y+height) (x+width, y+height) 8/23/2019 CS110-Spring 2005, Lecture 5

Coordinate System drawOval(x,y,width,height) drawOval(100,50,200,100) (0,0) 50 pixels (x+width,y) (300,50) (100, 50) 200 drawOval(x,y,width,height) drawOval(100,50,200,100) 100 pixels 100 (100,150 (300, 150) (x, y+height) (x+width, y+height) 8/23/2019 CS110-Spring 2005, Lecture 5

Coordinate System drawArc(x,y,width,height,startAngle, arcAngle) (0,0) 50 pixels (x+width,y) (300,50) (100, 50) 200 drawArc(x,y,width,height,startAngle, arcAngle) drawArc(100,50,200,100,180,180) 100 pixels 100 (100,150 (300, 150) (x, y+height) (x+width, y+height) 8/23/2019 CS110-Spring 2005, Lecture 5

Representing Colors A black and white picture can be stored by representing each pixel using a single bit. 0 – white pixel, 1 – black pixel In Java colors are specified by three numbers that are collectively referred to as RGB value. Color class in library to define and manage colors. 8/23/2019 CS110-Spring 2005, Lecture 5

Applets Java Programs Java Applets Java Applications (intended to be embedded into an HTML document and executed using Web Browser) import javax.swing.JApplet; import java.awt.*; public class Welcome2 extends JApplet { public void paint (Graphics page) } Java Applications public class Welcome { public static void main(String[] args) } 8/23/2019 CS110-Spring 2005, Lecture 5

Drawing Shapes import javax.swing.JApplet;//uses the JApplet in Swing package import java.awt.*; // uses the AWT package public class HappyFace extends JApplet //beginning of class { public void paint (Graphics page) //invoked automatically page.drawOval(100,50,200,200); page.fillOval(155,100,10,20); page.fillOval(230,100,10,20); page.drawArc(150,160,100,50,180,180); } All these services are provided by Graphics class 8/23/2019 CS110-Spring 2005, Lecture 5

Drawing Shapes 8/23/2019 CS110-Spring 2005, Lecture 5

Creating Objects In Java a variable name represents either a primitive value or an object. int i; String name; // name is an object of type String. The second declaration creates a String variable that holds a reference to a String object. An object variable does not hold an object itself. It holds an address of an Object. i name 8/23/2019 CS110-Spring 2005, Lecture 5

Creating Objects An object variable can also be set to null (a reserved word). String name = null; A null reference specifically indicates that a variable does not refer to an object. i = 10; name = new String(“James Gosling”); i 10 name “James Gosling” 8/23/2019 CS110-Spring 2005, Lecture 5

Creating Objects The act of creating an object using the new operator is called instantiation. An object is said to be an instance of a particular class. After an object is created we can use dot operator to access its methods. i = name.length(); i 13 8/23/2019 CS110-Spring 2005, Lecture 5

Aliases int y = 6; int x = 3; x = y; String name1 = “James”; String name2 = “Lewis”; name2 = name1; 6 x 3 y 6 x 6 “James” name1 “Lewis” name2 “James” name1 name2 8/23/2019 CS110-Spring 2005, Lecture 5

Aliases All references to the object originally referenced by name2 are gone, that object can not be used again in the program. The program can no longer invoke its methods. At this point the object is called garbage because it serves no useful purpose. Java performs automatic Garbage collection. 8/23/2019 CS110-Spring 2005, Lecture 5

Packages A Java Standard class library is a set of classes that supports the development of programs. The classes are grouped into packages. Each class is part of a particular package. String and System are part of java.lang package. Scanner is part of the java.util package. 8/23/2019 CS110-Spring 2005, Lecture 5