Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Code for drawing BufferedImage on a JPanel  (Read 5506 times)
0 Members and 1 Guest are viewing this topic.
Offline engrishgamer

Senior Newbie





« Posted 2008-06-11 00:02:20 »

I'm literally been looking around for this and reading tutorials for hours, nearly all day and I haven't been able to find exactly what I'm looking for. I'm trying to make my game world by using a LayeredPane with different layers representing the different graphical elements (the background, characters, etc). I initially used JLabels as components and had their icons perform this task but realized with BufferedImages and Graphics2D I could manipulate the images and so I'm trying to make the conversion.

Currently I'm running into trouble and don't really know where to begin. I've decided to draw directly onto JPanels and add those as Components to the LayeredPane but I'm running into some trouble. Specifically, I don't know all the methods needed to accomplish this. So far, all I have in my my constructor (pertaining to the BufferedImages) is:

JPanel background, character;
Graphics2D bgGraph, charGraph;
BufferedImage bg, chara; // Already contain the correct images

public mainConstructor() {

background=new JPanel();
character=new JPanel();

// In order to draw the images on their respective JPanels
bgGraph = (Graphics2D)background.getGraphics();
charGraph = (Graphics2D)character.getGraphics();

bgGraph.drawImage(bg,0,0,null);
charGraph.drawImage(chara, xpos,ypos,null);

// Other code, specifically code adding JPanels to LayeredPane
}

However this doesn't seem to work. I've looked around and there's code of people overwriting JPanels paintComponent() method and some other draw methods. I don't really know which are relevant and which other methods would be needed to allow me to repaint/draw the specific images that need updating
Offline engrishgamer

Senior Newbie





« Reply #1 - Posted 2008-06-11 02:10:00 »

Hmm, there seems to be a problem with painting on components placed on JLayeredPane's.

http://www.java-gaming.org/topics/jlayeredpane-trouble/10366/view.html

Does anyone know any alternative to JLayeredPane? Something that would allow a number of different layers (as opposed to the normal content and glass pane)?
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #2 - Posted 2008-06-11 03:13:54 »

I don't think that's the problem. The issue here is that you're trying to render directly to the panels by grabbing the graphics contexts. Instead your panels should override paintComponent() and do the rendering there. To force them to paint you'd call repaint() on some component that contains all your panels.

Although I'd have to say that using layered panes doesn't strike me like the best solution for this kind of application.

But this may be  a better question for the game design board.

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

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #3 - Posted 2008-06-11 03:19:03 »

Override the paintComponent(...) method in JPanel:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class YourPanel extends JPanel {
 
  // your other code...

  /**
   * Draws the component.
   *
   * @param g  Where to draw to.
   */

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    // do your drawing
   // e.g. - if you have an image that you want to draw...
   // this draws the image at coordinate (0, 0) = upper left corner in your panel
   Image image = ...
    g.drawImage(image, 0, 0, this);
  }
}

Offline engrishgamer

Senior Newbie





« Reply #4 - Posted 2008-06-11 04:23:03 »

I don't think that's the problem. The issue here is that you're trying to render directly to the panels by grabbing the graphics contexts. Instead your panels should override paintComponent() and do the rendering there. To force them to paint you'd call repaint() on some component that contains all your panels.

Although I'd have to say that using layered panes doesn't strike me like the best solution for this kind of application.

But this may be  a better question for the game design board.

Dmitri


Hmm? What would you suggest that would perform the same function?

Also kingaschi, what do you mean by // your other code here? Is the method override the only change I would make to my code?
Offline zammbi

JGO Coder


Medals: 4



« Reply #5 - Posted 2008-06-11 07:47:15 »

This what I use when I need a background image on a frame so this might help:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.JComponent;

   public class BackgroundPanel extends JComponent
   {
      private final  Image guiImage;
      private static final long serialVersionUID = 1L;

      public BackgroundPanel(final Image setGuiImage){
         super();
         setOpaque(false);
         guiImage = setGuiImage;
      }
      @Override
      public void paintComponent(final Graphics g)
      {
         super.paintComponent(g);
         final Dimension d = getSize();
         g.drawImage(guiImage, 0, 0, d.width, d.height, null);
      }
   }

Current project - Rename and Sort
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #6 - Posted 2008-06-11 09:14:45 »

Quote
Also kingaschi, what do you mean by // your other code here? Is the method override the only change I would make to my code?
Correct, that's the only change needed. By "//" I was referring to other code you might need such as a constructor where you probably pass in the image or create it or load it etc... Up to you if you need that.

Offline engrishgamer

Senior Newbie





« Reply #7 - Posted 2008-06-11 16:14:26 »

Ok, I managed to get it to work. Instead of using a LayeredPane I wrote my own PriorityQueue class which allowed you to enter a number to determine the priority (think Python's version) and then overrode the paintComponent method in JPanel to paint in order of priority (backgrounds have high priority, everything else lower).

But the problem here is I'm trying to use a JScrollPane to scroll as a character moves along the screen. The background is on a JPanel of the same size and the JScrollPane is just a small square that shows part of the background. The problem is that a huge gray box appears when I add the JScrollPane for some reason. Also, although the panel has the same dimensions as the background image, it is around 10 pixels wider for some reason.vLike this:



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   
public classExtendingJPanel()
   {
      // Other code
     background=new JPanel();
      background.setPreferredSize(new Dimension(bg.getWidth(),bg.getHeight()));
      background.setOpaque(false);
     
     
      window=new JScrollPane(background,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      window.setPreferredSize(new Dimension(bg.getWidth(),bg.getWidth()));
      window.setOpaque(false);
      add(window);
   }

Offline zammbi

JGO Coder


Medals: 4



« Reply #8 - Posted 2008-06-11 16:30:40 »

Does this remove the gray square?
1  
window.getViewport().setOpaque(false);


Current project - Rename and Sort
Offline engrishgamer

Senior Newbie





« Reply #9 - Posted 2008-06-11 17:36:33 »

No, it doesn't. I really don't know what could be causing it...nothing is opaque...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #10 - Posted 2008-06-11 20:56:27 »

So, what are you expecting to see behind the JScrollPane? If you put the scroll pane onto a JFrame or JWindow, gray is what you probably expecting?

Offline engrishgamer

Senior Newbie





« Reply #11 - Posted 2008-06-12 00:12:58 »

So, what are you expecting to see behind the JScrollPane? If you put the scroll pane onto a JFrame or JWindow, gray is what you probably expecting?

That was it! I made the mistake of making an empty JPanel, simply to get the window, without painting anything on it while the actual images were painted on the JPanel it was added to, that's why they appeared in the background. I changed it to paint directly onto that JPanel and add that to a JFrame and it worked perfectly. Now if only I could get my collision detection to work (for some reason objects stop when they collide but if I continue to hold the direction they clip through each other)
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.

CogWheelz (15 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (16 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!