0

Hi I am creating a small demo for a game I want to create. I want to have a picture of Mario on a black line. I have implemented the code below. The JFrame does start and I see the black line at the bottom, however the picture of Mario does not appear. I am sure the answer is obvious but help would be appreciated. I have tried adding the label to a JPanel but the result is the same.

package Game;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class world extends JFrame {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        world w = new world();
    }

    public  ImageIcon mario = new ImageIcon("D:\\Eclipse\\2DShooter\\Photos\\wall-jump.jpg");

    public world() {
        setTitle("Demo");
        setSize(800, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JLabel Mario = new JLabel();
        Mario.setIcon(mario);
        add(Mario);
        setVisible(true);
    }

    public void paint(Graphics g){

        g.setColor(Color.BLACK);
        g.fillRect(0, 460, 800, 20);
    }
}
  • Never override a JFrame's `paint` method. You're shooting yourself in the foot with this. By doing this and forgetting to call its super method, you completely wreck its functioning. – Hovercraft Full Of Eels Apr 24 '18 at 20:45
  • 1
    Also call `setVisible(true)` after adding components to the GUI, not before. – Hovercraft Full Of Eels Apr 24 '18 at 20:47
  • What method would you instead suggest then for adding an image? (sorry I am still very new to this) – Julian Young Apr 24 '18 at 20:48
  • 1
    ???? Your paint method has nothing to do with the image other than wrecking its viewing. – Hovercraft Full Of Eels Apr 24 '18 at 20:48
  • Please read the appropriate tutorials. You can find links to the Swing tutorials and to other Swing resources here: [Swing Info](http://stackoverflow.com/tags/swing/info) – Hovercraft Full Of Eels Apr 24 '18 at 20:51
  • *"What method would you instead suggest then for adding an image?"* Read the duplicate question: `paintComponent(...)` and calling `super.paintComponent(...)` as well... using a `JPanel` and not a `JFrame` as already suggested by @HovercraftFullOfEels – Frakcool Apr 24 '18 at 20:51
  • @Frakcool: the ImageIcon inside of a JLabel and then added to the JFrame should be enough to show the image, so overriding a JPanel's paintComponent is not needed here. His problems are two-fold: he's calling `setVisible(true)` **before** adding the JLabel, and he's overriding a JFrame's paint method in a bad way, preventing it from drawing appropriately. – Hovercraft Full Of Eels Apr 24 '18 at 20:52
  • You're right, I didn't notice that before @HovercraftFullOfEels :) – Frakcool Apr 24 '18 at 20:53
  • Thank you for your advice HovercraftFullOfEels and Frakcool. I have moved the setVisible(true); and will now look at some of the tutorials. – Julian Young Apr 24 '18 at 20:56
  • At some point you're also going to want to create jar files, and when you start doing that, you'll want to not get the image as a File but rather as a resource. Search on this site regarding this as well. – Hovercraft Full Of Eels Apr 24 '18 at 21:13

0 Answers0