Azbo
Azbo

Reputation: 1

Rendering a 2d image in java

I am fairly new to java programming and I am trying to make a simple game however, I need to render my image I have made here is the code I put in eclipse:

public class MainMenu extends JFrame{
 public static void main(String[] args){
        new MainMenu(); 

 }
 public MainMenu(){
     this.setSize(300, 450); 
        this.setLocationRelativeTo(null);     
        this.setResizable(true); 

        Toolkit tk = Toolkit.getDefaultToolkit(); 

        Dimension dim = tk.getScreenSize(); 

        int xPos = (dim.width / 2) - (this.getWidth() / 2);
        int yPos = (dim.height / 2) - (this.getHeight() / 2);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLocation(xPos, yPos); 

        this.setTitle("Frame For Flamedash Testing");

        this.setVisible(true); 
 }
 public void paint(Graphics g){   
        Image img1 = Toolkit.getDefaultToolkit().getImage("mainmenuscreen.png");
        g.drawImage(img1, 20, 20, null);
 } // public void paint(Graphics g)

 public void putit() {
     boolean MainMenu = true; 

     while (MainMenu == true){
         repaint(); 
     }

}
}

When I put that it is all fine and it does not give me any errors what so ever then I run it and it shows the frame but shows no picture than I full screen it because I set re-sizable true and it shows the default color in one spot with the size I set the frame too and another one identical but on the corner and black everywhere else. Any help to rendering this .png image will help, thank you in advance.

Upvotes: 0

Views: 158

Answers (3)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

You've got several problems in that code including:

  • Drawing directly within the JFrame, something that can have unpleasant side effects
  • Trying to read in an image from within a painting method.

Your actual problem is often due to specifying the wrong path to the image, and to solve this you need to identify the actual user's directory that Java is looking at as the base of the path. This can be found by calling

System.out.println(System.getProperty("user.dir"));

Upvotes: 1

camickr
camickr

Reputation: 324207

Don't override paint() in a JFrame.

The easiest way to display an image is to use a JLabel with an Icon.

Read the section from the Swing tutorial on How to Use Icons for more information and examples. The tutorial will show you how to better structure your code.

The tutorial also has a section on Custom Painting, if you really want to paint the image yourself. Custom painting is done by overriding the paintComponent() of a JPanel and then you add the panel to the frame.

Upvotes: 4

Jean-François Savard
Jean-François Savard

Reputation: 21004

First of all, in your paint method you should always call super.paint, else you break the paint chaining.

As for your issue,

You can call the repaint to force the painting. Keep a variable to hold the instance in your main and call repaint :

public MainMenu() {
    ...

    repaint();
}

Note that loading an image from the paint method is a really bad idea as it will be loaded each time the frame is painted.

Upvotes: 2

Related Questions