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

15
COP 4331 – OOD&P Lecture 6

Upload: lee-ferguson

Post on 12-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

COP 4331 – OOD&P

Lecture 6

Page 2: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

Review

• Midterm Review

• Complete sample application– See SwingColorTest.java

Page 3: 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

Page 4: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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

Page 5: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

Graphics2D Coordinates

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

Page 6: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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

Page 7: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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.

Page 8: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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.

Page 9: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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

Page 10: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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

Page 11: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

Example Applet

• See Watch.java

Page 12: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.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>

Page 13: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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

Page 14: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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

Page 15: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java

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