Results 1 to 16 of 16
  1. #1
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default Java GlassPane Works 30% Of The Time.

    70% of the time that I run my program / game. My GlassPane, wich I use as a grid to hold characters & solid objects. Does not appear on the screen. Any solutions? It worked until I increased the size of the grid from 15, 20 to 25, 30.

    Thanks,
    Alerhau

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Can you make a SSCCE and post it here to show the problem?

  3. #3
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    SSCCE Means? Like a short version of the code?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Sorry, yes that's what it means. Something small that executes and shows the problem.

  5. #5
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    I don't really know where the problem is in the code. But I can tell you short how the code / game is.

    It's basicly a giant grid, wich each of the squares in the grid has one JPanel & one JLabel wich holds the icon.

    The problem is that the GlassPane wich I have like another layer over the grid (The glass pane holds solid objects & characters wich will need transparency). The GlassPane works exactly the same as the first layer.

    So it just don't appear on the screen 70% of the time. Hope you understood the problem. Good luck with solving it, I have no idea so far. (I would also be "MORE" than happy if you could solve it).

    Thanks again,
    Alerhau

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Sorry, without code it is hard to see what is wrong.

  7. #7
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    Can I just post the entire code? And you can test it and see? It is ful with comments!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Yeah, go ahead.

  9. #9
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    I will give you the code, but it seems like some of my JPanels & JLabels just randomly dissapear.

  10. #10
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    I know it's a lot of code, but just load it into your code editor & it will be a lot easyer to understand anything + the comments.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class Engine {
    	//World Numbers & Labels That Will Hold Each Part Of The World;
    	public JPanel[][] GridHolder = new JPanel[20][30];
    	public JLabel[][] Grid       = new JLabel[20][30];
    	public int[][]    GridNumber = new int[20][30];
    	
    	public JPanel[][] OverHolder = new JPanel[20][30];
    	public JLabel[][] Over       = new JLabel[20][30];
    	public int[][]    OverNumber = new int[20][30];
    	
    	public int YPos = 10, XPos = 15;
    	
    	//Icons of the game, both liquid solid & normal;
    	public Icon[] Sprite = {new ImageIcon(getClass().getResource("img\\Air.png")), new ImageIcon(getClass().getResource("img\\Character.png")), 
    							new ImageIcon(getClass().getResource("img\\Grass.png")), new ImageIcon(getClass().getResource("img\\Sand.png")), 
    							new ImageIcon(getClass().getResource("img\\Dirt.png")), new ImageIcon(getClass().getResource("img\\Stone.png")), 
    							new ImageIcon(getClass().getResource("img\\Tree.png")), new ImageIcon(getClass().getResource("img\\Water.png")), 
    							new ImageIcon(getClass().getResource("img\\DeepWater.png"))};
    	
    	public static JFrame Frame = new JFrame();
    	
    	public static void main(String[] args) {
    		Frame.setVisible(true);
    		Frame.setResizable(false);
    		Frame.setSize(576, 408);
    		Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Engine Eng = new Engine();
    		Eng.Gui();
    	}
    	
    	public void Gui() {
    		Random Rand = new Random();
    		
    		int i = 0, x = 0, Ra;
    		
    		//First of all we have to generate the world, so that is what happens here "NORMAL GRID GENERATION";
    		
    		while(i < GridNumber.length) {
    			while(x < GridNumber[0].length) {
    				Ra = Rand.nextInt(1000);
    				if(Ra < 500) {
    					GridNumber[i][x] = 2;
    				} else {
    					GridNumber[i][x] = 3;
    				}
    				
    				x++;
    			}
    			x=0;
    			i++;
    		}
    		
    		i = 0;
    		x = 0;
    		
    		//Here we generate the Glass Panes with all of the monsters and solid objects on. Also your character "OVER GRID GENERATION";
    		
    		while(i < OverNumber.length) {
    			while(x < OverNumber[0].length) {
    				
    					OverNumber[i][x] = 0;
    				
    				x++;
    			}
    			x=0;
    			i++;
    		}
    		
    		//Well, adding the character which has the value of one;
    		OverNumber[YPos][XPos] = 1;
    		
    		i = 0;
    		x = 0;
    		
    		//Setting the layout of the frame so that it transforms into a giant grid;
    		JPanel GridPanel = new JPanel();
    		GridPanel.setLayout(new GridLayout(Grid.length, Grid[0].length, 0, 0));
    		Frame.add(GridPanel, BorderLayout.CENTER);
    		
    		//First, we have to create the Grid (JLabels that we have created), giving them a nice icon & adding them onto the screen;
    		while (i < Grid.length) {
    			while (x < Grid[0].length) {
    				Grid[i][x] = new JLabel();
    				GridHolder[i][x] = new JPanel();
    				
    				GridHolder[i][x].setLayout(new GridLayout(1, 1, 0, 0));
    				
    				Grid[i][x].setIcon(Sprite[GridNumber[i][x]]);
    				GridHolder[i][x].add(Grid[i][x]);
    				
    				GridPanel.add(GridHolder[i][x]);
    				x++;
    			}
    			
    			x=0;
    			i++;
    		}
    		
    		
    		//Creating the glass panel / pane for the character & mobs. Also for solid objects & transparency.
    		JPanel GlassPanel = (JPanel) Frame.getGlassPane();
    		GlassPanel.setVisible(true);
    		GlassPanel.setLayout(new GridLayout(Over.length, Over[0].length, 0, 0));
    		
    		x = 0;
    		i = 0;
    		
    		while(i < Over.length) {
    			while (x < Over[0].length) {
    				Over[i][x] = new JLabel();
    				OverHolder[i][x] = new JPanel();
    				
    				OverHolder[i][x].setLayout(new GridLayout(1, 1, 0, 0));
    				Over[i][x].setIcon(Sprite[OverNumber[i][x]]);
    				
    				OverHolder[i][x].add(Over[i][x]);
    				OverHolder[i][x].setOpaque(false);
    				
    				GlassPanel.add(OverHolder[i][x]);
    				
    				x++;
    			}
    			
    			x=0;
    			i++;
    		}
    		
    		Frame.addKeyListener(new KeyAdapter() {
    			public void keyPressed(KeyEvent e) {
    				
    				if(e.getKeyCode() == KeyEvent.VK_W) { //Upward movement key;
    					
    					MoveUpward();
    					
    				} else if(e.getKeyCode() == KeyEvent.VK_A) { //Left movement key;
    					
    					MoveLeft();
    					
    				} else if(e.getKeyCode() == KeyEvent.VK_D) { //Right movement key;
    					
    					MoveRight();
    					
    				} else if(e.getKeyCode() == KeyEvent.VK_S) { //Downward movement key;
    					
    					MoveDownward();
    					
    				}
    				
    			}
    		});
    		
    		
    		
    		
    	}
    	
    	//Key movements, made in different methods for the sake of space anywhere else in the code - "MOVEMENT METHODS";
    	
    	public void MoveUpward() { //The upward movement method;
    		
    		OverNumber[YPos][XPos] = 0;
    		OverNumber[YPos-1][XPos] = 1;
    		Over[YPos][XPos].setIcon(Sprite[OverNumber[YPos][XPos]]);
    		Over[YPos-1][XPos].setIcon(Sprite[OverNumber[YPos-1][XPos]]);
    		YPos -= 1;
    		
    	}
    	
    	public void MoveLeft() { //The left movement method;
    		
    		OverNumber[YPos][XPos] = 0;
    		OverNumber[YPos][XPos-1] = 1;
    		Over[YPos][XPos].setIcon(Sprite[OverNumber[YPos][XPos]]);
    		Over[YPos][XPos-1].setIcon(Sprite[OverNumber[YPos][XPos-1]]);
    		XPos -= 1;
    		
    	}
    	
    	public void MoveRight() { //The right movement method;
    		
    		OverNumber[YPos][XPos] = 0;
    		OverNumber[YPos][XPos+1] = 1;
    		Over[YPos][XPos].setIcon(Sprite[OverNumber[YPos][XPos]]);
    		Over[YPos][XPos+1].setIcon(Sprite[OverNumber[YPos][XPos+1]]);
    		XPos += 1;
    		
    	}
    	
    	public void MoveDownward() { //The downward movement method;
    		
    		OverNumber[YPos][XPos] = 0;
    		OverNumber[YPos+1][XPos] = 1;
    		Over[YPos][XPos].setIcon(Sprite[OverNumber[YPos][XPos]]);
    		Over[YPos+1][XPos].setIcon(Sprite[OverNumber[YPos+1][XPos]]);
    		YPos += 1;
    		
    	}
    }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Can you explain what I'm supposed to see?
    I get a screen covered with small images. There is a slight delay between the initial screen to the one with the images.
    Try moving the setVisible calls after where the GUI is complete.
    I am using a few of my own images repeatedly for the Sprite array.

  12. #12
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    On the normal grid (The ground) You're just supposed to see different textures like "Grass", "Sand", which I am using right now. On the over (Which is the glasspane), you're supposed to see a small character moving around.

  13. #13
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    Whooah, the thing with moving the setVisible made everything to work, thankyou very much! I did'nt know that such a small thing could do such a big difference. Can you please explain why that is making it to work?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    I think you are confusing the GUI manager. You tell it to show what is available when you call setVisible. The stuff you add after that doesn't make it to the list of what to show until a repaint is done or later???.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, you need to call setVisible(true) on the glass pane for it to be seen and block mouse input. You can find more on this here: Using a RootPane

  16. #16
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    A great thanks to all people, also, is it possible to use multiple GlassPanes? If I test without knewing I will destroy the entire code problbly.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. glasspane on main window
    By Annie George in forum NetBeans
    Replies: 3
    Last Post: 09-25-2010, 02:08 PM
  3. Relate JTextfield with glasspane to glasspane
    By finzaiko in forum AWT / Swing
    Replies: 1
    Last Post: 04-30-2009, 05:09 AM
  4. GlassPane for jsf page
    By nanaji in forum Advanced Java
    Replies: 0
    Last Post: 04-17-2009, 10:46 AM
  5. GlassPane problem
    By SwinGirl in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2007, 02:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •