2

Basically, I am making an application that represents a pokedex from the game pokemon. I am trying to add a background image, however only a small portion of the image appears on the screen. Please help :P

package window;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class RunWindow extends Canvas implements Runnable{
  private static final long serialVersionUID = 1L;
  public static final int WIDTH = 800;
  public static final int HEIGHT = 600;
public static final int SCALE = 1;
public static final String NAME = "Pokedex";
public int pokedexNum = 1;
public String pokedexName = "Bulbasaur";
private JFrame frame;
private JPanel panel = new JPanel();
private JButton b1;
private JButton b2;
private boolean update = true;
private JButton imageButton;
private ImageIcon image;
private ImageIcon image2;
private ImageIcon image3;
private ImageIcon image4;
private ImageIcon image5;
private ImageIcon image6;
private ImageIcon image7;
private ImageIcon image8;
private ImageIcon image9;
private ImageIcon image10;
private ImageIcon image11;
private ImageIcon image12;
private ImageIcon image13;
private ImageIcon image14;
private ImageIcon image15;
private ImageIcon image16;
private ImageIcon image17;
private ImageIcon image18;
private ImageIcon image19;
private ImageIcon image20;
JLabel label;
JLabel label2;
public boolean running = false;
public JButton Loading = new JButton();
public RunWindow(){

    //Pokemon image album 1 at http://imgur.com/a/qIQWh
    //Pokemon image album 2 at http://imgur.com/a/vmyT4
    do {
        switch(pokedexNum){
            case 1:
                pokedexName = "Bulbasaur";
                break;
        }
        label = new JLabel("Pokemon #"+pokedexNum+"-"+pokedexName);
        label2 = new JLabel("#"+pokedexNum);
        update = false;
    }while (update);

    label.setLocation(50, 50);
    frame = new JFrame(NAME);
    try {
        frame.setContentPane(new JLabel(new ImageIcon(new URL("http://imgur.com/Stwz5th.png"))));
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    frame.add( label, BorderLayout.NORTH );
    frame.add( panel , BorderLayout.CENTER);

    b1 = new JButton("Loading...");
    b2 = new JButton("Loading...");
    frame.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    frame.setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(Loading);
    frame.add(this,BorderLayout.CENTER);
    frame.setResizable(false);
    frame.add(label);
    frame.add(label2);
    frame.add(panel);
    frame.add(b1);
    frame.add(b2);
    frame.pack();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);

    try
    {
        image = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/hDvTgbj.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }
    try
    {
        image2 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/XjkNmIM.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image3 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/R9pYwq4.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image4 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/yQ6TRUi.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } try
    {
        image5 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/mmi8WLt.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image6 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/SqPhick.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image7 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/YRjfZQ9.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image8 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/gEjKqxQ.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image9 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/4JwkcH6.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image10 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/bCXo6Og.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image11 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/9HZYYRr.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image12 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/4O9hVAU.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image13 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/zM6gAsQ.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image14 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/WPu06mc.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image15 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/UcF9ho4.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image16 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/SqPhick.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image17 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/SqPhick.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image18 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/SqPhick.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image19 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/SqPhick.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 
    try
    {
        image20 = new ImageIcon(ImageIO.read(
                new URL("http://i.imgur.com/SqPhick.png")));
    }
    catch(MalformedURLException mue)
    {
        mue.printStackTrace();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    } 

    imageButton = new JButton("");
    imageButton.setIcon(image);



    b1.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
            if(pokedexNum < 15){
            pokedexNum++;
            switch(pokedexNum){
            case 1:
            pokedexName = "Bulbasaur";
            imageButton.setIcon(image);
            break;
            case 2:
            pokedexName = "Ivysaur";
            imageButton.setIcon(image2);
            break;
            case 3:
            pokedexName = "Venisaur";
            imageButton.setIcon(image3);
            break;
            case 4:
            pokedexName = "Charmander";
            imageButton.setIcon(image4);
            break;
            case 5:
            pokedexName = "Charmeleon";
            imageButton.setIcon(image5);
            break;
            case 6:
            pokedexName = "Charizard";
            imageButton.setIcon(image6);
            break;
            case 7:
                pokedexName="Squirtle";
                imageButton.setIcon(image7);
                break;
            case 8:
                pokedexName="Wartortle";
                imageButton.setIcon(image8);
                break;
            case 9:
                pokedexName="Blastoise";
                imageButton.setIcon(image9);
                break;
            case 10:
                pokedexName="Caterpie";
                imageButton.setIcon(image10);
                break;
            case 11:
                pokedexName="Metapod";
                imageButton.setIcon(image11);
                break;
            case 12:
                pokedexName="ButterFree";
                imageButton.setIcon(image12);
                break;
            case 13:
                pokedexName="Weedle";
                imageButton.setIcon(image13);
                break;
            case 14:
                pokedexName="Kakuna";
                imageButton.setIcon(image14);
                break;
            case 15:
                pokedexName="Beedrill";
                imageButton.setIcon(image15);
                break;
            case 16:
                pokedexName="Pidgey";
                imageButton.setIcon(image6);
                break;
            case 17:
                pokedexName="Pidgeotto";
                imageButton.setIcon(image6);
                break;
            case 18:
                pokedexName="Pidgeot";
                imageButton.setIcon(image6);
                break;
            case 19:
                pokedexName="Ratatta";
                imageButton.setIcon(image6);
                break;
            case 20:
                pokedexName="Raticate";
                imageButton.setIcon(image6);
                break;

            }
            update = false;
            label.setText("Pokemon #"+pokedexNum+"-"+pokedexName);
            label2.setText("#"+pokedexNum);
            }
            }

    } );
    b2.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) {
            if(pokedexNum > 1){
            pokedexNum--;
            switch(pokedexNum){
            case 1:
            pokedexName = "Bulbasaur";
            imageButton.setIcon(image);
            break;
            case 2:
            pokedexName = "Ivysaur";
            imageButton.setIcon(image2);
            break;
            case 3:
            pokedexName = "Venisaur";
            imageButton.setIcon(image3);
            break;
            case 4:
            pokedexName = "Charmander";
            imageButton.setIcon(image4);
            break;
            case 5:
            pokedexName = "Charmeleon";
            imageButton.setIcon(image5);
            break;
            case 6:
            pokedexName = "Charizard";
            imageButton.setIcon(image6);
            break;
            case 7:
                pokedexName="Squirtle";
                imageButton.setIcon(image7);
                break;
            case 8:
                pokedexName="Wartortle";
                imageButton.setIcon(image8);
                break;
            case 9:
                pokedexName="Blastoise";
                imageButton.setIcon(image9);
                break;
            case 10:
                pokedexName="Caterpie";
                imageButton.setIcon(image10);
                break;
            case 11:
                pokedexName="Metapod";
                imageButton.setIcon(image11);
                break;
            case 12:
                pokedexName="ButterFree";
                imageButton.setIcon(image12);
                break;
            case 13:
                pokedexName="Weedle";
                imageButton.setIcon(image13);
                break;
            case 14:
                pokedexName="Kakuna";
                imageButton.setIcon(image14);
                break;
            case 15:
                pokedexName="Beedrill";
                imageButton.setIcon(image15);
                break;
            case 16:
                pokedexName="Pidgey";
                imageButton.setIcon(image6);
                break;
            case 17:
                pokedexName="Pidgeotto";
                imageButton.setIcon(image6);
                break;
            case 18:
                pokedexName="Pidgeot";
                imageButton.setIcon(image6);
                break;
            case 19:
                pokedexName="Ratatta";
                imageButton.setIcon(image6);
                break;
            case 20:
                pokedexName="Raticate";
                imageButton.setIcon(image6);
                break;

            }
            update = false;
            label.setText("Pokemon #"+pokedexNum+"-"+pokedexName);
            label2.setText("#"+pokedexNum);

            }
            }
            } );


    b1.setBounds(300, 400, 40, 20);
    b2.setBounds(300, 420, 40, 20);

    b1.setHorizontalTextPosition(JButton.CENTER);
    b1.setVerticalTextPosition(JButton.CENTER);

    label.setBounds(300, 50, 200, 24); 
    label2.setBounds(340,400,80,20);
    frame.add(imageButton);
    imageButton.setBounds(350,300,64,64);
    frame.add( label, BorderLayout.NORTH );
    frame.add( panel , BorderLayout.CENTER);

}
public void run() {

}
public synchronized void start(){
    running = true;
    new Thread(this).start();

}

public synchronized void stop(){
    running = false;
}
public static void main(String[] args){
    new RunWindow().start();
}

}

Ayden
  • 37
  • 6
  • Ah, `JButton` is opaque...and so is `JPanel`... – MadProgrammer Mar 09 '16 at 00:27
  • I'd also have a look at [Reading/Loading an Image](http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html) and [How to Use BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html), which might help explain why some things don't show up as expected – MadProgrammer Mar 09 '16 at 00:29
  • 1) Why do you think it makes sense to post almost 600 lines of code to express "I can't load a BG image!" a [mcve] of that problem can be covered in around 50 LOC (i.e. <10% of what is shown above). 2) `label.setBounds(300, 50, 200, 24); .. frame.add( label, BorderLayout.NORTH );` layouts and 'pixel perfect positioning' don't mix. The layout will likely win. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. .. – Andrew Thompson Mar 09 '16 at 00:47
  • .. 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). 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Mar 09 '16 at 00:47

0 Answers0