Great Emperor of Middland Great Emperor of Middland - 5 months ago 24
Java Question

Java paintComponents is not being called

So I read some stuff online and found that that JPanel is already double-Buffered, but in order to utilize and double-Buffering it has one needs to use the

paintComponents(Graphics g)
functions instead of the
paint(Graphics g)
function.

So I did, but while I use
paint(Graphics g)
it does works and draws the things I need, it suffers from a flickering syndrome because of the lack of DB, but when I do the same with
paintComponents(Graphics g)
it simply doesn't even call this function.
Please help!!!

here's my code:

public class GamePanelFinal extends javax.swing.JFrame implements KeyListener, ActionListener{

int score = 0;
int snakeBodyParts = 1;

Sphere3D snakeHead, snakeFood;
Sphere3D loadingIndicator1, loadingIndicator2, loadingIndicator3;
Point3D lastSnakePosition, foodPostion;
Color snakeColor;

boolean paintCalled, foodShoulBeRedrawn, isGameOver, shouldSpawnBodyPart = false;
ArrayList<Sphere3D> snake;




public GamePanelFinal() {
initComponents();
setResizable(false);
setTitle("Snake Game");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(dim.width/2 - getSize().width/2, dim.height/2 - getSize().height/2);

gamePanelJPanel.setOpaque(true);
gamePanelJPanel.setBackground(Color.BLACK);


snake = new ArrayList<>();


snakeHead = spawnSnake();
snakeHead.Zoom(10);
snakeHead.TurnCenter(45);
snakeHead.Move(getWidth()/2, getHeight()/2, 0);
snake.add(snakeHead);
lastSnakePosition = new Point3D(getWidth()/2, getHeight()/2, 0);

initLoadIndicator();
spawnFood();


addKeyListener(this);
setFocusable(true);
pack();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}



public void initLoadIndicator() {
loadingIndicator1 = new Sphere3D(Color.MAGENTA);
loadingIndicator2 = new Sphere3D(Color.orange);
loadingIndicator3 = new Sphere3D(Color.gray);

loadingIndicator1.Move(100, 100, 0);
loadingIndicator2.Move(100, 100, 0);
loadingIndicator3.Move(100, 100, 0);

loadingIndicator1.Zoom(5);
loadingIndicator2.Zoom(5);
loadingIndicator3.Zoom(5);
}

public void spawnFood() {
snakeFood = new Sphere3D(Color.yellow);

Random r = new Random();
int resultX = r.nextInt((700 - 200) + 1) + 200;
int resultY = r.nextInt((500 - 100) + 1) + 100;

foodPostion = new Point3D(resultX, resultY, 0);

snakeFood.Zoom(10);
snakeFood.TurnCenter(45);
snakeFood.Move(resultX, resultY, 0);
}

public Sphere3D spawnSnake() {
Sphere3D snakeHead;

switch(NewGame.colorScheme) {
case 1: snakeHead = new Sphere3D(Color.red);
snakeColor = Color.red;
break;
case 2: snakeHead = new Sphere3D(Color.green);
snakeColor = Color.green;
break;
case 3: snakeHead = new Sphere3D(Color.blue);
snakeColor = Color.blue;
break;
default: snakeHead = new Sphere3D(Color.cyan);
snakeColor = Color.cyan;
break;
}

return snakeHead;
}

@Override
public void paintComponents(Graphics g) {
super.paintComponents(g); //To change body of generated methods, choose Tools | Templates.
System.out.println("paintComponents!");

snakeHead.DrawSphere(g);
snakeFood.DrawSphere(g);


g.drawOval(40, 58, 100, 100);
loadingIndicator1.DrawWierSphere(g);
loadingIndicator2.DrawWierSphere(g);
loadingIndicator3.DrawWierSphere(g);
loadingIndicator1.TurnCenter(0.08);
loadingIndicator2.TurnCenter(0.09);
loadingIndicator3.TurnCenter(0.06);

if(foodShoulBeRedrawn){
foodShoulBeRedrawn = false;
spawnFood();
}

if(shouldSpawnBodyPart){
shouldSpawnBodyPart = false;
snake.get(snake.size() - 1).DrawSphere(g);

}

//spawnSnake();
paintCalled = true;

repaint();
}

/*
@Override
public void paint(Graphics g) {
super.paint(g);

snakeHead.DrawSphere(g);
snakeFood.DrawSphere(g);

g.drawOval(40, 58, 100, 100);
loadingIndicator1.DrawWierSphere(g);
loadingIndicator2.DrawWierSphere(g);
loadingIndicator3.DrawWierSphere(g);
loadingIndicator1.TurnCenter(0.08);
loadingIndicator2.TurnCenter(0.09);
loadingIndicator3.TurnCenter(0.06);

if(foodShoulBeRedrawn){
foodShoulBeRedrawn = false;
spawnFood();
}

if(shouldSpawnBodyPart){
shouldSpawnBodyPart = false;
snake.get(snake.size() - 1).DrawSphere(g);

}

//spawnSnake();
paintCalled = true;

repaint();
}
*/

Answer

You're overriding Container#paintComponents(), which was inherited by your JFrame. Instead of extending JFrame, you need to extend JPanel so you can override its paintComponent() and getPreferredSize(). Then you can add() the panel to a JFrame, like they show here and here.