1 / 172

Introduction

Section 15.1. Introduction. Layout of Chapter 15. This final chapter is special and focuses primarily on graphics. There are no homework exercises, quizzes or tests with this chapter. However, there are 2 MAJOR Lab Projects based on the information in this chapter.

nickan
Download Presentation

Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 15.1 Introduction

  2. Layout of Chapter 15 This final chapter is special and focuses primarily on graphics. There are no homework exercises, quizzes or tests with this chapter. However, there are 2 MAJOR Lab Projects based on the information in this chapter. The first half of the chapter focuses on skills needed for the first project. The second half focuses on skills needed for the Final Project.

  3. Section 15.2 Using Mouse Events

  4. Ignore Deprecated API Warning Messages Java is constantly improving. When newer and improved classes become available, the older classes are officially labeled “deprecated”. When you use a deprecated class, you get a warning message as seen below. Warnings are not the same thing as errors. Warnings can be ignored. We are choosing to use the older technique for mouse interaction because it is simpler. The newer technique may be preferred, but it requires knowledge of AP Computer Science topics.

  5. // Java1501.java // This program counts the number of times a mouse is clicked. // Both left-clicks and right-clicks are counted using the <mouseDown> event. // Ignore the "deprecated API" warning. import java.applet.Applet; import java.awt.*; public class Java1501 extends Applet { int numClicks; public void init() { numClicks = 0; } public void paint(Graphics g) { g.drawString("Mouse is clicked " + numClicks + " times.",20,20); } public boolean mouseDown(Event e, int x, int y) { numClicks++; repaint(); return true; } } Initial Output 7 clicks later

  6. // Java1502.java // This program displays the position of the mouse every time it is clicked // using the <mouseDown> method. import java.applet.Applet; import java.awt.*; public class Java1502 extends Applet { int xCoord, yCoord; public void paint(Graphics g) { g.drawString("Mouse clicked at (" + xCoord + "," + yCoord + ")",20,20); } public boolean mouseDown(Event e, int x, int y) { xCoord = x; yCoord = y; repaint(); return true; } }

  7. // Java1503.java // This program demonstrates how to determine if the mouse is inside or // outside the Applet window using the <mouseEnter> and <mouseExit> methods. import java.applet.Applet; import java.awt.*; public class Java1503 extends Applet { booleaninsideApplet; public void paint(Graphics g) { if (insideApplet) g.drawString("Mouse is inside applet",20,20); else g.drawString("Mouse is outside applet",20,20); } public booleanmouseEnter(Event e, int x, int y) { insideApplet = true; repaint(); return true; } public booleanmouseExit(Event e, int x, int y) { insideApplet = false; repaint(); return true; } }

  8. // Java1504.java // This program determines if a mouse is clicked once or twice using the // <clickCount> method. This method works for the left or right button. boolean singleClick,doubleClick; public void paint(Graphics g) { if (singleClick) g.drawString("Mouse was clicked once",20,20); if (doubleClick) g.drawString("Mouse was clicked twice",20,20); } public boolean mouseDown(Event e, int x, int y) { switch (e.clickCount) { case 1: singleClick = true; doubleClick = false; break; case 2: doubleClick = true; singleClick = false; } repaint(); return true; } Single Click Double Click

  9. // Java1505.java // This program displays the position of the mouse every time it moves // using the <mouseMove> method. import java.applet.Applet; import java.awt.*; public class Java1505 extends Applet { intxCoord, yCoord; public void paint(Graphics g) { g.drawString("Mouse is located at (" + xCoord + "," + yCoord + ")",20,20); } public booleanmouseMove(Event e, int x, int y) { xCoord = x; yCoord = y; repaint(); return true; } }

  10. Section 15.3 Importing Images

  11. // Java1506.java // This program demonstrates how display to a single graphics image from a .gif file // at any x,y location on the screen. The same image can be displayed multiple times. import java.awt.*; public class Java1506 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"LSchram.gif"); } public void paint(Graphics g) { g.drawImage(picture,0,0,this); g.drawImage(picture,750,200,this); g.drawImage(picture,350,450,this); } }

  12. 0,0 750,200 350,450

  13. // Java1507.java // This program demonstrates that several different images can be displayed. import java.awt.*; public class Java1507 extends java.applet.Applet { Image picture1, picture2, picture3, picture4, picture5, picture6; public void init() { picture1 = getImage(getDocumentBase(),"Knight.gif"); picture2 = getImage(getDocumentBase(),"MouseWithCheese.gif"); picture3 = getImage(getDocumentBase(),"smile.gif"); picture4 = getImage(getDocumentBase(),"shark.gif"); picture5 = getImage(getDocumentBase(),"Spider.gif"); picture6 = getImage(getDocumentBase(),"TEDDY.gif"); } public void paint(Graphics g) { g.drawImage(picture1,100,100,this); g.drawImage(picture2,400,100,this); g.drawImage(picture3,700,100,this); g.drawImage(picture4,100,400,this); g.drawImage(picture5,400,400,this); g.drawImage(picture6,700,400,this); } }

  14. // Java1508.java // This program demonstrates that animated gifs are inserted in the same way as // normals gifs. Animated gifs can cause a flickering side-effect on the screen. import java.awt.*; public class Java1507 extends java.applet.Applet { Image picture1, picture2, picture3, picture4, picture5, picture6; public void init() { picture1 = getImage(getDocumentBase(),"space.gif"); picture2 = getImage(getDocumentBase(),"computer.gif"); picture3 = getImage(getDocumentBase(),"gears.gif"); picture4 = getImage(getDocumentBase(),"butterfly.gif"); picture5 = getImage(getDocumentBase(),"pizza.gif"); picture6 = getImage(getDocumentBase(),"jet.gif"); } public void paint(Graphics g) { g.drawImage(picture1,100,100,this); g.drawImage(picture2,450,100,this); g.drawImage(picture3,700,100,this); g.drawImage(picture4,100,400,this); g.drawImage(picture5,400,350,this); g.drawImage(picture6,700,400,this); } }

  15. // Java1509.java // This program demonstrates that .png files also work. import java.awt.*; public class Java1509 extends java.applet.Applet { Image picture1, picture2, picture3, picture4, picture5, picture6; public void init() { picture1 = getImage(getDocumentBase(),"Bunny.png"); picture2 = getImage(getDocumentBase(),"pineapple.png"); picture3 = getImage(getDocumentBase(),"Penguin.png"); picture4 = getImage(getDocumentBase(),"Koala.png"); picture5 = getImage(getDocumentBase(),"house.png"); picture6 = getImage(getDocumentBase(),"bird.png"); } public void paint(Graphics g) { g.drawImage(picture1,100,100,this); g.drawImage(picture2,400,100,this); g.drawImage(picture3,700,100,this); g.drawImage(picture4,100,400,this); g.drawImage(picture5,400,400,this); g.drawImage(picture6,700,400,this); } }

  16. // Java1510.java // This program demonstrates .jpg files can be used as well. // .jpg files are typically used for photographs. import java.awt.*; public class Java1510 extends java.applet.Applet { Image picture1, picture2, picture3, picture4; public void init() { picture1 = getImage(getDocumentBase(),"Paris.jpg"); picture2 = getImage(getDocumentBase(),"LetSleepingDogsLie.jpg"); picture3 = getImage(getDocumentBase(),"AllSmiling.jpg"); picture4 = getImage(getDocumentBase(),"Schrams.jpg"); } public void paint(Graphics g) { g.drawImage(picture1,0,0,this); g.drawImage(picture2,500,0,this); g.drawImage(picture3,0,330,this); g.drawImage(picture4,316,330,this); } }

  17. Section 15.4 Fixing Image Issues

  18. // Java1511.java // Problem #1 -- The image file has the wrong format. // This program demonstrates that .bmp files cannot be displayed. // Nothing will show up. // To fix this, load the bitmap file in Paint and resave it as a .gif, // .png or .jpg file. import java.awt.*; public class Java1511 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"ShortCircuit.bmp"); } public void paint(Graphics g) { g.drawImage(picture,100,100,this); } }

  19. Fixing the Problem Load ShortCircut.bmp in Microsoft Paint. If double-clicking the file does not work, right click the file, select “Open with” and then select “Paint”.

  20. Click this tab.

  21. Click “Save as”.

  22. Now select PNG, JPEG or GIF.

  23. Click Save.

  24. // Java1511.java // Problem #1 -- The image file has the wrong format. // This program demonstrates that .bmp files cannot be displayed. // Nothing will show up. // To fix this, load the bitmap file in Paint and resave it as a .gif, // .png or .jpg file. import java.awt.*; public class Java1511 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"ShortCircuit.png"); } public void paint(Graphics g) { g.drawImage(picture,100,100,this); } } Return to JCreator. Remember to alter the name of the file so it matches the name of the new file that you just created. Execute again.

  25. // Java1512.java // Problem #2 -- The image file is not in the correct location. // This program demonstrates that an image cannot be displayed if it is not in the // correct folder. When you execute the program, nothing is displayed. Move the // "DallasSkyline.png" file from the "Dallas" subfolder to the "Programs15" folder // and execute the program again. After the file is moved, the program should work. import java.awt.*; public class Java1512 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"DallasSkyline.png"); } public void paint(Graphics g) { g.drawImage(picture,0,100,this); } }

  26. Fixing the Problem The problem is that the image file is in the Dallas subfolder and not in the Programs15 folder with the rest of the files. Open the Dallas subfolder.

  27. Right-click this file and select “Copy”.

  28. Return to the Programs15 folder and paste the image file.

  29. // Java1512.java // Problem #2 -- The image file is not in the correct location. // This program demonstrates that an image cannot be displayed if it is not in the // correct folder. When you execute the program, nothing is displayed. Move the // "DallasSkyline.png" file from the "Dallas" subfolder to the "Programs15" folder // and execute the program again. After the file is moved, the program should work. import java.awt.*; public class Java1512 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"DallasSkyline.png"); } public void paint(Graphics g) { g.drawImage(picture,0,100,this); } } Return to JCreator. Execute the program again. This time no alterations to the program are necessary.

  30. Images in the Viewerare Bigger than They Appear If you double click the file castillo.JPG it might show up in a picture viewer like the one on the right. This is NOT the actual size of the picture. The true size of the picture is demonstrated in the next program. Actual Image Size: 3008 x 2000 pixels

  31. // Java1513.java // Problem #3 -- The image file is too big. // This program will be used to demonstrate what to do when a picture is too big. import java.awt.*; public class Java1513 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"castillo.jpg"); } public void paint(Graphics g) { Expo.setFont(g,"Snap ITC",Font.PLAIN,48); Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60); g.drawImage(picture,0,100,this); } }

  32. The picture is so big that all we can see is a little bit of the sky from the top-left corner. Solution #1 Resize the Image.

  33. Load the picture in Paint and click Resize.

  34. Select Pixels and enter 550 for vertical. Since “Maintain aspect ratio” is selected the Horizontal value will adjust automatically after you change the vertical. You can also change the picture size by entering a Percentage. 50 percent would make the picture have half the length and half the width.

  35. Press OK and save the resized picture. Do NOT close this file! We will use it again soon.

  36. // Java1513.java // Problem #3 -- The image file is too big. // This program will be used to demonstrate what to do when a picture is too big. import java.awt.*; public class Java1513 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"castillo.jpg"); } public void paint(Graphics g) { Expo.setFont(g,"Snap ITC",Font.PLAIN,48); Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60); g.drawImage(picture,0,100,this); } } Return to JCreator. Execute the program again. No alterations are necessary.

  37. Solution #2 – Specify Image Dimensions Return to Paint. Hit <Control>-<Z> to “Undo” the resizing. Re-save the image.

  38. // Java1513.java // Problem #3 -- The image file is too big. // This program will be used to demonstrate what to do when a picture is too big. import java.awt.*; public class Java1513 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"castillo.jpg"); } public void paint(Graphics g) { Expo.setFont(g,"Snap ITC",Font.PLAIN,48); Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60); g.drawImage(picture,0,100,752,500,this); } } Return to JCreator. Add the 2 numbers shown below to the drawImage command. Re-compile and execute. The picture will display ¼ its actual size of 3008 by 2000.

  39. Actual Size: 3008 by 2000 3008 / 4 = 752 2000 / 4 = 500 With dimensions of 752 by 500 the image is ¼ the width and ¼ the height or 1/16 the total area.

  40. // Java1513.java // Problem #3 -- The image file is too big. // This program will be used to demonstrate what to do when a picture is too big. import java.awt.*; public class Java1513 extends java.applet.Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"castillo.jpg"); } public void paint(Graphics g) { Expo.setFont(g,"Snap ITC",Font.PLAIN,48); Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60); g.drawImage(picture,0,100,376,250,this); } } Change the 2 numbers in the drawImage command. Re-compile and execute. The picture will display ⅛ its actual size of 3008 by 2000.

  41. Actual Size: 3008 by 2000 3008 / 8 = 376 2000 / 8 = 250 With dimensions of 376 by 250 the image is ⅛ the width and ⅛ the height or 1/64 the total area.

More Related