Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding backgroundcolor to snake applet
#1
Hi guys,

first, i'm not the best english speaker and -writer. Therefore i ask you to apologize my language mistakes Smile

Problably in your eyes, i have a very simply problem, but i cant add the background function to Brandonio's Snake Project. I want to have a black background in the applet. After this, i want to change the color of the grid to white.

I try to add two JPanel's to snakeApplet.java. But is it still not working. Every time i started the Applet, the background is white.



Code:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

public class snakeApplet extends Applet{

    private snakeCanvas c;
    JPanel jp1 = new JPanel();
    JPanel jp2 = new JPanel();
    
    public void init()
    {
        jp1.setBackground(Color.BLACK);
        add(jp1);
        
        jp2.setBackground(Color.BLACK);
        add(jp2, BorderLayout.SOUTH);
        
        c = new snakeCanvas();
        c.setSize(new Dimension(640, 480));
        c.setPreferredSize(new Dimension(640, 480));
        c.setVisible(true);
        c.setFocusable(true);
        this.add(c);
        this.setVisible(true);
        this.setSize(new Dimension(640, 480));
        
    }
    
    public void paint(Graphics g)
    {
        this.setSize(new Dimension(640, 480));
    }
}
In addition, i want to add music to the applet. The music should instantly playing if the applet is open.
If the idea of the black background and white grid is not to be a looker, than i would to add a picture in the background of the grid.
But i dont know how i can code it.
I hope someone can help me please Smile
Yours sincerley
Kevin

Edit: Kevin is only a nickname Big Grin
Reply
#2
Kevin,

Welcome to BP Forums! 

So I think that your issue regarding changing the color of the snake game's background comes down to the way you are attempting to do so.

Basically, it looks like you are trying to add two new panels, each with a black background. Then, you add the SnakeCanvas on top of the two panels. The background of the SnakeCanvas is white, so the black JPanels get covered up.

Since SnakeCanvas extends Java's Canvas, it can have it's background color set. I think that what you may want to do is alter the code of the SnakeCanvas class to have a Black background. If that doesn't work, you can always alter the Draw function of the SnakeCanvas class to first draw a black rectangle.

Then, you will need to change the colors for everything else in SnakeCanvas's draw method.
My Blog | My Setup | My Videos | Have a wonderful day.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)