[java] applet background color not changing.

Started by
7 comments, last by cptrnet 18 years, 7 months ago
Hello gamedev. My applet background color is grey, its not changing no matter what I do. There is probably something that I am not doing or that I am doing that makes it stay grey. I have a button and I am drawing on the applet. In my paint method I call super.paint(g) first then I draw some text, images and lines. I put setBackground(Color.black); in the init() method in the paint() method I don't know where to put it. I am a newbie at java applets, only a little experience. Is there something I am missing?
If you insist on saying "DUH", try to make a post that makes a little more sense
Advertisement
try putting this in your paint method:

g.setColor(Color.BLACK);g.fillRect(0,0,this.getWidth(),this.getHeight());


maybe that will work.
-----------------[ serafin studios ]
Where would I put that. I put it before super.paint(g) it makes the text white and the background is still grey. I put it after super.paint(g) and it erases my button.
If you insist on saying "DUH", try to make a post that makes a little more sense
The applet class inherits the setBackground( ) method from the Component class so all you have to do is call that method by specifying what color you would like the background to be. It does not matter where you call the setBackground( ) method. You may do it in either the init( ) method or the paint( ) method. Now I haven't used the setBackground method for any sort of animation, as I went with deonbotha's method of painting a rectangle each time, which is the size of the applet. But at the moment I see no reason why setBackground would not work. The only other thing that sounds strange from your post is your use of super.paint(g); I do not know why you would have that code as it is calling the paint( ) method of the Component Class, which Applet inherits. You might want to post your code for me so I know exactly what you are doing. Hope that helps and I apologize for being a bit wordy.

-Best of Luck

PS: search “the java tutorial” on Google and read the section on applets it will help you if you have little experience with applet programming.

Please note: I also had some problems with the code not running correctly in Firefox for some reason. This is in regards to the use of the setBackground command. I'm going to look into it, but try running your applet in IE or Java's applet viewer. It is probably just my settings on Firefox as they are very restrictive and I might not be up to date with my plug-in. Perhaps a more knowledgeable programmer can assist with answering that question.
I think you are a bit confused about how to use the applets init, and paint methods. As was said before, setBackground is inherited by java.applet.Applet and JApplet. Whichever one you are using, you would call setBackground in the applets init method without using super. That would set your applets background to the specified color. The paint method should draw any and all components that are to be displayed on the applet. This is called when a call to repaint is made. You can repaint everything or just repaint what needs to be updated, however im not sure if a call to repaint clears the applet first.

[edit] do make sure you close the browser to make sure the jvm doesnt have your old applet loaded. if you recompile it and run it in the same browser and jvm was never restarted, your applet wont be the newly compiled one, it will be the old one. Or at least thats an issue ive come across, i dont know if its me and something Im doing, but just thought id let you know.

[edit] oh yea... last thing.. if you add any panels to the applet, call setBackground on those too. If they cover the entire applet and you dont set their color, they go to the defaults, which on windows would be grey.

[Edited by - ju2wheels on August 23, 2005 7:34:06 AM]
Your right ju2wheels, I wasn't allowing the JVM to reload the newly compiled applet by neglecting to close the browser. :) I knew it was something easy. Thanks.
import javax.swing.JApplet;import java.awt.event.KeyListener;import java.awt.event.KeyEvent;import java.awt.event.MouseListener;import java.awt.event.MouseEvent;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.Graphics;import java.awt.Image;import java.awt.Color;import java.awt.FlowLayout;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JLabel;import java.awt.Insets;import java.awt.Dimension;public class AppletTest extends JApplet implements MouseListener, ActionListener{    private boolean PlayerOneTurn = true;  private boolean PlayerTwoTurn = false;  private boolean PlayerOneWin  = false;  private boolean PlayerTwoWin  = false;  private boolean[] squaresTaken;  private boolean[] squareToDraw;  private Image image_X;  private Image image_O;  private Image tmpImg;  private Graphics gfx;    private boolean isGameOver = false;    private JButton btnPlay;    public void init()  {        setLayout(null);    btnPlay = new JButton("Play");        Insets insets = getInsets();    Dimension size = btnPlay.getPreferredSize();    btnPlay.setBounds(375 + insets.left, 150 + insets.top,             size.width, size.height);    btnPlay.addActionListener(this);    add(btnPlay);        squaresTaken = new boolean[9];    squareToDraw = new boolean[9];    gfx = getGraphics();    image_X = getImage(getDocumentBase(), "x.jpg");    image_O = getImage(getDocumentBase(), "o.jpg");    addMouseListener(this);    reset();      }  public void paint(Graphics g)  {    g.setColor(Color.WHITE);     g.fillRect(0,0,this.getWidth(),this.getHeight());    super.paint(g);        g.drawString("Tic Tac Toe", 370, 25);    //draw all the squares that have been taken    for(int i = 0; i < 9; i++)    {      if(squaresTaken)      {        if(squareToDraw)        {          tmpImg = image_X;        }        else        {          tmpImg = image_O;        }        if(i < 3)        {          g.drawImage(tmpImg, i * 100, 0, this);        }        if(i >= 3 && i <= 5)        {          g.drawImage(tmpImg, (i - 3) * 100, 100, this);        }        if(i >= 6 && i <= 8)        {          g.drawImage(tmpImg, (i - 6) * 100, 200, this);        }      }    }        drawGrid();    if(!isGameOver)    {     if(PlayerOneTurn)     {       g.drawString("Player Ones Turn", 350, 75);     }     else     {       g.drawString("Player Twos Turn", 350, 75);     }    }    else    {     if(PlayerOneWin)     {       g.drawString("Player One Wins", 350, 75);     }     else     {       g.drawString("Player Two Wins", 350, 75);     }    }  }    private void reset()  {    for(int i = 0; i < 9; i++)    {         squaresTaken = false;         squareToDraw = false;    }        PlayerOneTurn = true;    PlayerTwoTurn = false;    PlayerOneWin  = false;    PlayerTwoWin  = false;    isGameOver = false;        repaint();  }  public void drawGrid()  {    gfx.drawLine(100, 0, 100, 300);    gfx.drawLine(200, 0, 200, 300);    gfx.drawLine(0, 100, 300, 100);    gfx.drawLine(0, 200, 300, 200);  }    private void CheckGameOver()  {    //Check X    //squaresTaken    //squareToDraw    int[][] checkArray = new int[8][3];        int[] tmpArray0 = {0,1,2};    checkArray[0] = tmpArray0;    int[] tmpArray1 = {3,4,5};    checkArray[1] = tmpArray1;    int[] tmpArray2 = {6,7,8};    checkArray[2] = tmpArray2;    int[] tmpArray3 = {0,3,6};    checkArray[3] = tmpArray3;    int[] tmpArray4 = {1,4,7};    checkArray[4] = tmpArray4;    int[] tmpArray5 = {2,5,8};    checkArray[5] = tmpArray5;    int[] tmpArray6 = {0,4,8};    checkArray[6] = tmpArray6;    int[] tmpArray7 = {2,4,6};    checkArray[7] = tmpArray7;    for(int i = 0; i < 8; i++)    {      if(squaresTaken[checkArray[0]] && squareToDraw[checkArray[0]] &&         squaresTaken[checkArray[1]] && squareToDraw[checkArray[1]] &&         squaresTaken[checkArray[2]] && squareToDraw[checkArray[2]])      {       //X Wins       isGameOver = true;       PlayerOneWin = true;      }            if(squaresTaken[checkArray[0]] && !squareToDraw[checkArray[0]] &&         squaresTaken[checkArray[1]] && !squareToDraw[checkArray[1]] &&         squaresTaken[checkArray[2]] && !squareToDraw[checkArray[2]])      {       //X Wins       isGameOver = true;       PlayerTwoWin = true;      }    }  }  public void mouseClicked(MouseEvent e)  {        if(isGameOver) return;    if(e.getModifiers() == MouseEvent.BUTTON1_MASK)    {            int square = CheckClick(e);      if(square != -1)      {        if(!squaresTaken[square])        {                    if(PlayerOneTurn)          {           PlayerOneTurn = false;           PlayerTwoTurn = true;           squareToDraw[square] = true;          }          else          {           PlayerOneTurn = true;           PlayerTwoTurn = false;           squareToDraw[square] = false;          }          squaresTaken[square] = true;          CheckGameOver();          repaint();        }      }    }  }    public void actionPerformed(ActionEvent e)  {    gfx.drawString("Button Click", 350, 200);    reset();  }  public int CheckClick(MouseEvent e)  {   int x = e.getX();   int y = e.getY();   int square = -1;   if(y >= 0 && y <= 99)   {     if(x >= 0 && x <= 99)     {       square = 0;     }     else if(x >= 100 && x <= 199)     {       square = 1;     }     else if(x >= 200 && x <= 299)     {       square = 2;     }   }   else if(y >= 100 && y <= 199)   {     if(x >= 0 && x <= 99)     {       square = 3;     }     else if(x >= 100 && x <= 199)     {       square = 4;     }     else if(x >= 200 && x <= 299)     {       square = 5;     }   }   else if(y >= 200 && y <= 299)   {     if(x >= 0 && x <= 99)     {       square = 6;     }     else if(x >= 100 && x <= 199)     {       square = 7;     }     else if(x >= 200 && x <= 299)     {       square = 8;     }   }      return square;  }    public void mouseExited(MouseEvent e){}  public void mouseEntered(MouseEvent e){}  public void mouseReleased(MouseEvent e){}  public void mousePressed(MouseEvent e){}}
If you insist on saying "DUH", try to make a post that makes a little more sense
Don't forget that with Swing top level containers (JFrame and JApplet), you need to get the content pane for any background action.

getContentPane().setBackground(Color.BLACK);getContentPane().add(new JButton("Ok"));
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Thanks
If you insist on saying "DUH", try to make a post that makes a little more sense

This topic is closed to new replies.

Advertisement