stworzenie przezroczystego JPanel

0

Witam.
Posiadam kod prostej gry 2D RPG.
Działa ona w Frame (fullscreen)
Chce zrobic przezroczysty panel, ktory dodam do Frame. Na tym panelu wyswietlac sie beda grafiki (menu etc.) poniewaz wywolanie obrazu na danych x,y nie spowoduje ze zmieni on swoje polozenie jak moglo by sie to dziac po utworzeniu obrazu na Frame'ie z gra.

Utworzylem panel, jego klase, dodalem do Frame, dalem jako przezroczysty oraz widoczny i nawet probowalem wswietlac na nim obrazy ale po uruchomieniu gry w ogóle nic nie widac (jesli chodzi o panel i jego rysunki) widoczne jest tylko okienko z gra. Ponizej zamieszczam skrocony kod.

public class Game extends Canvas implements KeyListener, MouseListener {
	public Image sprite;
	public Image spriteR;
	private BufferStrategy strategy;
	Toolkit tk = Toolkit.getDefaultToolkit();
	Image inf = tk.getImage("ingame.png");
	private boolean left;
	public boolean lpm;
	public int x;
	public Ramka ramka;
	public Graphics2D g;
	public Music m;
	public Panel1 panel1;
	public Clip clip;
	public int y;
	public Image image;
	private boolean spm;
	private boolean ppm;
	private boolean right;
	public int WIDTH2;
	private boolean up;
	private boolean down;
	private Map map;
	public int[][] blockedxy = new int[WIDTH][HEIGHT];
	public int[][] data = new int[WIDTH][HEIGHT];
	public EntityPlayer player;
	public Image pretorian;
	public Image fog;
	public Color k;
	public int version = 1;
	public Point p;
	public String versionS;
	public int music = 1;
////////////////////////////


	public Game() {
//stworz obiekty frame i panel
//zainicjalizuj ramke + full screen
		Container pow = new Container();
		Frame frame = new Frame("The Last Templar");
		Panel panel1 = new Panel();
		pow.add(panel1);
				//dodaj panel1 do Frame
		//ustaw przezroczystosc
		frame.add(this);
	    frame.setUndecorated(true);
 	    frame.setResizable(false);
		GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);
		//odmaluj okno
		frame.repaint();
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
//////////////////////////////

		// add a key listener that allows us to respond to player
		// key presses. We're actually just going to set some flags
		// to indicate the current player direciton
		frame.addKeyListener(this);
		addKeyListener(this);


		// show the frame before creating the buffer strategy!
		frame.setVisible(true);

		// create the strategy used for accelerated rendering. More details
		// in the space invaders 2D tutorial
		createBufferStrategy(2);
		strategy = getBufferStrategy();

		// create our game objects, a map for the player to wander around
		// and an entity to represent out player
		map = new Map();
		player = new EntityPlayer(sprite, map, 10f, 10f);
		repaint();

		frame.add(panel1);
		panel1.setVisible(true);
		//odmaluj
		repaint();
		// start the game loop
		gameLoop();
	}
	
	
	public void gameLoop() {
		boolean gameRunning = true;
		long last = System.nanoTime();

		// keep looking while the game is running
		while (gameRunning) {
			Graphics2D g = (Graphics2D) strategy.getDrawGraphics();

			// clear the screen
		g.fillRect(0,0,10050, 10050);

			// render our game objects
			g.translate(-55,-55);
			map.paint(g);
			player.paint(g);
					// flip the buffer so we can see the rendering
			g.dispose();
			strategy.show();

			// pause a bit so that we don't choke the system
			try { Thread.sleep(10); } catch (Exception e) {};

			// calculate how long its been since we last ran the
			// game logic
			long delta = (System.nanoTime() - last) / 1000000;
			last = System.nanoTime();
			for (int i=0;i<delta / 5;i++) {
				logic(5);
			}
			// after we've run through the segments if there is anything
			// left over we update for that
			if ((delta % 5) != 0) {
				logic(delta % 3);
			}

			if (player.x >= WIDTH2) {
			}

		}
	}

	public void logic(long delta) {
		// check the keyboard and record which way the player
		// is trying to move this loop
		float dx = 0;
			float dy = 0;

		if (left) {
			dx -= 1;
		}
		if (right) {
			dx += 1;
		}
		if (up) {
			dy -= 1;

		}
		if (down) {
			dy += 1;

		}


		// if the player needs to move attempt to move the entity
		// based on the keys multiplied by the amount of time thats
		// passed
		if ((dx != 0) || (dy != 0)) {
			player.move(dx * delta * 0.003f,
						dy * delta * 0.003f);
	}
	}


	public void keyPressed(KeyEvent e) {

//Po wcisnieciu ESC wylacz gre
		if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			System.exit(0);
		}

			        if (e.getKeyCode() == KeyEvent.VK_ALT) {
                lpm = true;
                x= MouseInfo.getPointerInfo().getLocation().x;
                y= MouseInfo.getPointerInfo().getLocation().y;

                System.out.println("x: " + String.valueOf(x) + "  y: " + String.valueOf(y));
			        }
///////////////////////////////

		if (e.getKeyCode() == KeyEvent.VK_A) {
			left = true;
			System.out.print("left");

		}

				if (e.getKeyCode() == KeyEvent.VK_T && music == 1) {
					System.out.print("Sound OFF");
					music = 0;
         midiPlayer.stop();
         midiPlayer.close();
			// TO DO
				} else if (e.getKeyCode() == KeyEvent.VK_T && music == 0) {
         	         startMidi("SFX/1.mid");
         	         music = 1;
		}


		if (e.getKeyCode() == KeyEvent.VK_D) {
			right = true;
			System.out.print("right");
		}


		if (e.getKeyCode() == KeyEvent.VK_S) {
			down = true;
			System.out.print("down");
		}

				if (e.getKeyCode() == KeyEvent.VK_P) {
						try {
							//TO DO PAUZA
Thread.sleep(10000);
		System.out.print("PAUZED");
} catch(InterruptedException e1) {
}
				}
		if (e.getKeyCode() == KeyEvent.VK_W) {
			System.out.print("up");
			up = true;
		}

				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
	}
		if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
		Image spriteR = Toolkit.getDefaultToolkit().getImage("GFX/3.png");
					player = new EntityPlayer(spriteR, map, 10f, 10f);
		this.repaint();
			System.out.print("HIT!");
		}

	}

	public void keyReleased(KeyEvent e) {

		if (e.getKeyCode() == KeyEvent.VK_A) {
			left = false;
		}
		if (e.getKeyCode() == KeyEvent.VK_D) {
			right = false;
		}
		if (e.getKeyCode() == KeyEvent.VK_S) {
			down = false;
		}
			if (e.getKeyCode() == KeyEvent.VK_CONTROL) {


		}
		if (e.getKeyCode() == KeyEvent.VK_W) {
			up = false;
		}


	}
    class Panel1 extends JPanel{
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		//panel1.setOpaque(false);
		Toolkit zestaw = Toolkit.getDefaultToolkit();
		Image menu = zestaw.getImage("GFX/Interface/menu.png");
		g.drawImage(menu, 0, 0, null);
		panel1.setVisible(true);
		repaint();
}
}


	public static void main(String[] argv) {
		new Game();
	}

   private JButton b5 = new JButton("Wyjdź");
}
1

Absolutnie nigdy nie próbuj wywoływać repaint, ani setVisible z kodu paint, paintComponent, paintBorder, czy paintChildren. To co robisz powoduje nieskończoną pętlę rekurencyjną za pośrednictwem kolejki zdarzeń gui. Nie przeglądnąłem jeszcze reszty kodu, ale od tego trzeba zacząć.

1

JPanel.setOpaque(false)

1 użytkowników online, w tym zalogowanych: 0, gości: 1