kemp5
kemp5

Reputation: 9

JLabel doesn't change when setBackground() and setBorder() are called

I've been working on a snake program for one of my classes. It runs perfectly except for one small problem: In View (extends JLabel) I set the background to Color.WHITE, opaque to true and the border to Color.GREEN in the constructor. None of these lines seem to affect the GUI.

Here is the code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JLabel;

public class View extends JLabel {
    private Snake snake;
    private Fruit fruit;
    private Game game;

    public View(int size, Game g) {
        int W = Game.SIZE * Snake.SIZE;
        int H = Game.SIZE * Snake.SIZE;
        this.setOpaque(true);
        this.setBounds((Game.WIDTH-W)/2, (Game.HEIGHT-H)/2, W, H);
        this.setBackground(Color.WHITE);
        this.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        snake = new Snake(Snake.SIZE, Snake.SIZE);
        fruit = new Fruit();
        game = g;

    }

    public void start() {
        boolean flag = true;
        snake.start();

        while(flag) {
            snake.move();
            repaint();
            try {
                Thread.sleep(100);
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void move(int dir) {
        snake.move(dir);
    }

    public void doDrawing(Graphics2D g) {
        int s = snake.SIZE;
        int fx = fruit.getX();
        int fy = fruit.getY();
        g.setColor(Game.SNAKE_COLOR);
        Snake sn = snake;
        boolean flag = true;
        while(flag) {
            g.fillRect(sn.getX(), sn.getY(), s, s);
            if(sn.hasTail()) {
                sn = sn.getTail();
            }
            else flag = false;
        }

        g.setColor(Game.FRUIT_COLOR);
        g.fillRect(fx, fy, s, s);

        if(snake.getX() == fx && snake.getY() == fy) {
            snake.ate();
            fruit.create();
        }
    }

    public void paint(Graphics g) {
        game.repaint();
        Graphics2D d = (Graphics2D) g;
        doDrawing(d);
    }
}

Upvotes: 1

Views: 83

Answers (2)

kemp5
kemp5

Reputation: 9

package snake;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JComponent;

@SuppressWarnings("serial")
public class View extends JComponent {
    private Snake snake;
    private Fruit fruit;
    private Game game;

    public View(int size, Game g) {
        int W = Control.SIZE * Snake.SIZE;
        int H = Control.SIZE * Snake.SIZE;
        this.setOpaque(true);
        this.setVisible(true);
        this.setBounds((Game.WIDTH-W)/2, (Game.HEIGHT-H)/2, W, H);
        this.setBackground(Color.BLACK);
        this.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        snake = new Snake(Snake.SIZE, Snake.SIZE);
        fruit = new Fruit();
        game = g;

    }

    public void start() {
        boolean flag = true;
        snake.start();

        while(flag) {
            snake.move();
            repaint();
            try {
                Thread.sleep(Control.TIME);
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void move(int dir) {
        snake.move(dir);
    }

    public void doDrawing(Graphics2D g) {
        int s = Snake.SIZE;
        int fx = fruit.getX();
        int fy = fruit.getY();
        g.setColor(Control.SNAKE_COLOR);

        snake.display(g);

        g.setColor(Control.FRUIT_COLOR);
        g.fillRect(fx, fy, s, s);

        if(snake.getX() == fx && snake.getY() == fy) {
            snake.ate();
            fruit.create();
        }
    }

    public void paintComponent(Graphics g) {
        game.repaint();

        super.paintComponent(g);
        Graphics2D d = (Graphics2D) g;
        doDrawing(d);
    }
}

Game extends JFrame

Upvotes: -1

copeg
copeg

Reputation: 8348

You are overriding the paint method, yet never calling super.paint() to let the parent class perform its painting operation. More information available here.

Some other suggestions for improvement:

  1. Instead of using paint(), you should be overriding paintComponent() and in one of the first lines of the method make a call to the parent painting routine super.paintComponent(g).

  2. You might consider extending JPanel or JComponent rather than JLabel, as you seem to not use any functionality of this class.

  3. Do NOT call repaint in your painting method

Upvotes: 3

Related Questions