Due: Wednesday, June 18
Goal: More inheritance
This drill is mostly a walk-through to illustrate inheritance in a graphical format. The zip file below contains a NetBeans project that draws a small rectangle and bounces it off the sides of the screen. It does this for three rectangles (Drawable objects).
Unzip the file and extract it to your "Documents" folder (or some other location where you have write access). Start up NetBeans and open the project. If you don't want to use NetBeans then you'll have to extract the files from the src folder. By default, NetBeans puts everything into a package, so you'll have to either use the package or delete the "package" line at the top of each file if you want them to be in the default package.The GraphicsLab class is the main class and it creates a JFrame window. You don't need to edit this file or really need to know how it works, but look it over to see what is in it.
The GraphicsPanel class extends a JPanel, which is just used in this project to paint onto the JFrame. A JPanel is typically used as a container for GUI widgets like buttons, textboxes, etc. This class creates an array of "Drawable" objects and draws them up the screen. It does so using a timer, which calls the "actionPerformed" method every 5 ms. The actionPerformed method calls "repaint" which forces the "paint" method to be called. In the paint method, the program draws each Drawable object in the array by calling its paint method.
Study the Drawable class. The Drawable class selects a random x/y point for a drawable object. The paint method draws a small rectangle at the x/y coordinates. The update method changes the x/y coordinate so the object will bounce off the sides of the screen.
To do:
Add the SoccerBall class below which is derived from the Drawable class. It loads an image of a soccer ball and draws it on the screen.
package graphicslab; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class SoccerBall extends Drawable { private static BufferedImage imgBall = null; public SoccerBall() { super(); try { imgBall = ImageIO.read(new File("ball.png")); } catch (Exception e) { System.out.println("Error loading ball"); } } public void paint(Graphics2D g) { g.drawImage(imgBall, x, y, null); } }
This class loads an image of a soccer ball. The paint method draws it on the screen at coordinate x,y. Things to note:
To make the soccer ball bounce around on the screen, edit GraphicsPanel.java so that one of the sprites is a new SoccerBall object instead of a Drawable object.
To do: Repeat! Try again but this time add the SolidBall class below. It is similar to SoccerBall except it draws a green circle instead of an image. Edit the GraphicsPanel.java file so one of the sprites is set to a new SolidBall instead of a Drawable.
package graphicslab; import java.awt.Color; import java.awt.Graphics2D; public class SolidBall extends Drawable { public SolidBall() { super(); } public void paint(Graphics2D g) { g.setColor(Color.GREEN); g.fillOval(x, y, 30, 30); } }
The program should bounce around a rectangle, solid green ball, and a soccer ball. Note that with the SolidBall we have inherited the x/y variables, the update method, and we overrode the paint method to draw a circle instead of a rectangle.