Final Exam - Trimester 3
PLEASE NOTE: The instructions are in gold!
1. Please create a new class, drawPhoto, and paste this code into the new class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class drawPhoto extends JFrame
{
static final long serialVersionUID=0;
Image image;
drawPhoto()
{
super("Game Skeleton");
JPanel p = new JPanel();
image = readImage("hero.png");
this.setSize(600, 600);
this.setVisible(true);
p.requestFocus();
}
public static Image readImage(String imageName)
{
Image i = Toolkit.getDefaultToolkit().getImage(imageName);
MediaTracker imageTracker = new MediaTracker(new JPanel());
imageTracker.addImage(i, 0);
try
{
imageTracker.waitForID(0);
}
catch(InterruptedException e)
{
return null;
}
return i;
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillRect(0,0,600,600);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(image,10,30, this);
}
public static void main(String args[])
{
drawPhoto app = new drawPhoto();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class drawPhoto extends JFrame
{
static final long serialVersionUID=0;
Image image;
drawPhoto()
{
super("Game Skeleton");
JPanel p = new JPanel();
image = readImage("hero.png");
this.setSize(600, 600);
this.setVisible(true);
p.requestFocus();
}
public static Image readImage(String imageName)
{
Image i = Toolkit.getDefaultToolkit().getImage(imageName);
MediaTracker imageTracker = new MediaTracker(new JPanel());
imageTracker.addImage(i, 0);
try
{
imageTracker.waitForID(0);
}
catch(InterruptedException e)
{
return null;
}
return i;
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillRect(0,0,600,600);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(image,10,30, this);
}
public static void main(String args[])
{
drawPhoto app = new drawPhoto();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
2. Create a new hero image using the Microsoft Paint tool. Save the image as a png to the desktop.
3. Drag this image into eclipse and drop it INTO YOUR PROJECT NAME, not with your source code. Notice where the image is below, in the project, but not with the code.
3. Drag this image into eclipse and drop it INTO YOUR PROJECT NAME, not with your source code. Notice where the image is below, in the project, but not with the code.
4. Once you have your hero.png image stored in your project, make sure that the name you called the image matches what is in the drawPhoto program.
5. You should now be able to run the drawPhoto class and see your custom image at the top left corner.
6. Repeat steps 2 and 3 for your enemy.png image.
7. Please copy the readImage code from the drawPhoto class and place it in your hero class. Make sure that you are placing it outside the curly braces of any method. This code is highlighted in green above in the drawPhoto code. Place this same readImage code into your Enemy class.
8. For the hero class, at the top, create new variables, set to null of type Image. So for the hero class add this line: Image heroImage=null;
9. In the constructor method, that has the same name as the class, add this line:
heroImage = readImage("hero.png");
Note that my filename is 'hero.png', yours may be different!!!
10. In the hero class, in the draw method, add this line of code at the bottom of the method: g.drawImage(heroImage, x, y, null);
11. repeat steps 8, 9 and 10 for the enemy class.
5. You should now be able to run the drawPhoto class and see your custom image at the top left corner.
6. Repeat steps 2 and 3 for your enemy.png image.
7. Please copy the readImage code from the drawPhoto class and place it in your hero class. Make sure that you are placing it outside the curly braces of any method. This code is highlighted in green above in the drawPhoto code. Place this same readImage code into your Enemy class.
8. For the hero class, at the top, create new variables, set to null of type Image. So for the hero class add this line: Image heroImage=null;
9. In the constructor method, that has the same name as the class, add this line:
heroImage = readImage("hero.png");
Note that my filename is 'hero.png', yours may be different!!!
10. In the hero class, in the draw method, add this line of code at the bottom of the method: g.drawImage(heroImage, x, y, null);
11. repeat steps 8, 9 and 10 for the enemy class.