![Page 1: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/1.jpg)
COP 4331 – OOD&P
Lecture 6
![Page 2: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/11.jpg)
Example Applet
• See Watch.java
![Page 12: COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083008/56649e9e5503460f94b9f9eb/html5/thumbnails/15.jpg)
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