Search code examples
javaswingjframejpanelpaintcomponent

JFrame background color flashes but then reverts


Lately I've been attempting to replicate Space Invaders in java to help with learning about developing applications with java and the programming language in general. However I've run into a little problem with JFrame: the background color that I declared for the window doesn't stay, it just flashes and then reverts to the default. Here's my code:

import javax.imageio.ImageIO;`
import java.io.*;`
import javax.swing.*;`
import java.awt.*;`
import java.awt.image.*;`
import java.awt.image.ImageObserver;`
import java.awt.event.*;`

public class Invaders extends JPanel{

    public static int x = 40;
    public static int y = 345;
    public static int h = 20;
    public static int k = 180;
    public static int move = 1;
    static final Invaders m = new Invaders();

    public static void main(String[] args){

        final JFrame frame = new JFrame("Movement of 2d Shapes");
        frame.setSize(404,390);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(m);
        frame.setBackground(Color.BLACK);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                if(x <= 350){
                    x += 10;
                    m.repaint();
                };
            }
        };

        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                if(x >= 10){
                    x -= 10;
                    m.repaint();
                };
            }
        };

        KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
        KeyStroke left = KeyStroke.getKeyStroke("LEFT");

        InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(right, "RIGHT");
        inputMap.put(left, "LEFT");
        m.getActionMap().put("RIGHT", actionRight);
        m.getActionMap().put("LEFT", actionLeft);

    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        draw(g);
        cpu_move(m);
    }

    public void cpu_move(Invaders m){ 
        if(h == 0){
            move = 0;
        }else if(h == 375){
            move = 1;
        }
        if(move == 0){
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            h += 5;
            m.repaint();
        }else if(move == 1){
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            h -= 5;
            m.repaint();
        };
    }

    public void draw(Graphics g){
        try{
            g.drawImage(ImageIO.read(getClass().getResource(
                 "images/Ship.jpg")), x, y, 35, 23, Color.BLACK, null);
            g.drawImage(ImageIO.read(getClass().getResource(
                 "images/Alien.jpg")), h, k, 28, 20, Color.BLACK, null);
        }catch(IOException k){
            Component temporaryLostComponent = null;
            JOptionPane.showMessageDialog(temporaryLostComponent, 
                  "one or more image files missing or corrupt");
        }
    }
}

What's wrong with the declaration of the background color? there are no errors when compiling, but it still does this. What am I doing wrong?


Solution

  • Try next :

    frame.getContentPane().add(m);
    m.setBackground(Color.BLACK);
    

    instead of frame.setBackground(Color.BLACK); because Invaders m fills all frame.

    Your backgound will be black with image.