Note that there are some explanatory texts on larger screens.

plurals
  1. POjpanel graphics to not repaint when resized etc
    primarykey
    data
    text
    <p>Right now I have a function, in a class that is used to listen to my server, that draws my game map based off of a piped file:</p> <pre><code>String[] mapSplit = map.split("\\|"); mapWidth = Integer.parseInt(mapSplit[0]); mapHeight = Integer.parseInt(mapSplit[1]); String[] groundLayer = mapSplit[2].split(", "); String[] buildingLayer = mapSplit[3].split(", "); String[] objectLayer = mapSplit[4].split(", "); int count = 0; logger.info("Drawing started."); for (int x=0; x&lt;mapWidth; x++){ xDraw = 0; for (int y=0; y&lt;mapHeight; y++){ if (!groundLayer[count].equals("0")){ //don't draw full transparent tiles SpriteStore.get().getSprite("images/tiles/" + groundLayer[count] + ".png").draw(m.gamePanel.getGraphics(), xDraw, yDraw); } if (!buildingLayer[count].equals("0")){ //don't draw full transparent tiles SpriteStore.get().getSprite("images/tiles/" + buildingLayer[count] + ".png").draw(m.gamePanel.getGraphics(), xDraw, yDraw); } if (!objectLayer[count].equals("0")){ //don't draw full transparent tiles SpriteStore.get().getSprite("images/tiles/" + objectLayer[count] + ".png").draw(m.gamePanel.getGraphics(), xDraw, yDraw); } xDraw += 32; count++; } yDraw += 32; } logger.info("Drawing done."); </code></pre> <p>This is all fine and dandy. Right now I don't have a gameloop as I am working on a client/server protocol so it only draws once(when the character is selected and login is done). My problem comes when the jrame the jpanel sits in is resized, or when any of my jinternal frames move over the jpanel.</p> <p>When the JFrame is resized the entire map disappears.</p> <p>When the JInternalFrames are moved over the JPanel the map is removed where the overlap occurs.</p> <p>I am pretty sure this is due to a repaint being called by AWT(from some looking around I did before posting here), but I am unable to get a solution.</p> <p>Prettymuch I don't want my JPanel(map) to be cleared/erased/edited unless I choose to update it. This should include, other components being dragged on it, window being minimized, another (windows)application covering part/all of the JFrame, etc.. Any help with this would be greatly appreciated.</p> <p><img src="https://i.stack.imgur.com/Qg9Bx.png" alt="JInternalFrame obscuring the map"></p> <p>^^above is an example of what I meant by dragging a JInternalFrame removes the parts you drag over^^</p> <p><strong>EDIT</strong> I wanted to post my solution that Stefan Haustein helped me come to!!!</p> <p>in my main class I made the image: gamePanelImage = new BufferedImage(gamePanelWidth, gamePanelHeight, BufferedImage.TYPE_INT_ARGB); gamePanel = new MapGamePanel();</p> <p>in my server command listener:</p> <pre><code>logger.info("Drawing started."); for (int x=0; x&lt;mapWidth; x++){ xDraw = 0; for (int y=0; y&lt;mapHeight; y++){ //m.gamePanel.getGraphics() if (!groundLayer[count].equals("0")){ //don't draw full transparent tiles SpriteStore.get().getSprite("images/tiles/" + groundLayer[count] + ".png").draw(MyClient.getGamePanelImage(), xDraw, yDraw); } if (!buildingLayer[count].equals("0")){ //don't draw full transparent tiles SpriteStore.get().getSprite("images/tiles/" + buildingLayer[count] + ".png").draw(MyClient.getGamePanelImage(), xDraw, yDraw); } if (!objectLayer[count].equals("0")){ //don't draw full transparent tiles SpriteStore.get().getSprite("images/tiles/" + objectLayer[count] + ".png").draw(MyClient.getGamePanelImage(), xDraw, yDraw); } xDraw += 32; count++; } yDraw += 32; } logger.info("Drawing done."); </code></pre> <p>and finally the class I used to override JPanel:</p> <pre><code>package com.jayavon.game.graphics; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import javax.swing.BorderFactory; import javax.swing.JPanel; import com.jayavon.game.client.MyClient; public class MapGamePanel extends JPanel { private static final long serialVersionUID = 1L; public MapGamePanel() { super(); setLayout(new BorderLayout()); setBorder(BorderFactory.createLineBorder(Color.black)); } protected void paintComponent(Graphics g) { g.drawImage(MyClient.getGamePanelImage(), 0, 0, null); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload