0

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));
    }
}
Gee Boughuski
  • 53
  • 1
  • 6
  • 1
    Your new Animals class still extends Image. You have not mentioned anything about this Image class -- is it your class? Is it `java.awt.Image`? – Hovercraft Full Of Eels Apr 15 '16 at 00:38
  • 1
    `win.setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 15 '16 at 01:12
  • Sorry for the confusion, Image is a class that the professor gave us. I seemed to have worked out the problems on my own but @HovercraftFullOfEels your ideas are what gave me the impetus. Thanks – Gee Boughuski Apr 15 '16 at 01:12
  • *"..your ideas are what gave me the impetus. Thanks"* Tip: Add @HovercraftFullOfEels (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Apr 15 '16 at 01:13

1 Answers1

3

First and foremost, make sure that Animals does not extend Image. Animals is not an Image in any way shape or form and so should not extend this. Also you need to clarify just what Image class you're using. A Swing component will expect to use Images that derive from java.awt.Image, and if you have your own class named Image, and it looks like you do (e.g., birds = new Image(225,200,150,200);) rename that immediately to avoid name clashes.

Now to get the JFrame to use images, you must create images that it can use, usually a BufferedImage, usually obtained via ImageIO.read(...) -- I see no BufferedImage use in your code, so if I were you I'd start there.

It also looks like your Animals class shouldn't be extending Images of any type but rather should extend some type of Swing component since you appear to be adding it to your JFrame.

Other issues:

  • Your Driver class code uses null layout. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one. Did your teacher create this class? If so, I have to wonder if they know what they are doing.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • First off, thank you for answering, it's people like you who make this community great. After looking at your answer I edited my code a little bit to maybe make things work in the context my teacher needs them to. You said animals wast an image, but that's kinda what i need it to be. I will edit my original post to show the new code if you're willing to take a look again – Gee Boughuski Apr 15 '16 at 00:08
  • I definitely see what you mean about the null layout thing, it really does make things awkward. – Gee Boughuski Apr 15 '16 at 00:12