'Paint not showing up on JPanel? I want to paint only if if statement conditions are met
I want to paint the walls of the pacman maze for my APCS A final. I am relatively new to graphics, as you can probably tell. The paintComponent() methods don't appear to be doing anything and when I run the program I just get a black panel. So how do I get the rectangles and circles to actually show up? I want to do as little in the driver as possible and do it all in the drawMaze() method, which is called in the constructor.
Driver:
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Game {
public static void main(String[] args) {
Maze board = new Maze();
EventQueue.invokeLater(new Runnable() {
public void run()
{
board.createAndShowGUI();
}
});
}
}
Maze Class:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Maze extends JPanel
{
//boardsize in pixels
public static int BOARDWIDTH = 2700;
public static int BOARDHEIGHT = 3100;
//Level data copied from the first pacman maze
//These numbers make up the maze. 0 is an empty space, 1 is a left wall, 2 is a top wall, 4 is a right wall, 8 is a bottom wall, 16 is a pellet, and 32 is a mega pellet
//These numbers are additive to make a space on the board. IE 19 would be a top left corner with a pellet (1 + 2 + 16)
private final int [][] LEVELDATA = {{3, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 2, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 6},
{5, 19, 26, 26, 26, 26, 18, 26, 26, 26, 26, 26, 22, 5, 19, 26, 26, 26, 26, 26, 18, 26, 26, 26, 26, 22, 5},
{5, 21, 3, 2, 2, 6, 21, 3, 2, 2, 2, 6, 21, 5, 21, 3, 2, 2, 2, 6, 21, 3, 2, 2, 6, 21, 5},
{5, 37, 1, 0, 0, 4, 21, 1, 0, 0, 0, 4, 21, 5, 21, 1, 0, 0, 0, 4, 21, 1, 0, 0, 4, 37, 5},
{5, 21, 9, 8, 8, 12, 21, 9, 8, 8, 8, 12, 21, 13, 21, 9, 8, 8, 8, 12, 21, 9, 8, 8, 12, 21, 5},
{5, 17, 26, 26, 26, 26, 16, 26, 26, 18, 26, 26, 24, 26, 26, 24, 26, 24, 26, 26, 16, 26, 26, 26, 26, 20, 5},
{5, 21, 3, 2, 2, 6, 21, 3, 6, 21, 3, 2, 2, 2, 2, 2, 6, 21, 3, 6, 21, 3, 2, 2, 6, 21, 5},
{5, 21, 9, 8, 8, 12, 21, 1, 4, 21, 9, 8, 8, 0, 8, 8, 12, 21, 1, 4, 21, 9, 8, 8, 12, 21, 5},
{5, 25, 26, 26, 26, 26, 20, 1, 4, 25, 26, 26, 22, 5, 19, 26, 26, 28, 1, 4, 17, 26, 26, 26, 26, 28},
{9, 10, 10, 10, 10, 6, 21, 1, 0, 2, 2, 6, 5, 5, 5, 3, 2, 2, 0, 4, 21, 3, 10, 10, 10, 10, 12},
{2, 2, 2, 2, 6, 5, 21, 1, 0, 8, 8, 12, 5, 5, 5, 9, 8, 8, 0, 4, 21, 5, 3, 2, 2, 2, 2},
{0, 0, 0, 0, 4, 5, 21, 1, 4, 3, 10, 10, 8, 10, 8, 10, 10, 6, 1, 4, 21, 5, 1, 0, 0, 0, 0},
{8, 8, 8, 8, 12, 5, 21, 1, 4, 5, 3, 10, 10, 10, 10, 10, 6, 5, 1, 4, 21, 5, 9, 8, 8, 8, 8},
{11, 10, 10, 10, 10, 12, 21, 9, 12, 5, 5, 3, 2, 2, 2, 6, 5, 5, 9, 12, 21, 9, 10, 10, 10, 10, 15},
{10, 10, 10, 10, 10, 10, 16, 10, 10, 4, 5, 1, 0, 0, 0, 4, 5, 1, 10, 10, 16, 10, 10, 10, 10, 10, 10},
{11, 10, 10, 10, 10, 6, 21, 3, 6, 5, 5, 9, 8, 8, 8, 12, 5, 3, 6, 21, 3, 10, 10, 10, 10, 15},
{2, 2, 2, 2, 6, 5, 21, 1, 4, 5, 9, 8, 8, 8, 8, 8, 12, 5, 1, 4, 21, 5, 3, 2, 2, 2, 2},
{0, 0, 0, 0, 4, 5, 21, 1, 4, 1, 10, 10, 10, 10, 10, 10, 10, 4, 1, 4, 21, 5, 1, 0, 0, 0, 0},
{8, 8, 8, 8, 12, 5, 21, 1, 4, 5, 3, 2, 2, 2, 2, 2, 6, 5, 1, 4, 21, 5, 9, 8, 8, 8, 8},
{3, 10, 10, 10, 10, 12, 21, 9, 12, 5, 9, 8, 8, 0, 8, 8, 12, 5, 1, 4, 25, 9, 10, 10, 10, 10, 6},
{5, 19, 26, 26, 26, 26, 16, 26, 26, 24, 26, 26, 22, 5, 19, 26, 26, 24, 1, 4, 16, 26, 26, 26, 26, 22, 5},
{5, 21, 3, 2, 2, 6, 21, 3, 2, 2, 2, 6, 21, 5, 21, 3, 2, 2, 2, 6, 21, 3, 2, 2, 6, 21, 5},
{5, 21, 9, 8, 0, 4, 21, 9, 8, 8, 8, 12, 21, 13, 21, 9, 8, 8, 8, 12, 21, 1, 0, 8, 12, 21, 5},
{5, 41, 26, 22, 1, 4, 17, 26, 26, 18, 26, 26, 24, 26, 24, 26, 26, 18, 26, 26, 20, 1, 4, 19, 26, 44, 5},
{1, 2, 6, 21, 1, 4, 21, 3, 6, 21, 3, 2, 2, 2, 2, 2, 6, 21, 3, 6, 21, 1, 4, 21, 3, 2, 4},
{1, 8, 12, 21, 9, 12, 21, 1, 4, 21, 9, 8, 8, 0, 8, 8, 12, 21, 1, 4, 21, 9, 12, 21, 9, 8, 4},
{5, 19, 26, 24, 26, 26, 28, 1, 4, 25, 26, 26, 22, 5, 19, 26, 26, 28, 1, 4, 25, 26, 26, 24, 21, 22, 5},
{5, 21, 3, 2, 2, 2, 2, 0, 0, 2, 2, 6, 21, 5, 21, 3, 2, 2, 0, 0, 2, 2, 2, 26, 21, 5},
{5, 21, 9, 8, 8, 8, 8, 8, 8, 8, 8, 12, 21, 13, 21, 9, 8, 8, 8, 8, 8, 8, 8, 12, 21, 5},
{5, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 24, 26, 24, 26, 26, 26, 26, 26, 26, 26, 26, 26, 28, 5},
{9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 12}};
public Maze() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//creates a temporary array because LEVELDATA can't be modified
int[][] temp = LEVELDATA;
//Draws the maze based on the level data
for (int r = 0; r < 31; r++) {
for (int c = 0; c < 26; c++) {
final int row = r;
final int col = c;
//draws a power pellet
if(temp[r][c] - 32 >=0){
g.setColor (Color.white);
g.fillOval((row*10)-7, (col*10)-7, 5, 5);
temp[r][c] -= 32;
}
//draws a tic tac
if(temp[r][c] - 16 >=0) {
g.setColor (Color.white);
g.fillOval((row*10)-7, (col*10)-7, 1, 1);
temp[r][c] -= 16;
}
//draws a bottom wall
if(temp[r][c] - 8 >=0) {
g.setColor (Color.blue);
g.fillRect((row*10) - 10, (col*10) - 1, 10, 1);
temp[r][c] -= 8;
}
//draws a right wall
if(temp[r][c] - 4 >=0) {
g.setColor (Color.blue);
g.fillRect((row*10) - 1, (col*10) - 10, 1, 10);
temp[r][c] -= 4;
}
//draws a top wall
if(temp[r][c] - 2 >=0) {
g.setColor (Color.blue);
g.fillRect((row*100) - 100, (col*10) - 10, 10, 1);
temp[r][c] -= 2;
}
//draws a left wall
if(temp[r][c] - 1 >=0) {
g.setColor (Color.blue);
g.fillRect((row*10) - 10, (col*10) - 1, 1, 10);
temp[r][c] -= 1;
}
}
}
}
/**
* Creates the Gui for the maze
*/
public void createAndShowGUI() {
JComponent board = new Maze();
JPanel panel = new JPanel();
panel.setBackground(Color.black);
panel.add(board);
JFrame pacFrame = new JFrame();
pacFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pacFrame.setContentPane(panel);
setSize(BOARDWIDTH, BOARDHEIGHT);
pacFrame.pack();
pacFrame.setVisible(true);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|