Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Painting something on a JPanel  (Read 1463 times)
0 Members and 1 Guest are viewing this topic.
Offline Giovanni

Junior Member




chop!


« Posted 2012-02-19 13:33:32 »

I was trying to paint an image on my JPanel (which is placed on a JFrame). I have created the panel & frame with this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
JFrame frame = new JFrame("Game");
panel = (JPanel)frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(800,600));
panel.setLayout(null);
panel.add(this);
setIgnoreRepaint(true);
     
frame.pack();
frame.setResizable(false);
frame.setVisible(true);


To be able to paint something on the screen I have decided to create a mehtod called getGraphics, which will return me the graphics from my JPanel. To load the images I am using this code:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
Image background;
   
public void loadImages()
{
   try {
      background = ImageIO.read(new File("background.jpg"));
      System.out.printf("[IMAGE] %dx%d",background.getHeight(null),background.getWidth(null));
   } catch (IOException e) {
      e.printStackTrace();
   }
}


Just to make sure that it has worked fine, I am giving the dimension of my image out to the console after loading it.

To paint that all on the screen I am using this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Panel extends JComponent
{
   @Override
   public void paint(Graphics g)
   {
      TextureEngine te = new TextureEngine();
     
      g.drawImage(te.background,60,60,null);
   }
}



If I for example draw a line on my JPanel, everything works fine. But if I try to draw an image on it, nothing happens. What am I doing wrong here?
Offline evilfrenchguy

Junior Member


Medals: 3



« Reply #1 - Posted 2012-02-19 14:31:49 »

Can I see what TextureEngine does?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-02-19 15:09:55 »

Don't create a new TextureEngine object every time paint is called. Make it a class variable inside your Panel class.

Is loadImages ever called?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Giovanni

Junior Member




chop!


« Reply #3 - Posted 2012-02-19 19:27:42 »

Can I see what TextureEngine does?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class TextureEngine
{
   Image background;
   
   public void loadImages()
   {
      try {
         background = ImageIO.read(new File("background.jpg"));
         System.out.printf("[IMAGE] %dx%d",background.getHeight(null),background.getWidth(null));
         
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}




Don't create a new TextureEngine object every time paint is called. Make it a class variable inside your Panel class.

Is loadImages ever called?

Ah yeah, right, it would be pretty stupid to create it on that way I guess. Fixed.
And yes, loadImages gets called:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public static void main(String[] args)
{
   Game g = new Game();
   TextureEngine te = new TextureEngine();
   g.running = true;
   te.loadImages();
   g.run();
   
}


(From the main class called 'Game')

This is my run method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public void run()
{
   Panel p = new Panel();
   while(running)
   {
      //Render our Panel
     p.paint(getGraphics());
   }
}


Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-02-19 19:59:33 »

No you never do call loadImages, the TextureEngine instance in your main method and the one in your Panel are completely different Wink

Offline evilfrenchguy

Junior Member


Medals: 3



« Reply #5 - Posted 2012-02-19 20:22:30 »

You're declaring the TextureEngine variable locally in main(), so Game and Panel don't see it. Try reorganizing your code a little so that the te variable can be used in your Panel's paint() method.  Smiley
Offline StumpyStrust
« Reply #6 - Posted 2012-02-19 20:49:43 »

Well if your calling everything from the main 'static' context then I think all the methods have to be static...at least the ones in that class.

Also, if you want to use any variables in a different class (your JPanel) they need to be declared outside of any method.

1  
2  
3  
4  
public void setStuff()
{
     int myInt =  0;
}


nothing outside of that method can use that int.

1  
2  
3  
4  
5  
6  
private int myInt;

public void setStuff()
{
     myInt = 0;
}


Now you can use that int outside of the method.

Offline Giovanni

Junior Member




chop!


« Reply #7 - Posted 2012-02-19 21:42:40 »

Wow thank you so much! I just had to change a few lines to fix everything.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (19 views)
2014-09-19 03:14:18

Dwinin (34 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (87 views)
2014-09-10 06:39:09

Tekkerue (42 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!