Docsity

Java Applet Graphics: Creating and Filling Shapes, Study notes of Computer Science

Toggle sidebar
bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Java Graphics
Friday, November 30, 2007
Applet graphics 21-2
Java Programs
Java programs can be applets or applications.
Most of the programs that we’ve developed this semester have been
run as applications (e.g., by pressing F2 or “Run” in Dr. Java to execute
a class’s main method).
An applet, an instance of the Applet of JApplet class, is a program that
can run inside a window (like a browser window).
When an applet is created, a main method is
not
invoked.
The browser expects an applet to respond to the following message:
paint()
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Java Applet Graphics: Creating and Filling Shapes and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Java Graphics

Friday, November 30, 2007

Applet graphics 21-

Java Programs

Java programs can be applets or applications.

Most of the programs that we’ve developed this semester have been

run as applications (e.g., by pressing F2 or “ Run ” in Dr. Java to execute

a class’s main method).

An applet , an instance of the Applet of JApplet class, is a program that

can run inside a window (like a browser window).

When an applet is created, a main method isnot invoked.

The browser expects an applet to respond to the following message:

paint()

Applet graphics 21-

import java.awt.; import javax.swing.; // for JApplet public class Check extends JApplet { public void paint (Graphics g) { g.setColor(Color.red); g.drawLine(0, 200, 200, 400); g.setColor(Color.blue); g.drawLine(200, 400, 400, 0); } }**

Check.java

Graphics object provided by Applet to which we send all drawing commands

Imagine ‘g’ is a painting machine. You’re telling it what to paint and where. It does all the work.

Check.class Check.html Check.java

Applet graphics 21-

**

**

Check.html

origin (0, 0)

positive x

positive y

400 X 400

Applet window (0,200)

(200,400)

(400,400)

Applet graphics 21-

fillRect (int x, int y, int width, int height)

400 X 400

Applet window

(0,0)

(200,100)

y

x

g.setColor(Color.blue); g.fillRect(200,100,60,200);

200

60

Applet graphics 21-

drawOval (int x, int y, int width, int height)

400 X 400

Applet window

(0,0)

(200,100)

y

x

g.setColor(Color.red); g.drawOval(200,100,60,200);

200

60

Applet graphics 21-

fillOval (int x, int y, int width, int height)

400 X 400

Applet window

(0,0)

(200,100)

y

x

g.setColor(Color.red); g.fillOval(200,100,60,200);

200

60

Applet graphics 21-

drawArc (int x, int y, int width, int height,

int startAngle, int arcAngle)

400 X 400

Applet window

(0,0)

(200,100)

y

x

g.setColor(Color.magenta); g.drawArc(200,100,60,200,0,270);

200

60

Applet graphics 21-

drawString (String str, int x, int y)

400 X 400

Applet window

(0,0)

(100,100)

y

x

g.setColor(Color.darkGray); g.drawString(“There’s no place like home!”,100,100);

There’s no place like home!

Applet graphics 21-

The Polygon class x

y

o A member of the Polygon class has multiple vertices. o We create a new Polygon with, you guessed it, a constructor, then add vertices. o Polygons don’t appear until asked.

Polygon anna = new Polygon(); anna.addPoint(0,400); anna.addPoint(200,400); anna.addPoint(200,200); g.drawPolygon(anna); g.fillPolygon(anna);

(0,0)

Creates an empty Poly

and Poly gets the point

(200,400)

(200,200)

(0,400)

Applet graphics 21-

g.setColor(Color.red); g.drawOval(100,100,50,50); // Head Polygon hat = new Polygon(); hat.addPoint(100,100); hat.addPoint(125,70); hat.addPoint(150,100); g.fillPolygon(hat); g.drawString("If I only had a brain", 80, 60); g.fillRect(100,150,50,80); // Body g.fillRect(105,230,15,50); // Leg g.fillRect(130,230,15,50); // Leg g.fillRect(75,175,100,20); // Arms

If I only had a brain

The Tin Man

Applet graphics 21-

We don’t need no stinkin’ browser!

import java.awt.*;

import javax.swing.*;

public class TinManApplication {

public static void main (String[] args) {

// Enable window decorations

JFrame.setDefaultLookAndFeelDecorated(true);

// Create and set up the window

JFrame frame = new JFrame("Portrait of a TinMan");

frame.setSize(280, 400);

// Add applet to frame

frame.add(new TinMan(), BorderLayout.CENTER);

// Display the window

frame.setVisible(true);