Click here to Skip to main content
15,666,844 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am maeing a game and i am puting all of the stuff on a JLayeredPane but it is not showing hear is my code:
import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;



public class main extends JFrame {
	public static boolean getRandomBoolean(float sr) {
	       return Math.random() < sr;
	       //I tried another approaches here, still the same result
	   }
	static JPanel pand = new JPanel();
	static JLayeredPane pane = new JLayeredPane();
	static JLabel world[][] = {{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()},{new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()}};
	public main(JLabel labels[][],JPanel pand,JLayeredPane pane) {
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setLayout(null);
		int x = 0;
		int y = 0;
		setBounds(0,0,600,600);
		setResizable(false);
		for(JLabel label[] : labels) {
			for(JLabel lab : label) {
				if(x + y > 4) {
					if(getRandomBoolean((float) .25)) {
						Class<?> theClass = main.class;
				    	java.net.URL url = theClass.getResource("/res/tree.png");
				    	ImageIcon ii = new ImageIcon(url);
				    	lab.setIcon(ii);
						lab.setBounds(x*60, y*60, 60, 60);
						//bv[y][x] = t;
						pand.add(lab);
					}else {
						Class<?> theClass = main.class;
				    	java.net.URL url = theClass.getResource("/res/island.png");
				    	ImageIcon ii = new ImageIcon(url);
				    	lab.setIcon(ii);
						lab.setBounds(x*60, y*60, 60, 60);
						pand.add(lab);
					}
				}else {
					Class<?> theClass = main.class;
			    	java.net.URL url = theClass.getResource("/res/island.png");
			    	ImageIcon ii = new ImageIcon(url);
			    	lab.setIcon(ii);
					lab.setBounds(x*60, y*60, 60, 60);
					pand.add(lab);
				
				}
				x++;
			}
			x=0;
			y++;
		}
		pane.add(pand, 0, 0);
		add(pane);
	}
	
	public static void main(String[] args) {
		Class<?> theClass = main.class;
    	java.net.URL url = theClass.getResource("/res/playerA.png");
    	ImageIcon ii = new ImageIcon(url);
		JFrame main = new main(world,pand,pane);
		world[0][0].setIcon(ii);
		main.setVisible(true);

	}

}


What I have tried:

I do not know how to address this so I have not tried anything
Posted
Updated 20-Dec-22 12:19pm
Comments
Richard MacCutchan 14-Dec-22 10:43am    
Calling your class 'main' makes it somewhat confusing trying to understand the code. Also why do you have the line
Class<?> theClass = main.class;
all over the place?
gamedevgod 15-Dec-22 9:01am    
i have the line Class theClass = main.class; all over the place because i took some of the code from a older project and did not change it
Richard MacCutchan 15-Dec-22 9:21am    
So you just dump any old code there and hope it does what you want?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900