cop 4331 – ood&p lecture 6. review midterm review complete sample application –see...

Post on 12-Jan-2016

214 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

COP 4331 – OOD&P

Lecture 6

Review

• Midterm Review

• Complete sample application– See SwingColorTest.java

TYJ Day 13 – Graphics

• Graphic operations are preformed on a canvas such as a JPanel

• Suppose JFrame main is the main panel

• Gain access to the current display area with code likeContainer content = main.getConentPane();– add() method can be applied to content– Can also draw on it

TYJ Day 13 – Graphics.2

• Every canvas has a – paintComponent(Graphics g) method– This method is called whenever the image

must be displayed or updated– Can be overridden to draw graphic objects– Cast g to (Graphics2D) g to apply drawing

methods

Graphics2D Coordinates

• Coordinate system– Upper left corner is 0,0– Unit is pixel

Drawing Text

• Given that comp2D is a Graphics2D object

• Use the drawString(str, x, y)– Where str is the string to display– x, y are the coordinates to start the string

Fonts

• Font class constructorFont (“font name”, Font.STYLEs, pts)

• Get information about a font with FontMetrics fm = getFontMetrics(f);– Accessors can get characteristics

getSize(), stringWidth(s), etc.

Colors

• Color class objects provide color information

new Color(r, g, b)– color values range from 0 to 255– Some color names are predefined, e.g.

Color.white, Color.red, etc.

– setBackground(c), setForeground(c)– Can also get colors.

TYJ Day 14 – Applets

• Applet class defines applets• Applet runs in a web page under browser

• Applet security– Cannot read or write files on user system– Cannot communicate with internet site other

than one that loaded them– Cannot run programs on browser’s system– Cannot load programs from user’s system

Applet Structure

• Instead of main() applet has methods that are invoked under particular situations– init() runs once when applet is loaded– start() runs each time applet starts– stop() runs when user leaves page– destroy() runs when applet is destroyed– paint(Graphics screen) runs whenever the

screen must be redrawn

• Default methods are empty

Example Applet

• See Watch.java

The <APPLET> Tag

• The applet tag specifies the location and characteristics of an applet on a web page

• <applet code=“code.class” height=“hh” width=“ww”>

alternate text

</applet>

The <APPLET> Tag.2

• Other fields– HSPACE, VSPACE specify padding around

applet– CODEBASE indicates location of applet code

if not the same as the web page

• Applet can be viewed outside a web page with appletviewer

Applets.3

• The OBJECT tag is a more general alternative to the APPLET tag

• See Watch2.html

• Java archives permit packaging all files needed for an applet into a single file

Applet Parameters

• Can pass parameters to applet with tags<PARAM NAME=“name” VALUE=“val”>– Retrieved in applet with

getParameter(“name”);

• See NewWatch.java and NewWatch.html

top related