简体   繁体   中英

JApplet setBackground not working

I am wondering why the method setBackground() is not actually making the background black. I have feeling this has something to do with the class implementing JApplet as opposed to Applet but I can not figure out the specifics. It's really bugging me. Any help is appreciated!

import javax.swing.*;
import java.awt.*;

public class Rocket extends JApplet
{
    private final int APPLET_WIDTH = 200;
    private final int APPLET_HEIGHT = 200;

    private int[] xRocket = {100, 120, 120, 130, 130, 70, 70, 80, 80};
    private int[] yRocket = {15, 40, 115, 125, 150, 150, 125, 115, 40};

    private int[] xWindow = {95, 105, 110, 90};
    private int[] yWindow = {45, 45, 70, 70};

    private int[] xFlame = {70, 70, 75, 80, 90, 100, 110, 115, 120, 130, 130};
    private int[] yFlame = {155, 170, 165, 190, 170, 175, 160, 185, 160, 175, 155};

    public void init ()
    {
        setBackground (Color.black);
        setSize (APPLET_WIDTH, APPLET_HEIGHT);
    }

    public void paint (Graphics page)
    {
        page.setColor (Color.cyan);
        page.fillPolygon (xRocket, yRocket, xRocket.length);

        page.setColor (Color.gray);
        page.fillPolygon (xWindow, yWindow, xWindow.length);

        page.setColor (Color.red);
        page.drawPolyline (xFlame, yFlame, xFlame.length);
    }
} 

Set the color on the ContentPane rather than on the parent applet component

getContentPane().setBackground(Color.BLACK);

Aside:

For custom painting in Swing override paintComponent rather than paint . JApplet is not a sub-class of JComponent so a new component based on this is needed to do this. Make sure to invoke super.paintComponent(g) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM