Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Review of Graphics in Java,

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Review of Graphics in Java,"— Presentation transcript:

1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Review of Graphics in Java,

2 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Applet vs Java Program  The method to start execution in an Applet is init() instead of main.  The window class inherits JApplet, instead of JFrame.  For graphics in an applet, override method paint instead of paintComponent. 2

3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Using the Graphics Class F Define a class that inherits JPanel F Override method paintComponent class MydrawPanel extends JPanel { Public void paintComponent (Graphics g) { super.paintComponent(g); g.drawLine(40,80, 60, 95); … } // end paintComponent } // end class 3

4 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Program Frame class DrawMyFrame extends JFrame { public DrawMyFrame () { … Container cpane = getContentPane(); MyDrawPanel mypanel = new MyDrawPanel(); cpane.add(mypanel); … } 4

5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Driver Class public class Mydriver { public static void main (String [] args) { JFrame myframe = new DrawMyFrame(); myframe.show(); } } // end class 5

6 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 F You can use the Image class from the java.awt package to display JPEG, PNG and GIF images (as well as BMP others). An Image object is drawn using the drawImage() Graphics class method at an (x,y) coordinate, and it can be resized as well. F In an Applet, you load an image using the getImage() and getCodeBase() methods of the JApplet or Applet class like so: Image car = getImage(getCodeBase(),"car.gif"); F In an Applet, you must load the image from the init() method: private image car; public void init() { car = getImage(getCodeBase(),"car.gif"); } Images

7 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 F If your program is a Standard Java Application and not an Applet, then you cannot use the Applet’s getImage() and getCodeBase() methods. Instead you use the getImage() method of the ImageIcon class of the java.swing package like so: Image car = new ImageIcon("car.gif").getImage(); F In an Application you don’t use the init() method to load your image, you can load it at any place in your program. F In Eclipse, images for an Application are placed inside your project folder. For an Applet images are placed inside your bin folder that is inside your project folder. You can also put them in a subfolder in those folders like so: Image car = new ImageIcon("images/car.gif").getImage(); Images

8 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 F You display the image object at position (40,20) as its top left corner in the current Graphics object (which we will call “page” but you can give it other names) using the following page.drawImage() method: page.drawImage(car,40,20,null); F To resize an image you just add the width and height dimensions page.drawImage(car,40,20,60,30,null); F This would still position the car image at (40,20) as its top left corner, but now the image would be drawn 60 pixels wide and 30 pixels high.  See program TestImage.java Positioning and Resizing Images

9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 F In an Applet or an Application, when you call the getImage() method in Java, it returns immediately, and does not wait until the image is fully loaded, F You can use the MediaTracker class of the java.awt package to wait until the image is completely loaded before attempting to draw it. car = getImage(getCodeBase(),"car.gif"); // or car = new ImageIcon("car.gif").getImage(); MediaTracker track = new MediaTracker(this); track.addImage(car,0); try { track.waitForAll(); // wait until loading is finished } catch (InterruptedException e) {} Waiting for Images to Load


Download ppt "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Review of Graphics in Java,"

Similar presentations


Ads by Google