JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
on:
2012-01-29 16: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.
|
|
|
|
|
gbeebe
Full Member   Posts: 145 Medals: 5
|
 |
«
Reply #1 on:
2012-01-29 18:12:39 » |
|
Change: to: ...maybe? And do you have @Override above your paint mentod?
|
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #2 on:
2012-01-29 18: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 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); 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! Go get 'em!
|
|
ra4king
JGO Kernel      Posts: 3158 Medals: 196
I'm the King!
|
 |
«
Reply #3 on:
2012-01-29 18: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.
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #4 on:
2012-01-29 18: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.
|
|
|
|
|
ReBirth
JGO Wizard     Posts: 1275 Medals: 19
|
 |
«
Reply #5 on:
2012-01-29 18:47:01 » |
|
Easy solution, split again the JPanel and use Layout.
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #6 on:
2012-01-29 18:47:41 » |
|
@Above, can you elaborate more please XD.
|
|
|
|
|
|
|
ReBirth
JGO Wizard     Posts: 1275 Medals: 19
|
 |
«
Reply #8 on:
2012-01-29 18:51:51 » |
|
@OP http://docs.oracle.com/javase/tutorial/uiswing/layout/using.htmlbasiclly, 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.
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #9 on:
2012-01-29 18:56:13 » |
|
@OP http://docs.oracle.com/javase/tutorial/uiswing/layout/using.htmlbasiclly, 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! Go get 'em!
|
|
ReBirth
JGO Wizard     Posts: 1275 Medals: 19
|
 |
«
Reply #10 on:
2012-01-29 19: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).
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #11 on:
2012-01-29 19: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.
|
|
|
|
|
ReBirth
JGO Wizard     Posts: 1275 Medals: 19
|
 |
«
Reply #12 on:
2012-01-29 19:22:20 » |
|
Do you have any picture that describing what you need?
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #13 on:
2012-01-29 19:34:59 » |
|
|
|
|
|
|
ReBirth
JGO Wizard     Posts: 1275 Medals: 19
|
 |
«
Reply #14 on:
2012-01-29 19:43:35 » |
|
okay, leave the layout. use your own custom class, with this way you can set tranparency level and background image.
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #15 on:
2012-01-29 19:49:20 » |
|
Not sure I understand you can you elaborate more about the custom class?
|
|
|
|
|
ReBirth
JGO Wizard     Posts: 1275 Medals: 19
|
 |
«
Reply #16 on:
2012-01-29 20: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.
|
|
|
|
ra4king
JGO Kernel      Posts: 3158 Medals: 196
I'm the King!
|
 |
«
Reply #17 on:
2012-01-29 20:25:34 » |
|
In the end, it is best to completely avoid Swing while making games. You should create your own Button class.
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #18 on:
2012-01-29 20: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*?
|
|
|
|
|
ra4king
JGO Kernel      Posts: 3158 Medals: 196
I'm the King!
|
 |
«
Reply #19 on:
2012-01-29 20: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 
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #20 on:
2012-01-29 20: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() { } 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?
|
|
|
|
|
antelopeDJ
JGO n00b  Posts: 49
Java Developer on the Weekends!
|
 |
«
Reply #21 on:
2012-02-16 12: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.
|
|
|
|
|
ra4king
JGO Kernel      Posts: 3158 Medals: 196
I'm the King!
|
 |
«
Reply #22 on:
2012-02-16 13: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.
|
|
|
|
JayTech
Jr. Member   Posts: 59 Medals: 1
|
 |
«
Reply #23 on:
2012-02-26 14: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.
|
|
|
|
|
|