Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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  
  JPanel Problem =/ Question(My Code Provided)  (Read 3214 times)
0 Members and 1 Guest are viewing this topic.
Offline JayTech

Junior Duke


Medals: 1



« Posted 2012-01-29 21:57:26 »

Ok Question, I have a GameBoard class(JPanel) which allows me to ingegrate w.e maps I want and w.e sprites to be loaded onto it, then this is put onto my JFrame class. For my GameBoard Class I painted a Custom image onto the JPanel by:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void paint(Graphics g) {
        super.paint(g);
       
        bg = new ImageIcon("Trees!.png").getImage();
        g.drawImage(bg, 0,0,null);  
   Graphics2D g2d = (Graphics2D)g;
        2d.drawImage(spO.getImage(), spO.getX(), spO.getY(), this);
       

       
       
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
         
}


Question is, when I try to add a JLabel or JButton onto this GameBoard It keeps getting painted behind the Image. The reason I know this is If I don't paint an Image via commenting out g.drawImage(bg, 0,0,null);  I get a regular background (blue) and my buttons and lables are there, so how do I make this Image the standard panes image?

All Help is appreciated thanks.
Offline gbeebe
« Reply #1 - Posted 2012-01-29 23:12:39 »

Change:
1  
g.dispose();

to:
1  
g2d.dispose();


...maybe?  And do you have @Override above your paint mentod?
Offline JayTech

Junior Duke


Medals: 1



« Reply #2 - Posted 2012-01-29 23:25:11 »

Tried g2d.dispose, didnt work neither did adding the @Override. Here's the entire code for this Board Class:
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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//------------
//import customframe.CustomFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JComponent;
import java.util.*;
import java.io.*;


//------------
import javax.swing.JPanel;
import javax.swing.Timer;


public class Board3 extends JPanel implements ActionListener {

    private Timer timer;
    private SpriteO spO;
   private SpriteS sp;
   
    Random randomGenerator = new Random();
   monster_elixisDragon ED = new monster_elixisDragon();  
 
     public Image bg;  
    public Board3() {
         
         
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.BLUE);
        setDoubleBuffered(true);
         setPreferredSize(new Dimension(450, 210));
          sp= new SpriteS();
   
        spO = new SpriteO();

       timer = new Timer(5, this);
     
      timer.start();
       this.add(ED.mlab);//Label I want to display
       
       repaint();  
   
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
       
        bg = new ImageIcon("Trees!.png").getImage();
        g.drawImage(bg, 0,0,this);  
        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(sp.imgArray().get(sp.getFrame()), sp.getX(), sp.getY(), this);
        g2d.drawImage(spO.getImage(), spO.getX(), spO.getY(), this);
        Toolkit.getDefaultToolkit().sync();
        g2d.dispose();
         
    }


    public void actionPerformed(ActionEvent e) {
        sp.move();
        repaint();  
         
     
    }


    private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            sp.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            sp.keyPressed(e);
        }
    }

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

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-29 23:33:47 »

You should be overriding paintComponent(Graphics g) instead. However, that will still paint over the buttons because the panel draws all children first.

I don't understand what you are trying to achieve through this. You should be drawing on a JComponent that you don't add any components to.

Offline JayTech

Junior Duke


Medals: 1



« Reply #4 - Posted 2012-01-29 23:41:25 »

I'm trying to put JButtons and JLabels on this panel and have them being painted over the background Image bg. In this class I paint sprites and the background and this gets put onto a JFrame where other UI components are.

I want to make the image bg the absolute background image instead of the panels raw color background.  Pretty much I don't want the image bg to be painting over the other components? Is what i'm trying to achieve the wrong way of doing so?

And I thought I was overriding the paint method?

Thanks in  advance =D.
Offline ReBirth
« Reply #5 - Posted 2012-01-29 23:47:01 »

Easy solution, split again the JPanel and use Layout.

Offline JayTech

Junior Duke


Medals: 1



« Reply #6 - Posted 2012-01-29 23:47:41 »

@Above, can you elaborate more please XD.
Offline gbeebe
« Reply #7 - Posted 2012-01-29 23:48:02 »

I've tried this too.  Using regular awt buttons and stuff works, but I can't get it to work with JPanel and Jbuttons.  I came across this link when searching for an answer:

http://stackoverflow.com/questions/5636170/jbutton-is-drawing-behind-an-image
Offline ReBirth
« Reply #8 - Posted 2012-01-29 23:51:51 »

@OP
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
basiclly, you define different JPanels and each one draws different thing. For your case, you need two JPanels. One to hold the swing components while other do Graphics draw. Then add both of them in single JPanel that using Layout (so you can adjust their position). The single JPanel that holds these two can be added to JFrame.

Offline JayTech

Junior Duke


Medals: 1



« Reply #9 - Posted 2012-01-29 23:56:13 »

@OP
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
basiclly, you define different JPanels and each one draws different thing. For your case, you need two JPanels. One to hold the swing components while other do Graphics draw. Then add both of them in single JPanel that using Layout (so you can adjust their position). The single JPanel that holds these two can be added to JFrame.

Interesting I'll try this, thanks. Pretty much my game is an RPG and I want some npcs to have JLabels which pop up and i'd rather use java's built in components to do so instead of drawing rectangles and such manually.
So what you're saying is the one with Sprites and Images is on one, then the swing components etc get put on a transparent JPanel which then gets added on top of the Images one?

Also Is there better industry standard for making "Buttons" and components than what I'm doing to achieve such?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #10 - Posted 2012-01-30 00:13:57 »

Oh! if you mean label or button inside game world, then I suggest you to leave swing. I thought you want to create some HUD or what. Rather use JLabel or JButton, make one by your own - a class that receive Graphics parameter to draw text and background while keep updating its position (for example, above player's head).

Offline JayTech

Junior Duke


Medals: 1



« Reply #11 - Posted 2012-01-30 00:17:38 »

Thanks yea, that is what I meant originally(thanks ill look more into that). Also, that does bring me back to a HuD, for now I have a panel that is adhesive to the main frame which has different buttons settings and has a custom painting on the panel, but to have a real HuD like what you originally thought I wanted, which I do but I coped out with a JPanel Holding buttons on the frame. TO do this like you say how do I prioritize A panel over a panel such as a HuD by which I mean fully transparent and can still allow clicks onto the main panel the game world? For example I would idealy like a HUD that hold a Portrait of the player with Health and one with action bars, and then eventually start menus and such.
Offline ReBirth
« Reply #12 - Posted 2012-01-30 00:22:20 »

Do you have any picture that describing what you need?

Offline JayTech

Junior Duke


Medals: 1



« Reply #13 - Posted 2012-01-30 00:34:59 »

Here is what I have so far..
http://s986.photobucket.com/albums/ae348/TheArasonage/?action=view&current=GamePhoto.png
Offline ReBirth
« Reply #14 - Posted 2012-01-30 00:43:35 »

okay, leave the layout. use your own custom class, with this way you can set tranparency level and background image.

Offline JayTech

Junior Duke


Medals: 1



« Reply #15 - Posted 2012-01-30 00:49:20 »

Not sure I understand you can you elaborate more about the custom class?
Offline ReBirth
« Reply #16 - Posted 2012-01-30 01:17:23 »

Let's say a custom button. So this class acts like Entity, needs to be drawed. It can have fields like background image and position (x, y). A simple method to use this class: for example check if mouse click's point happened on top of this class. If yes, tell game state to do what this class have to do.

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #17 - Posted 2012-01-30 01:25:34 »

In the end, it is best to completely avoid Swing while making games. You should create your own Button class.

Offline JayTech

Junior Duke


Medals: 1



« Reply #18 - Posted 2012-01-30 01:36:59 »

Alright thanks for the code, i'll read it over and try to understand it. So yea I guess that's what I have to learn to do is avoid Swing, and for HuDs and all GUI you would create everything from scratch like that button class?

So pretty much the only Swing Components you use are just Panels and frames just do the basics and that's it for game development for Swing*?
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #19 - Posted 2012-01-30 01:47:26 »

Yes, unless you want to use really complicated GUI components where it's less time consuming to just wrangle with Swing rather than make the component yourself.

Avoiding Swing means that you'll need to avoid classes beginning with J, except maybe for JFrame but you could substitute that with java.awt.Frame.

It is best to also draw on a java.awt.Canvas, using BufferStrategy. Google "site:java-gaming.org BufferStrategy" (without quotes) and look through posts explaining how to use it Smiley

Offline JayTech

Junior Duke


Medals: 1



« Reply #20 - Posted 2012-01-30 01:55:11 »

Alright thanks, question about the Button class.
Here's one I wrote just now that uses Java2D:
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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
public class Button {
    private boolean isDisabled, isPressed, isHovering;
    private String text;
    private Font font;
    private Color backgroundColor, hoverColor, pressedColor, textColor;
    private Rectangle2D.Double bounds;
   
    public Button(String text, Font font, int x, int y, int width, int height) {
        this.text = text;
        this.font = font;
        backgroundColor = Color.blue;
        hoverColor = Color.blue.darker();
        pressedColor = Color.blue.darker().darker();
        textColor = Color.black;
        bounds = new Rectangle2D.Double(x,y,width,height):
    }
   
    public void doAction() {
        //Called when clicked.
    }
   
    public void mousePressed(int button, int x, int y) {
        if(isHovering) {
            isHovering = false;
            isPressed = true;
        }
    }
   
    public void mouseReleased(int button, int x, int y) {
        if(bounds.contains(x,y) && isPressed)
            doAction();
       
        isPressed = false;
    }
   
    public void mouseMoved(int x, int y) {
        isHovering = false;
       
        if(bounds.contains(x,y))
            isHovering = true;
    }
   
    public void draw(Graphics2D g) {
        if(isDisabled)
            g.setColor(Color.darkGray);
        else if(isPressed)
            g.setColor(pressedColor);
        else if(isHovering)
            g.setColor(hoverColor);
        else
            g.setColor(backgroundColor);
        g.fill(bounds):
       
        FontMetrics fm = g.getFontMetrics(font);
        int width= fm.stringWidth(text);
        int height = fm.getHeight();
        int x = bounds.x+(bounds.width-width)/2;
        int y = bounds.y+(bounds.height+height)/2+fm.getDescent();
       
        g.setColor(textColor);
        g.drawString(text,x,y);
    }
}


I know this sounds stupid but I don't quite understand the mouse methods ><, where are we getting the input from?  And what is the int button for?
Offline antelopeDJ

Junior Duke


Projects: 2


Java Developer on the Weekends!


« Reply #21 - Posted 2012-02-16 17:49:10 »

public void paint(Graphics g) {
        super.paint(g);
       
        bg = new ImageIcon("Trees!.png").getImage();
        g.drawImage(bg, 0,0,null);   
   Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(spO.getImage(), spO.getX(), spO.getY(), this);
       

       
       
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
         
}

You were missing a 'g' on the object name g2d, as marked. This might be your issue.
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #22 - Posted 2012-02-16 18:41:19 »

@JayTech
Sorry for not seeing your post earlier, the input methods should be called from you mouse listeners. The into button is the button number that causes the event, MouseEvent.BUTTON1-3.

Offline JayTech

Junior Duke


Medals: 1



« Reply #23 - Posted 2012-02-26 19:51:32 »

Was on vacation(Actually didn't bring my computer) just got back onto the forums, thanks for all of the responses . ra4king ah, I gotcha makes sense now thanks =).

Edit: the 2d some how cut off the g when I pasted the code onto here.
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.

theagentd (18 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!