Title: CS 303E Class 9: Applets and Graphics
1CS 303E Class 9Applets and Graphics
A picture is worth ten thousand words. -
Chinese proverb A formula is worth ten thousand
pictures. -Dykstra
2A Bit of History
- One of the reasons Java has gotten so much ink
and is considered a hot language is its
connection to the internet. - For a history of Java see
- java.sun.com/features/1998/05/birthday.html
- Then Gosling moved the mouse over an illustration
of a 3D molecule in the middle of the text. The
3D molecule rotated with the mouse movement. Back
and forth, up and around. "The entire audience
went Aaaaaaah!'" says Gosling. "Their view of
reality had completely changed because it MOVED."
Now everyone was paying close attention. Next,
Gosling and Gage pushed the audience over the
edge with an animated line-sorting algorithm that
Gosling had written.
3Applets
- Have nothing to do with apples
- Suppose to mean a little program. (As opposed to
an application) - Java can be used to write applets or applications
- So far we have only done applications
- applets can be embedded in web pages and seen via
the internet
4Examples of Applets
- www.horstmann.com/applets/Retire/Retire.html
- www.chemsymphony.com/index2.htm
- java.sun.com/applets/jdk/1.0/demo/SortDemo/
- www.traffic.uni-duisburg.de/model/
- Applets can be made as a part of a web page or
viewed via an appletviewer. The appletviewer is
used when developing the applet.
5What's Going on Here
- Applets are Java Programs.
- Like web pages they are stored on a remote
computer called a server. - When you visit a page the html code and java byte
code for the applet are sent from the sever via
the internet to your computer, the client - You web browser presents the html code to you and
your Java Virtual Machine runs the byte code to
produce machine code for your computer.
6A Picture of the Previous Info
applets
This is why Java was thought to hold such great
potential. A program could be written and then
used on any computer. It didn't have to be
rewritten for macs, unix, PCs.
7Graphics
- Graphics are easy when done in the context of an
applet - They are easy in applications too, it just takes
a little more work. - We will use a number of predefined Java classes
Graphics Graphics2D Rectangle Ellipse2D.Double Po
int2D.Double Color Font Line2D.Double
8Very Basic Computer Graphics
- The screen consists of pixels (picture elements)
- Pixels are small rectangles on the screen that
can appear in many colors. - Individual pixels are defined with by an x and y
coordinate. - The screen is set up with 0,0 in the top left
corner
0,0
9The paint method
- Applet never have a main method
- When creating an applet you must define a method
called paint. - This method is called whenever the window the
applet is in is opened, uncovered, resized, or
affected in any of number of ways. - the paint method is sent 1 parameter, a Graphics
object which can be used to draw things in the
applet window - Due to some technical reasons this Graphics
object is normally converted to a Graphics2D
object
10The Basics of method paint
import java.applet.Applet import
java.awt.Graphics import java.awt.Graphics2D imp
ort java.awt.Rectangle public class
MyFirstApplet extends Applet public void
paint(Graphics g) Graphics2D g2
(Graphics2D) g / and then we get to work
creating the various things we want to draw
/ Rectangle ceralBox new
Rectangle(0,0,20,30) Rectangle smallSquare
new Rectangle(10,10,5,5) g2.draw(ceralBox)
g2.draw(smallSquare)
11Other Graphical Shapes
- Ellipse2D.Double
- create by specifying x, y, width and height of
the bounding box for the ellipse
x, y
width
The constructor Ellipse2D.Double circ new
Ellipse2D.Double(10, 20, 30,40)
height
12Lines, and Points
- Line2D.Double is used to draw lines between 2
specified points - Specify the x and y's of each end point or create
Point2D.Double objects. - Point2D.Double objects useful if you have lots of
lines connecting to the same point / pixel in a
drawing. - Points cannot be drawn
- To draw a point use a line with the start point
equal to the endpoint
13Colors and Font Objects
- Change colors by creating Color objects
- Color darkGreen new Color(0.0F, 0.5F, 0.0F)
- specify amount of red, green, blue present
- g2.setColor(darkGreen) /anything drawn after
this will be darkGreen/ - g2.fill(ceralBox)
- Can draw strings
- g2.drawString("Applet", 50, 100)
- Create fonts
- Font smallFont new Font("Monospaced",
Font.BOLD, 8) - g2.setFont(smallFont)
- g2.drawString("APPLET", 150, 200)