For a project I am doing, I have to create 2 classes, one of which creates a JFrame, and the other of which contains the an image which must be added to the JFrame.
My JFrame creating class is called Driver and looks like this:
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Driver {
private JFrame win;
private Animals animals = new Animals();
private BirdButton nextBtn, enlargeBtn, shrinkBtn, moveToBtn;
private JTextField field;
public Driver() {
win = new JFrame("Angry Animal Name Game");
win.setBounds(100, 100, 600, 600);
win.setLayout(null);
win.setVisible(true);
nextBtn = new BirdButton( "NEXT", 10, 10, animals);
win.add(nextBtn, 0);
enlargeBtn = new BirdButton( "ENLARGE", 10, 60, animals);
win.add(enlargeBtn, 0);
shrinkBtn = new BirdButton( "SHRINK", 10, 110, animals);
win.add(shrinkBtn, 0);
field = new JTextField();
field.setBounds(10, 250, 100, 20);
win.add(field, 0);
moveToBtn = new BirdButton( "MOVETO", 10, 275, animals, field);
win.add(moveToBtn, 0);
win.add(animals, 0);
animals.recenter();
win.repaint();
}
}
Then I have a separate class Animals, which contains an image of some birds. Id like to get this image to appear on my JFrame but my code seems to not be working so hot. Animals looks like this:
public class Animals extends Image {
public Circle selector;
public Image birds;
public Animals(){
birds = new Image(225,200,150,200);
birds.setImage("AngryBirds.png");
selector = new Circle(70,70,birds.getX(),birds.getY());
selector.setThickness(5);
birds.add(selector,0);
}
public void recenter(){
birds.setLocation((600-birds.getWidth()/2),(600-birds.getHeight()/2));
}
}
Since Animals extends Image am I able to use the this command to make Animals the image itself or what would I have to do? I'm not supposed to modify Driver in any way.
Thanks
After some responses I edited my Animals code to be this:
public class Animals extends Image {
public Circle selector;
public Animals(){
new Image(225,200,150,200);
setImage("AngryBirds.png");
selector = new Circle(70,70,this.getX(),this.getY());
selector.setThickness(5);
this.add(selector,0);
}
public void recenter(){
this.setLocation((600-this.getWidth()/2),(600-this.getHeight()/2));
}
}