You have already seen how to build up simple images by drawing lines and shapes. Complex images, such as photographs, are usually generated externally, for example, with a scanner or special image-manipulation software. (As you will see in Volume 2, it is also possible to produce an image, pixel by pixel, and store the result in an array. This procedure is common for fractal images, for example.) Once images are stored in local files or someplace on the Internet, you can read them into a Java application and display them on Graphics objects. As of JDK 1.4, reading an image is very simple. If the image is stored in a local file, call String filename = "..."; Image image = ImageIO.read(new File(filename)); Otherwise, you can supply a URL: String urlname = "..."; Image image = ImageIO.read(new URL(urlname)); The read method throws an IOException if the image is not available. We discuss the general topic of exception handling in Chapter 11. For now, our sample program just catches that exception and prints a stack trace if it occurs. Now the variable image contains a reference to an object that encapsulates the image data. You can display the image with the drawImage method of the Graphics class. public void paintComponent(Graphics g) { . . . g.drawImage(image, x, y, null); } Example 7-7 takes this a little bit further and tiles the window with the graphics image. The result looks like the screen shown in Figure 7-15. We do the tiling in the paintComponent method. We first draw one copy of the image in the top-left corner and then use the copyArea call to copy it into the entire window: for (int i = 0; i * imageWidth <= getWidth(); i++) for (int j = 0; j * imageHeight <= getHeight(); j++) if (i + j > 0) g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight); Figure 7-15. Window with tiled graphics imageNOTE
Example 7-7 shows the full source code of the image display program. This concludes our introduction to Java graphics programming. For more advanced techniques, you can turn to the discussion about 2D graphics and image manipulation in Volume 2. Example 7-7. ImageTest.java1. import java.awt.*; 2. import java.awt.event.*; 3. import java.io.*; 4. import javax.imageio.*; 5. import javax.swing.*; 6. 7. public class ImageTest 8. { 9. public static void main(String[] args) 10. { 11. ImageFrame frame = new ImageFrame(); 12. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 13. frame.setVisible(true); 14. } 15. } 16. 17. /** 18. A frame with an image panel 19. */ 20. class ImageFrame extends JFrame 21. { 22. public ImageFrame() 23. { 24. setTitle("ImageTest"); 25. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 26. 27. // add panel to frame 28. 29. ImagePanel panel = new ImagePanel(); 30. add(panel); 31. } 32. 33. public static final int DEFAULT_WIDTH = 300; 34. public static final int DEFAULT_HEIGHT = 200; 35. } 36. 37. /** 38. A panel that displays a tiled image 39. */ 40. class ImagePanel extends JPanel 41. { 42. public ImagePanel() 43. { 44. // acquire the image 45. try 46. { 47. image = ImageIO.read(new File("blue-ball.gif")); 48. } 49. catch (IOException e) 50. { 51. e.printStackTrace(); 52. } 53. } 54. 55. public void paintComponent(Graphics g) 56. { 57. super.paintComponent(g); 58. if (image == null) return; 59. 60. int imageWidth = image.getWidth(this); 61. int imageHeight = image.getHeight(this); 62. 63. // draw the image in the upper-left corner 64. 65. g.drawImage(image, 0, 0, null); 66. // tile the image across the panel 67. 68. for (int i = 0; i * imageWidth <= getWidth(); i++) 69. for (int j = 0; j * imageHeight <= getHeight(); j++) 70. if (i + j > 0) 71. g.copyArea(0, 0, imageWidth, imageHeight, 72. i * imageWidth, j * imageHeight); 73. } 74. 75. private Image image; 76. } javax.swing.ImageIO 1.4
java.awt.Image 1.0
java.awt.Graphics 1.0
java.awt.Component 1.0
java.awt.MediaTracker 1.0
|