0

I am following a video tutorial which tries to make a Java game in 2d. I have found that course's author's approach is not working properly because of changing JFrame's color doesn't show up!

His approach, the Window class:

package Modelos;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.JFrame;

public class Ventana extends JFrame implements WindowListener  {

    public Ventana(String titulo){
        super(titulo);
        this.setSize(800,600);
        addWindowListener(this);
        setBackground(Color.BLACK);
    }

    @Override
    public void windowOpened(WindowEvent we) {
    }

    @Override
    public void windowClosing(WindowEvent we) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent we) {
    }

    @Override
    public void windowIconified(WindowEvent we) {
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
    }

    @Override
    public void windowActivated(WindowEvent we) {
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
    }
}

Panel class which is supposed to graph rectangles, triangles, circles to represent game's objects:

package Modelos;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.JPanel;

public class PanelFG extends JPanel {

    ArrayList aDibujar;

    public PanelFG(ArrayList Dibujar){
        this.aDibujar=Dibujar;
    }

    public void print(Graphics g){
        Dibujable dib;
        for (int i = 0; i <aDibujar.size(); i++) {
            dib = (Dibujable) aDibujar.get(i);
            dib.dibujar(g);
        }
    }
}

And Main

package Ejecuciones;
import Modelos.*;
import java.awt.Button;
import java.awt.Color;
import java.util.ArrayList;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Ventana nuestraVentana = new Ventana("Juego de Naves");
        ArrayList ArregloDeObjetos = new ArrayList();

        PanelFG nuestroPanel = new PanelFG(ArregloDeObjetos);

        nuestraVentana.add(nuestroPanel);

        nuestraVentana.setSize(800,600);
        nuestraVentana.setVisible(true);
    }
}

RESULT: White JFRAME

However my approach:

WINDOW:

package Modelos;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.JFrame;

public class Ventana extends JFrame implements WindowListener  {

    public Ventana(String titulo){
        super(titulo);
        this.setSize(800,600);
        addWindowListener(this);
        this.getContentPane().setBackground(Color.BLACK);
        this.setVisible(true);
    }

    @Override
    public void windowOpened(WindowEvent we) {
    }

    @Override
    public void windowClosing(WindowEvent we) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent we) {
    }

    @Override
    public void windowIconified(WindowEvent we) {
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
    }

    @Override
    public void windowActivated(WindowEvent we) {
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
    }
}

PANEL class is equal.

MAIN:

package Ejecuciones;
import Modelos.*;
import java.awt.Button;
import java.awt.Color;
import java.util.ArrayList;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Ventana nuestraVentana = new Ventana("Juego de Naves");
        ArrayList ArregloDeObjetos = new ArrayList();

        PanelFG nuestroPanel = new PanelFG(ArregloDeObjetos);
    }
}

BLACK correct window I think it is due to the author did not used getContentPane() when creating the main JFrame to set its background to black, and I did.

I have followed the topic: JFrame.setBackground() not working -- why?

Also I do not understand how a code would work properly and other do not, considering that we do both are using Netbeans 8.1.

Any opinion/suggestion/explanation?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Yone
  • 2,064
  • 5
  • 25
  • 56

1 Answers1

0

you never add any control to JPanel container .... gray color is default color background to de JPanel and black is set it for the JFrame. Exactly what do you want to do?

erod
  • 181
  • 1
  • 17