Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: JPanel Constantly Being Repainted Due To setOpaque(false) Method

  1. #1
    Join Date
    Jan 2015
    Rep Power

    Default JPanel Constantly Being Repainted Due To setOpaque(false) Method

    I am creating a game of Snake and have come across the issue of having to use a JLayeredPane. I have drawn the board using the DrawBoard class which constantly must be repainted according to a timer. I have to assign a random colour to 3 fruit I am placing on the board, as I cannot place this in the DrawBoard class (as it is constantly being repainted and therefore the random colour keeps on changing for every repaint) I have had to make a separate JPanel, c2. c2 is then put this into a JLayeredFrame along with the container (containing the drawBoard component) JPanel so that the c2 can sit on top of container -- effectively overlaying the fruit onto the board as to allow manual repainting of the fruit component every time the fruit is collected to set a random colour.

    As I am drawing the fruit onto the board, I must set the background of the components and the JPanel to be transparent, so that you can see the board below. The problem, I have identified is that when c2.setOpaque(false) is called, the c2 JPanel keeps on repainting itself, therefore producing random colours continuously for the fruit.

    The JLayeredPane is then placed into a JFrame.

    How would it be possible to make the c2 JPanel have a transparent background without it calling itself?

    Board Class -- this is where the JFrame is

    Java Code:
    public Board() {
    public void boardBack(Graphics g) {
    questionBox = new QuestionBox();
    drawBoard = new DrawBoard();
    points = new Points();
    fruit = new Fruit();
    filler = new Filler();
    JFrame frame = new JFrame("Snake");
    JPanel container = new JPanel();
    JPanel c2 = new JPanel();
    JLayeredPane pane = new JLayeredPane();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    container.setLayout(new BorderLayout());
    c2.setLayout(new BorderLayout());
    container.add(questionBox, BorderLayout.NORTH);
    container.add(points, BorderLayout.CENTER);
    container.add(drawBoard, BorderLayout.SOUTH);
    c2.add(fruit, BorderLayout.SOUTH);
    c2.add(filler, BorderLayout.NORTH);
    //This is where the problem exists, 
    //when removed I cannot see the drawBoard component below,
    //but it no longer repaints.
    container.setBounds(0, 0, 600, 720);
    c2.setBounds(0, 0, 600, 720);
    pane.add(c2, new Integer(2));
    pane.add(container, new Integer(1));
    pane.setPreferredSize(new Dimension(600, 720));
    In this class I also have a actionPerformed method and Timer which does that action for every tick. There is no recalling of the fruit component within this.

    Fruit class

    Java Code:
     package snake;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.util.Random;
    import javax.swing.JPanel;
    public class Fruit extends JPanel {
    public static Color red = new Color(8005929);
    public static Color brown = new Color(9067566);
    public static Color purple = new Color(6684774);
    public static Dimension dim4 = new Dimension(600, 600);
    public Dimension getPreferredSize() {
    return new Dimension(dim4.width, dim4.height);
    Random rand = new Random();
    public int selectColour;
    //The structure of the array is as follows
    // [0] - fruit1 colour
    // [1] - fruit2 colour
    // [2] - fruit3 colour
    public int[] colourSelected = new int[3];
    protected void paintComponent(Graphics g) {
    //This allows us to select a random colour for the fruit to be assigned.
    //Where selectColour = 0 refers to the colour red
    //Where selectColour = 1 refers to the colour brown
    //Where selectColour = 2 refers to the colour purple
    int selectColour = rand.nextInt(3);
    switch (selectColour) {
    case 0:
    colourSelected[0] = selectColour;
    case 1:
    colourSelected[0] = selectColour;
    case 2:
    colourSelected[0] = selectColour;
    //Draws the fruit at the point of the fruit
    g.fillRect(Board.fruit1.x * Board.SCALE, Board.fruit1.y * Board.SCALE,
    Board.SCALE, Board.SCALE);
    //This draws the second fruit
    g.fillRect(Board.fruit2.x * Board.SCALE, Board.fruit2.y * Board.SCALE,
    Board.SCALE, Board.SCALE);
    //This draws the third fruit
    g.fillRect(Board.fruit3.x * Board.SCALE, Board.fruit3.y * Board.SCALE,
    Board.SCALE, Board.SCALE);
    This shows the issue when run I have put a `System.out.println()` within the `paintComponent()` method to show the problem.

    When setOpaque(false) is still in the code (the bottom left fruit continuously changes colour)

    When removed
    Last edited by JosAH; 01-11-2015 at 06:00 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: JPanel Constantly Being Repainted Due To setOpaque(false) Method

    Why change selectColour inside of the paintComponent() method? Can it be changed somewhere else where the code has better control of when it is changed?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: JPanel Constantly Being Repainted Due To setOpaque(false) Method

    @OP: don't edit away your original post; it renders the entire thread useless; I put back the original content (more or less); there are more people reading this thread.

    DarrylBurke likes this.
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: JPanel Constantly Being Repainted Due To setOpaque(false) Method

    I haven't run your code so this my be unrelated. But why do you set only item # 0 of colourSelected? It is an array of length 3.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Clock is not repainted properly
    By skiabox in forum AWT / Swing
    Replies: 3
    Last Post: 12-15-2011, 10:54 PM
  2. Replies: 5
    Last Post: 05-26-2011, 06:55 AM
  3. Replies: 1
    Last Post: 05-16-2011, 01:19 PM
  4. a setOpaque(false) JPanel is not transparent
    By imorio in forum AWT / Swing
    Replies: 9
    Last Post: 09-01-2010, 09:02 PM
  5. NetBeans GUI Builder + constantly updating information
    By Inventor22 in forum New To Java
    Replies: 5
    Last Post: 07-07-2010, 11:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts