Presentation is loading. Please wait.

Presentation is loading. Please wait.

Drawing Shapes (Graphics Class) Creating Objects Packages

Similar presentations


Presentation on theme: "Drawing Shapes (Graphics Class) Creating Objects Packages"— Presentation transcript:

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

2 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

3 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

4 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

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

6 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

7 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

8 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

9 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

10 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

11 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

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

13 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

14 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

15 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

16 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

17 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

18 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


Download ppt "Drawing Shapes (Graphics Class) Creating Objects Packages"

Similar presentations


Ads by Google