1

The background I have made is not showing up, which is a big problem for me. Heres the code:

//Package and imports...

public class startScreen extends JFrame
{
    public static asteriod asteriod = new asteriod();
    static JFrame Screen = new JFrame("Stars");
    static boolean isShown = (boolean) Screen.isVisible();
    static heart heart = new heart();
    public static JPanel jpanel = new JPanel(); 
    static Random rand = new Random();
    static int suptogus = rand.nextInt(20)+1;
    static enemy enemy = new enemy();
    static player player = new player();
    static JLabel creatoranddate = new JLabel("Copyright 2013© All rights reserved.");
    public static int HEALTH = 100;
    public static int health = 50;

    public startScreen() 
    {
        super("Stars");
        setLayout(new FlowLayout());
        Screen.setContentPane(new JPanel() 
        {
            Image image = ImageIO.read(new File("src/stars/optionsBackground.png"));
            public void paintComponent(Graphics g) 
            {
                 super.paintComponent(g);
                 g.drawImage(image, 0, 0, 700, 600, this);
            }
        });
        creatoranddate.setFont(new Font("Arial", Font.BOLD, 14));
        creatoranddate.setForeground(Color.WHITE.brighter());
        Point location = creatoranddate.getLocation();
        System.out.println("Located JLabel, at position " + location);
        add(creatoranddate);
        //adds background picture
        try{
            //adds logo
            JLabel logo = (new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/logo.png")))));
            add(logo);
            System.out.println("Successfully added logo.\n");
        }catch(IOException e){
            System.out.println("Failed to display logo. This is prone for the Stars.jar program.\n");
            e.printStackTrace();
        }
        try{
            JLabel enemyLabel = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/enemyLabel.png"))));
            JLabel playerLabel = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/playerLabel.png"))));
            playerLabel.setForeground(null);
            enemyLabel.setForeground(null);
            enemyLabel.setBounds(0, 0, 700, 600);
            playerLabel.setBounds(20, 20, 700, 600);
            JLabel player = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/playerSprite.png"))));
            JLabel enemy = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/enemySprite.png"))));
            player.setForeground(null);
            enemy.setOpaque(true);
            player.setBounds(0, 0, 700, 600);
            enemy.setBounds(0, -32, 700, 600);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String getVersion(String str)
    {
        return str = "Version InDev 2.0";
    }

    public static void main(String[]args)
    {

    }
}

The problem is all the code that is right below the declaration of super("Stars"); to the end of the paintComponent method. I don't know whats going on, and if I even throw IOException, it still does not show the background image.

Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • 1
    Tristen: you will learn that you're far better off testing problem code in isolation in a simple program. This way you can see if the problem is with reading in the image file or one of rending. So I suggest you do some debugging by dividing and conquering. – Hovercraft Full Of Eels Jul 03 '13 at 00:53
  • 1
    Use standard Java naming conventions. Your code is too hard to read. Class names should start with an upper case character. Variables names should start with a lower case character. Your code is inconsistent. You also should not be using static variables. Variable names like "HEALTH" and "health" are also confusing. Don't use case to differentiate variables. – camickr Jul 03 '13 at 03:10
  • 1
    Too true, as said in above comments. Moreover, had you tried to override the [getPreferredSize()](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getPreferredSize()), as well along with `paintComponent()`. Make it return some valid Dimension object, something like `repturn (new Dimension(300, 300));`. I guess that will do, if you had added your resources in the right sense to the project :-) [For example](http://stackoverflow.com/a/11372350/1057230) – nIcE cOw Jul 03 '13 at 07:19
  • Moreover, you are re-initializing your `JFrame` by writing `static JFrame Screen = new JFrame("Stars");`, even though your class extends `JFrame` itself. Hence you should remove that line and use `setContentPane(new JPanel() {...})`, instead of `Screen.setContentPane(new JPanel(){...})` – nIcE cOw Jul 04 '13 at 02:05

0 Answers0