Features of Java_第1頁
Features of Java_第2頁
Features of Java_第3頁
Features of Java_第4頁
Features of Java_第5頁
已閱讀5頁,還剩31頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、1Features of JavaCS 3331Fall 20092OutlineAbstract classInterfaceApplication - animation applets3Motivation - Drawing Board4Class Shapepublic class Shape private int x, y; private Color c; public Shape(int x, int y, Color c) this.x = x; this.y = y; this.c = c; public void draw(Graphics g) /* */ publi

2、c int getX() return x; public int getY() return y; public Color getColor return c; Q: What is wrong with this definition?5At Least Two Problems6Solution: Abstract Classpublic abstract class Shape private int x, y; private Color c; protected Shape(int x, int y, Color c) this.x = x; this.y = y; this.c

3、 = c; public abstract void draw(Graphics g); / no body here! public int getX() return x; public int getY() return y; public Color getColor return c; 7Abstract Classes?Classes that cant be instantiatedUsed to define common properties that are to be inherited by subclassesOften provide partial impleme

4、ntationsMay include abstract methods, methods that have no body8How Abstract Classes Solve the Problems?9In Sum, Abstract Classes Provide partial implementations to be inherited by subclassesMay include abstract methodsAre good for factoring out common properties among classes10OutlineAbstract class

5、esInterfacesApplication - animation applets11InterfacesDeclare features to be supported by classesProvide no implementationOnly allow public abstract methods and constants (public static final fields)public interface Runnable public void run();12Why Interfaces?To draw automobiles DrawingBoardCircleR

6、ectangleTriangleVehicleAutomobile0.*Shapeabstract13How to Draw Automobiles?By programming to the interface.14In Sum, Interfaces Good for establishing a well-defined boundary between modules (subsystems)Thus, make programs more reusable and maintainable15Abstract Classes vs. InterfacesPartial code vs

7、. no code at allClass vs. interface16ExerciseSeparate the display of DigitalClock to support various ways of displaying time, e.g., digital, analog, customized background, etc. Explain your design by drawing a UML class diagram.DigitalClock# timer: Timer# font: Font# color: Color+ DigitalClock(): vo

8、id+ start(): void+ stop(): void+ paint(g: Graphics): void17Applications - Animation AppletsEnhanced digital clock appletScrolling banner appletlInitial versionlDouble-buffered version18Enhanced Digital Clock AppletSetting applet parameters in the Web page 19Getting Applet Parametersimport java.awt.C

9、olor;public class DigitalClock2 extends DigitalClock public void init() String param = getParameter(“color”); if (“red”.equals(param) color = Color.RED; else if (“blue”.equals(param) color = Color.BLUE; else if (“yellow”.equals(param) color = Color.YELLOW; /* */ else color = Color.GREEN; 20Animation

10、 AppletsEnhanced digital clock appletScrolling banner appletlInitial versionlDouble-buffered version21The java.awt.Graphics ClassClass GraphicslRepresents graphics context, an abstraction of various drawing surfaces, e.g., screen, printer, off-screen image (an image stored in memory).lProvide a rich

11、 set of graphics methods. drawString() drawLine() drawArc() fillArc() drawOval() fillOval()drawPolygon() fillPolygon()drawRect() fillRect() drawRoundRect() fillRoundRect()22Graphics Class (Cont.)Other methodssetColor(color) set the current colorsetFont(font) set the current font setPaintMode() set t

12、he paint, or overwrite mode setXORMode(color) set the XOR mode getColor() get the current color getFont() get the current font getFontMetrics() get the font metrics of the current font getFontMetrics(font) get the font metrics for the specified font 23The java.awt.FontMetrics ClassgetAscent() getDes

13、cent() getHeight() getLeading() stringWidth(s) Upwidthheightleadingascentdescentbaseline24Scrolling Banner Appletimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class ScrollingBanner extends java.applet.Applet public void init() . public void paint(Graphics g) . public void sta

14、rt() . public void stop() . 25Field Declarationsprotected String text; protected Font font = new java.awt.Font(Sans-serif, Font.BOLD, 24); protected Dimension dim; protected int x, y; protected int delay = 100; protected int offset = 1;protected Timer timer; / animation timer26Initializationpublic v

15、oid init() / get parameters delay and text String att = getParameter(delay); if (att != null) delay = Integer.parseInt(att); att = getParameter(text); if (att != null) text = att; else text = “Go Miners!”; / set initial position of the text dim = getSize(); x = dim.width; y = font.getSize();/ initia

16、lize animation timertimer = new Timer(delay, new ActionListener() public void actionPerformed() repaint(); );27Painting the Current FrameGo Miners!Go Miners!Go Miners!lengthviewing area(dim.width, y)(dim.width-1, y)(-length, y)(x, y)(0, 0)leftmost positionrightmost positioncurrent position28Painting

17、 the Current Frame (Cont.)public void paint(Graphics g) / get the font metrics to determine the length of the text g.setFont(font); FontMetrics fm = g.getFontMetrics(); int length = fm.stringWidth(text); / adjust the position of text from the previous frame x = x - offset; / if the text is completel

18、y off to the left end / move the position back to the right end if (x -length) x = dim.width; / set the pen color and draw the background g.setColor(Color.BLACK); g.fillRect(0, 0, dim.width, dim.height); / set the pen color, then draw the text g.setColor(Color.GREEN); g.drawString(text, x, y); 29The

19、 start() and stop() Methodspublic void start() timer.start(); public void stop() timer.stop(); 30ExerciseDefine a subclass of ScrollingBanner, called ScrollingBanner3, that scrolls the banner vertically. Reuse code as much as possible and minimize code duplication.31How to Avoid Flickering?Flickerin

20、g is caused by repaint() lrepaint() calls the update() method. lThe default update() method does the following: paint the whole area with the background color; set the foreground color; call the paint() method. lThe update() method is also called by the system to update windows. Solution: loverride

21、the update() method luse an off-screen image 32Using Off-Screen ImageDouble bufferingimport java.awt.*; public class ScrollingBanner2 extends ScrollingBanner protected Image image; / off-screen image protected Graphics offscreen; / off-screen graphics public update(Graphics g) . public paint(Graphic

22、s g) . 33Using Off-Screen Image (Cont.)public void update(Graphics g) / create the offscreen image if it is the first time if (image = null) image = createImage(dim.width, dim.height); offscreen = image.getGraphics(); / draw the current frame into the off-screen image / using the paint method of the superclass super.paint(offscreen); / copy the off-screen image to the screen g.drawImage(image, 0, 0, t

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論