Java Applet Graphics in CS111 Computer Programming Course - Prof. Orit Shaer, Study notes of Computer Science

A series of slides from wellesley college's cs111 computer programming course, focusing on applet graphics using the japplet class. It covers applet messages, the graphics class, drawing strings, lines, rectangles, ovals, and polygons, and creating a simple gui with buttons. The document also includes an example of an applet running as an application outside of a browser.

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-fa8-2
koofers-user-fa8-2 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
GUIs
Graphical User Interfaces
Applet graphics 21-2
What’s an Applet?
A program that can run inside a window (like a browser window), an
instance of the Applet or JApplet class.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Applet Graphics in CS111 Computer Programming Course - Prof. Orit Shaer and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

GUIs

Graphical User Interfaces

Applet graphics 21- 2

What’s an Applet?

A program that can run inside a window (like a browser window), an

instance of the Applet or JApplet class.

Applet graphics 21- 3 What’s an Applet?

A program that can run inside a window (like a browser window), an

instance of the Applet or JApplet class.

The browser expects an applet to respond to these messages:

init()

start()

paint()

stop()

destroy()

Applet graphics 21- 4 import java.awt.; import javax.swing.; // for JApplet public class Hello extends JApplet { public void paint (Graphics g) { g.drawString(“Hello CS111”, 25 , 25 ); g.drawString(“Computer science rocks!”, 150 , 25 ); g.drawString(“I love programming.”, 25 , 50 ); } }** Hello.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.

Applet graphics 21- 7 **

** The results origin (0, 0) positive x positive y

400 X 400

Applet window Hello CS111. Computer science rocks! I love programming. Applet graphics 21- 8 Graphics contract (partial) public void drawString (String str, int x, int y) Draws the text given by the specified string, using this graphics context's current font and color. public void setColor (Color c) Sets this graphics context's current color to the specified color. public void drawLine (int x1, int y1, int x2, int y2) Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system. public void drawRect (int x, int y, int width, int height) Draws the outline of the specified rectangle. public void fillRect (int x, int y, int width, int height) Fills the specified rectangle. public void drawOval (int x, int y, int width, int height) Draws the outline of an oval. public void fillOval (int x, int y, int width, int height) Fills the specified oval.

Applet graphics 21- 9 drawLine(int x1, int y1, int x2, int y2) 400 X 400 Applet window (0,0) (0,200) y x g.setColor(Color.red); g.drawLine(0,200,200,400); (200,400) Applet graphics 21- 10 Look familiar? (0,0) (0,200) g.setColor(Color.red); g.drawLine(0,200,200,400); g.setColor(Color.blue); g.drawLine(200,400,400,0); (200,400) y x 400 X 400 Applet window

Applet graphics 21- 13 drawOval (int x, int y, int width, int height) 400 X 400 Applet window (0,0) (100,200) y x g.setColor(Color.red); g.drawOval(100,200,60,200); 200 60 Applet graphics 21- 14 The Polygon class (200,200) 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); (200,400) (0,0) Creates an empty Poly and Poly gets the point (0,400)

Applet graphics 21- 15 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 The Tin Man If I only had a brain *See notes page for GraphicsDemo.java, Circles.java, and Ovals.java Applet graphics 21- 16 Getting gui

import java.awt.*;

import javax.swing.*;

public class SimpleButton extends JApplet

private JButton runButton;

public void init()

runButton = new JButton("Click me”);

this.getContentPane().add(runButton, ”South”);

init() is called automatically to initialize the applet

Applet graphics 21- 19 That he didn’t, didn’t already have public class TinmanButton extends JApplet implements ActionListener { private final String draw = "Draw"; private final String hide = "Hide"; private JButton drawOrHideButton; private OzCanvas ozCanvas; public void init() { drawOrHideButton = new JButton(draw); // Draw first, Hide later drawOrHideButton.addActionListener(this); // register our interest ozCanvas = new OzCanvas(); // A place to draw ozCanvas.setVisible(false); // hide for now Container content = getContentPane(); content.setLayout(new BorderLayout()); content.add(drawOrHideButton, BorderLayout.SOUTH); content.add(ozCanvas, BorderLayout.CENTER); } public void actionPerformed(ActionEvent event) { boolean newlyVisible = !ozCanvas.isVisible(); ozCanvas.setVisible(newlyVisible); drawOrHideButton.setLabel( newlyVisible? hide : draw); } } Applet graphics 21- 20 And we don’t need no stinkin’ browser! public class TinmanButton extends JApplet implements ActionListener { … as before private static JFrame appFrame; // When running as application public static void main (String[] args) { appFrame = new JFrame("Frame for Tinman Applet"); TinmanButton tinman = new TinmanButton(); appFrame.setSize(725, 600); appFrame.getContentPane().add("Center", tinman); appFrame.addWindowListener(new AppFrameCloser(appFrame)); tinman.init(); appFrame.show(); } } class AppFrameCloser extends WindowAdapter { private JFrame frame; public AppFrameCloser(JFrame frame) { this.frame = frame; } public void windowClosing(WindowEvent e) { frame.dispose(); } }