1

私はアプレットに取り組んでいますが、これについての経験はありません.. 2 つのオブジェクトをペイントし、画像を挿入し、背景色を黒に変更したいと考えています。色を変更しなければ、すべて問題なく動作します。問題は、背景色も変更することにしたときに発生しました。私が得るのは、絵や写真のない黒い画面です。ウィンドウを最小化またはサイズ変更すると、すべてが取得されます。以下は私のコードで、単純化されたバージョンです。

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

public class JAlienHunt extends JApplet implements ActionListener {

private JButton button = new JButton();
JLabel greeting = new JLabel("Welcome to Alien Hunt Game!");
JLabel gameOverMessage = new JLabel(" ");
JPanel displayPanel = new JPanel(new GridLayout(2, 4));
private int[] alienArray = new int[8];
int countJ = 0, countM = 0; 
private ImageIcon image = new ImageIcon("earth.jpg");
private int width, height;

Container con = getContentPane();
Font aFont = new Font("Gigi", Font.BOLD, 20);       

public void init() {

    /** Setting the Layout and adding the content. */  

    width = image.getIconWidth();
    height = image.getIconHeight();

    greeting.setFont(aFont);
    greeting.setHorizontalAlignment(SwingConstants.CENTER);
    con.setLayout(new BorderLayout());
    con.add(greeting, BorderLayout.NORTH);
    con.add(displayPanel, BorderLayout.CENTER);


    /** Add Buttons to the Applet */

        displayPanel.add(button);
        String text = Integer.toString(i+1);     // convert button # to String adding 1.
        buttons.setText(text);
        buttons.addActionListener(this);
} 

public void actionPerformed(ActionEvent event) {

    /** Shows the Alien representing the selected button and deactivate the button. */
    if(event.getSource() == buttons) 
                button.setText("Jupiterian");

            else 
                buttons[i].setText("Martian");

            button.setEnabled(false);
        con.remove(greeting);
        displayPanel.remove(button);
        displayPanel.setLayout(new FlowLayout());
        gameOverMessage.setHorizontalAlignment(SwingConstants.CENTER);
            con.add(gameOverMessage, BorderLayout.NORTH);
            repaint();
    }

public void paint(Graphics gr) {

    super.paint(gr);

    /** Condition when user loses the game. Two Jupiterians will be painted on the screen*/
    Jupiterian jupit = new Jupiterian();
    displayPanel.setBackground(Color.BLACK);
    gameOverMessage.setFont(new Font ("Calibri", Font.BOLD, 25));
    gameOverMessage.setText("The Earth has been destroyed!");
    jupit.draw(gr, 250, 120);   
    gr.copyArea(190, 40, 465, 300, 500, 0); 
    gr.drawImage(image.getImage(), 400, 400, width, height, this);  //+ 

}   
}

---------------- Jupiterian クラスの draw() メソッド

public void draw(Graphics g, int x, int y) {

    g.setColor(Color.WHITE);
    g.drawOval(x, y, 160, 160);                         // Body of the alien
    g.drawLine(x, y + 80, x - 40, y + 170);             // Left hand
    g.drawLine(x - 40, y + 170, x - 40, y + 180);       // Left hand fingers
    g.drawLine(x - 40, y + 170, x - 55, y + 180); 
    Font aFont = new Font ("Chiller", Font.BOLD, 30);  // Description text.
g.setFont(aFont);
g.drawString(toString(), 230, 60); 
}

--- 抽象クラス

public abstract class Aliena {

protected String name;
protected String planet;

/** Constructor for the class. Creates the Alien object with the parameters provided */

public Aliena(String nam, int eyes, String hair, String plan){

    name = nam;
    planet = plan;
}

/** Method that returns a String with a complete description of the Alien. */

public String toString(){

    String stringAlien = "I am " + name + " from " + planet;
    return stringAlien;
}
}

前もって感謝します!

4

1 に答える 1

0

メソッド内からdisplayPanel.setBackground(Color.BLACK);gameOverMessage.setFont(new Font("Calibri", Font.BOLD, 25));gameOverMessage.setText("The Earth has been destroyed!");または他の UI コンポーネントを更新しないでください。paint

これにより、単にrepaint再スケジュールが行われ、更新の悪循環が開始され、CPU が消費され、世界が破滅のブラック ホールに吸い込まれます...

代わりに、呼び出す前にコンポーネントの状態を変更しますrepaint

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class JAlienHunt extends JApplet implements ActionListener {

    private JButton button = new JButton();
    JLabel greeting = new JLabel("Welcome to Alien Hunt Game!");
    JLabel gameOverMessage = new JLabel(" ");
    JPanel displayPanel = new JPanel(new GridLayout(2, 4));
    private int[] alienArray = new int[8];
    int countJ = 0, countM = 0;
    private ImageIcon image = new ImageIcon("earth.jpg");
    private int width, height;
    Container con = getContentPane();
    Font aFont = new Font("Gigi", Font.BOLD, 20);

    public void init() {

        /**
         * Setting the Layout and adding the content.
         */
        width = image.getIconWidth();
        height = image.getIconHeight();

        greeting.setFont(aFont);
        greeting.setHorizontalAlignment(SwingConstants.CENTER);
        con.setLayout(new BorderLayout());
        con.add(greeting, BorderLayout.NORTH);
        con.add(displayPanel, BorderLayout.CENTER);



        /**
         * Add Buttons to the Applet
         */
        displayPanel.add(button);
//        String text = Integer.toString(i + 1);     // convert button # to String adding 1.
        button.setText("!");
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event) {

        /**
         * Shows the Alien representing the selected button and deactivate the
         * button.
         */
//        if (event.getSource() == buttons) {
//            button.setText("Jupiterian");
//        } else {
////            buttons[i].setText("Martian");
//        }

        button.setEnabled(false);
        con.remove(greeting);
        displayPanel.remove(button);
        displayPanel.setLayout(new FlowLayout());
        gameOverMessage.setHorizontalAlignment(SwingConstants.CENTER);
        con.add(gameOverMessage, BorderLayout.NORTH);
        displayPanel.setBackground(Color.BLACK);
        gameOverMessage.setFont(new Font("Calibri", Font.BOLD, 25));
        gameOverMessage.setText("The Earth has been destroyed!");
        repaint();
    }

    public void paint(Graphics gr) {

        super.paint(gr);

        /**
         * Condition when user loses the game. Two Jupiterians will be painted on
         * the screen
         */
        Jupiterian jupit = new Jupiterian();
//        displayPanel.setBackground(Color.BLACK);
//        gameOverMessage.setFont(new Font("Calibri", Font.BOLD, 25));
//        gameOverMessage.setText("The Earth has been destroyed!");
        jupit.draw(gr, 250, 120);
//        gr.copyArea(190, 40, 465, 300, 500, 0);
        gr.drawImage(image.getImage(), 400, 400, width, height, this);  //+ 

    }

    public class Jupiterian {

        public void draw(Graphics g, int x, int y) {

            g.setColor(Color.WHITE);
            g.drawOval(x, y, 160, 160);                         // Body of the alien
            g.drawLine(x, y + 80, x - 40, y + 170);             // Left hand
            g.drawLine(x - 40, y + 170, x - 40, y + 180);       // Left hand fingers
            g.drawLine(x - 40, y + 170, x - 55, y + 180);
            Font aFont = new Font("Chiller", Font.BOLD, 30);  // Description text.
            g.setFont(aFont);
            g.drawString(toString(), 230, 60);
        }
    }
}
于 2013-04-18T00:27:23.723 に答える