• Graphics • Applets • Drawing Shapes (Graphics Class) • Creating Objects

advertisement
• Graphics
– 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.
6/30/2016
CS110-Spring 2005, Lecture 5
2
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.
6/30/2016
CS110-Spring 2005, Lecture 5
3
Coordinate System
positive X
direction
(0,0)
50 pixels
Y axis
(100, 50)
100 pixels
(0,0)
X axis
positive Y
direction
6/30/2016
CS110-Spring 2005, Lecture 5
4
Coordinate System
(0,0)
50 pixels
(100, 50)
drawLine(x1,y1,x2,y2)
drawLine(100,50,200,100)
6/30/2016
100 pixels
200 pixels
CS110-Spring 2005, Lecture 5
100 pixels
(200,100)
5
Coordinate System
(0,0)
50 pixels
(100, 50)
drawRect(x,y,width,height)
drawRect(100,50,200,100)
100 pixels
100
(100,150
(x, y+height)
6/30/2016
200
(x+width,y)
(300,50)
CS110-Spring 2005, Lecture 5
(300, 150)
(x+width, y+height)
6
Coordinate System
(0,0)
50 pixels
(100, 50)
drawOval(x,y,width,height)
drawOval(100,50,200,100)
100 pixels
100
(100,150
(x, y+height)
6/30/2016
200
(x+width,y)
(300,50)
CS110-Spring 2005, Lecture 5
(300, 150)
(x+width, y+height)
7
Coordinate System
(0,0)
50 pixels
(100, 50)
drawArc(x,y,width,height,start
Angle, arcAngle)
drawArc(100,50,200,100,180,
180)
6/30/2016
200
(x+width,y)
(300,50)
100 pixels
100
(100,150
(x, y+height)
CS110-Spring 2005, Lecture 5
(300, 150)
(x+width, y+height)
8
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.
6/30/2016
CS110-Spring 2005, Lecture 5
9
Applets
Java Programs
Java Applications
Java Applets
public class Welcome
{
public static void main(String[] args)
{
}
}
(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)
{
}
6/30/2016
}
CS110-Spring 2005, Lecture 5
10
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
{
All these
page.drawOval(100,50,200,200);
services are
page.fillOval(155,100,10,20);
provided by
page.fillOval(230,100,10,20);
Graphics class
page.drawArc(150,160,100,50,180,180);
}
}
6/30/2016
CS110-Spring 2005, Lecture 5
11
Drawing Shapes
6/30/2016
CS110-Spring 2005, Lecture 5
12
Creating Objects
• In Java a variable name represents either a
primitive value or an object.
– int i;
i
– String name; // name is an object of type String.
name
– 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.
6/30/2016
CS110-Spring 2005, Lecture 5
13
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
• i = 10;
• name = new String(“James Gosling”);
name
6/30/2016
“James Gosling”
CS110-Spring 2005, Lecture 5
14
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
6/30/2016
CS110-Spring 2005, Lecture 5
13
15
Aliases
int y = 6;
y
int x = 3;
x
3
x = y;
y
6
6
String name1 = “James”;
String name2 = “Lewis”;
name2 = name1;
6/30/2016
x
6
name1
“James”
name2
“Lewis”
name1
“James”
name2
CS110-Spring 2005, Lecture 5
16
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.
6/30/2016
CS110-Spring 2005, Lecture 5
17
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.
6/30/2016
CS110-Spring 2005, Lecture 5
18
Download