I'm painting a grid on the JPanel and trying to scroll the grid with a JScrollPane. I have a feeling the scrollpane is getting the actual size of the panel, instead of the size including what is not being shown. Something along those lines at least, I can't quite wrap my head around the problem which is why I can't figure it out.
Here's the JPanel class that actually get's painted on.
/**
* Created by GeoDoX on 2015-03-16.
*/
public class GridPanel extends JPanel
{
private Tile[][] tiles;
private int width, height;
private double scale;
public GridPanel(int width, int height)
{
this.width = width;
this.height = height;
scale = 1;
tiles = new Tile[width][height];
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i < height; i++)
{
g.drawLine(0, i * (int) (TILE_VIEW_SIZE_DEFAULT * scale), width * (int) (TILE_VIEW_SIZE_DEFAULT * scale), i * (int) (TILE_VIEW_SIZE_DEFAULT * scale));
}
//System.out.println((int) (TILE_VIEW_SIZE_DEFAULT * scale));
for(int j = 0; j < width; j++)
{
g.drawLine(j * (int) (TILE_VIEW_SIZE_DEFAULT * scale), 0, j * (int) (TILE_VIEW_SIZE_DEFAULT * scale), height * (int) (TILE_VIEW_SIZE_DEFAULT * scale));
}
}
public void paintTile(Tile t, int posX, int posY)
{
tiles[posX][posY] = t;
this.getGraphics().drawImage(t.getTexture(), posX * (int) (TILE_VIEW_SIZE_DEFAULT * scale), posY * (int) (TILE_VIEW_SIZE_DEFAULT * scale), (int) (t.getTexture().getWidth() * scale), (int) (t.getTexture().getHeight() * scale), null);
}
public void fill(Tile t)
{
for(int i = 0; i < tiles.length; i++)
{
for(int j = 0; j < tiles[0].length; j++)
{
paintTile(t, i, j);
}
}
}
public void setScale(double scale)
{
this.scale = scale;
System.out.println(scale);
this.repaint();
}
public double getScale() { return scale; }
public void updateSize()
{
tiles = new Tile[width][height];
}
public void setWidthAndHeight(int width, int height)
{
this.width = width;
this.height = height;
this.repaint();
}
public void setTiles(Tile[][] tiles)
{
this.tiles = tiles;
this.width = tiles.length;
this.height = tiles[0].length;
drawTiles();
}
private void drawTiles()
{
for(int i = 0; i < tiles.length; i++)
{
for(int j = 0; j < tiles[0].length; j++)
{
this.getGraphics().drawImage(tiles[i][j].getTexture(), i * TILE_VIEW_SIZE_DEFAULT, j * TILE_VIEW_SIZE_DEFAULT, tiles[i][j].getTexture().getWidth(), tiles[i][j].getTexture().getHeight(), null);
}
}
}
public Tile[][] getTiles() { return tiles; }
}
This is the initialization of the ScrollPane, and where it get's added to the GUI.
gridScrollPane = new JScrollPane(gridPanel);
gridScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
gridScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tilePanel, gridScrollPane);
PS: Is there a better way to code the Fill method in GridPanel class? The process is SUPER slow on maps that are max size, 10000 x 10000.