I have it set up so all it does is draw a light gray background and some rectangles to create a grid. My next goal was to assign two images so I can default the grid to load as one, and then eventually add some listeners to let me change them to the other.
However, when I assign the image (png format) to the Image object through an ImageIcon (created and assigned previously and creating a new one on the spot were tested) it causes the default background color to be shown, and nothing drawn.
This occurs no matter where I move the assignments to except if I put the assignment into the draw method itself.
The image that I'm trying to use works fine, I pulled it straight from a previously working project, the location hadn't changed. But to be sure, I copied the address and put it in again.
Here is the code (The assignment causing the issue is in Block):
Main:
package BLURGpackage;
import javax.swing.JFrame;
public class Main extends JFrame
{
private final int FRAME_WIDTH = 1200, FRAME_HEIGHT = 1034; // add 34 onto height
//to show all of the panel
public Main()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
Panel panel = new Panel();
add(panel);
}
public static void main(String[] args)
{
Main m = new Main();
}
}
Panel:
package BLURGpackage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Panel extends JPanel implements Runnable
{
public final static int PANEL_WIDTH = 1200, PANEL_HEIGHT = 1000;
private Dimension panelDimension = new Dimension(PANEL_WIDTH, PANEL_HEIGHT);
private Thread panelThread = null;
private boolean threadRunning = false;
private Image dbi = null;
private Graphics dbg = null;
Grid grid;
private int mouseX = 0, mouseY = 0;
// ============ CHANGE THESE TO CHANGE THE LOCATION OF WHERE THE X AND Y COORDS ARE PRINTED ON THE SCREEN =============
private int printXCoordsX = 10; // X-Coordinate for where to print the current mouse X location
private int printXCoordsY = 855; // Y-Coordinate for where to print the current mouse X location
private int printYCoordsX = printXCoordsX; // X-Coordinate for where to print the current mouse Y location
private int printYCoordsY = printXCoordsY + 15; // Y-Coordinate for where to print the current mouse Y location
// ====================================================================================================================
public Panel()
{
grid = new Grid();
setPreferredSize(panelDimension);
setBackground(Color.LIGHT_GRAY);
// ===== Listeners ==========
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
}
});
// ====== Start ============= (if startPanel is before listeners, dbi is null... dunno why)
startPanel();
}
private void startPanel()
{
if(panelThread == null || threadRunning == false)
{
panelThread = new Thread(this);
threadRunning = true;
panelThread.start();
}
else
System.out.println("panelThread is NOT null on startup");
}
@Override
public void run()
{
while(threadRunning)
{
update();
render();
paintScreen();
}
}
public void update()
{
// Logic here
}
public void render()
{
if(dbi == null)
{
dbi = createImage(PANEL_WIDTH, PANEL_HEIGHT);
if(dbi == null)
System.err.println("dbi is NULL");
else
{
dbg = dbi.getGraphics();
}
}
dbg.setColor(Color.LIGHT_GRAY);
dbg.fillRect(0, 0, PANEL_WIDTH, PANEL_HEIGHT);
draw(dbg);
}
public void paintScreen()
{
Graphics g;
try
{
g = this.getGraphics();
if(dbi != null && g != null)
{
g.drawImage(dbi, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}catch(Exception e)
{
e.printStackTrace();
}
}
public void draw(Graphics g)
{
g.setColor(Color.BLACK);
g.drawString("X: "+mouseX, printXCoordsX, printXCoordsY);
g.drawString("Y: "+mouseY, printYCoordsX, printYCoordsY);
// Allow the Grid to draw
grid.draw(g);
}
}
Grid:
package BLURGpackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Grid
{
// ======== CHANGE THESE VARIABLES TO CHANGE THE SIZE OF THE GRID =============
public final static int GRID_ROWS = 21, GRID_COLUMNS = 25;
private final int RECT_WIDTH = 48, RECT_HEIGHT = 40;
private final int NUM_BLOCKS = GRID_ROWS*GRID_COLUMNS;
/*public final static int GRID_ROWS = 25, GRID_COLUMNS = 25;
private final int RECT_WIDTH = Panel.PANEL_WIDTH / GRID_ROWS; // 1200 / 25 = 48 px width per rect
private final int RECT_HEIGHT = Panel.PANEL_HEIGHT / GRID_COLUMNS; // 1000 / 25 = 40 px height per rect
private final int NUM_BLOCKS = GRID_ROWS*GRID_COLUMNS;*/
// ============================================================================
private Block[] blockArray;
private int[] blockSelectArray; // numbers for which block is selected. Feed in through file reader
public Grid()
{
// ===== FOR TESTING ONLY======
blockArray = new Block[NUM_BLOCKS];
blockSelectArray = new int[NUM_BLOCKS];
for(int i=0; i < NUM_BLOCKS; i++)
{
blockSelectArray[i] = 0;
}
// ============================
loadBlocks();
}
private void loadBlocks()
{
int x, y;
x = y = 0;
for(int i = 0; i < NUM_BLOCKS; i++)
{
if(x >= Panel.PANEL_WIDTH) // if x hits right boundary, reset x and increment y by the px height of a single block
{
x = 0;
y += RECT_HEIGHT;
}
blockArray[i] = new Block(blockSelectArray[i], x, y);
x += RECT_WIDTH;
}
}
public void draw(Graphics g)
{
g.setColor(Color.BLACK);
for(int i=0;i<NUM_BLOCKS;i++)
{
g.drawRect(blockArray[i].getBlockX(), blockArray[i].getBlockY(), RECT_WIDTH, RECT_HEIGHT);
blockArray[i].draw(g);
}
}
}
Block:
package BLURGpackage;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Block
{
private Image blockImage = null;
private Image DIRT_IMG = new ImageIcon("C:/Users/Tyler/workspace/IntTut1/src/thejavahub/images/dirt.png").getImage();
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ Problem assignment
private Rectangle blockRect = null;
private boolean solid;
private final int RECT_WIDTH = Panel.PANEL_WIDTH / Grid.GRID_ROWS; // 1200 / 25 = 48 px width per rect
private final int RECT_HEIGHT = Panel.PANEL_HEIGHT / Grid.GRID_COLUMNS; // 1000 / 25 = 40 px height per rect
public Block(int blockNum, int x, int y)
{
// switch statement on blockNum for what block
switch(blockNum)
{
default:
System.out.println("Default of switch in Block constrcutor hit");
break;
case 0: // background block
solid = false;
break;
case 1: // dirt block
blockImage = DIRT_IMG; // <---- uses the assigned image --------
solid = true;
break;
}
// set the coordinates of the block
blockRect = new Rectangle(x, y, RECT_WIDTH, RECT_HEIGHT);
}
public void draw(Graphics g)
{
// draw the image here (.... g.drawImage(blockImage, blockRect.x, blockRect.y, null); )
g.drawImage(blockImage, blockRect.x, blockRect.y, null);
}
// ==== GETTERS AND SETTERS ===
public int getBlockX()
{
return this.blockRect.x;
}
public void setBlockX(int x)
{
this.blockRect.x = x;
}
public int getBlockY()
{
return this.blockRect.y;
}
public void setBlockY(int y)
{
this.blockRect.y = y;
}
public boolean isSolid() {
return solid;
}
public void setSolid(boolean solid) {
this.solid = solid;
}
}