Applet问题中的Java绘图图形

时间:2011-04-21 12:38:39

标签: java

我希望这不会出现太多新手问题。我之前没有完成图形样式编程。我的目标是在applet中创建一个弹球广告游戏。但是,我陷入了第一个障碍之一。我的applet没有显示我的Table类(扩展JPanel)的paintComponent方法的结果。我尝试过几个方面,例如我如何加载图像(目前使用双缓冲,但之前我确实使用过mediatracker),看看是否没有任何其他GUI内容可以让绘画发生(因为我想知道它是否正在以某种方式被绘制出来)和其他东西。这个问题困扰了我,我开始怀疑(并希望)它是否是我忽略的小事,如果是的话,那我很抱歉但仍会感激不尽求助,因为我不能在没有先解决这个问题的情况下走得很远。我的弹球(applet)和Table类的代码如下,其他类尚未实现。再一次,我感谢任何帮助。

import javax.swing.*; // useful for the drawing side, also going to be a JApplet
import java.awt.*;
import java.net.*;

public class Pinball extends JApplet {
    // variables go here
    Table table;

    // further initialisation of the GUI
    public void init() {
                        setSize(300, 300);
                table = new Table(this);
                add(table);
                                setContentPane(table); // makes our graphical JPanel container the content pane for the Applet
                // createGUI(); // this has been moved onto the table class
            }

    public void stop() {

    }

}

现在是Table类:

import java.awt.*; // needed for old style graphics stuff
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*; // gives us swing stuff
import java.io.IOException;
import java.net.*; // useful for anything using URLs

public class Table extends JPanel {
// attributes go here
    Pinball pb;
    Color bgColour;
    JPanel eastPanel;
    JPanel logoPanel;
    JPanel livesPanel;
    JPanel scorePanel;
    JPanel tablePanel;
    JPanel scrollTextPanel;
        Image logo;

    // constructor goes here
public Table(Pinball pb) {
setPreferredSize(new Dimension(300, 300));
    this.pb = pb;
    // this is not needed anymore, with the new loadImage class down the bottom
//  Toolkit tk = Toolkit.getDefaultToolkit(); // needed to get images
//  logo = tk.getImage(base + "images/logo.jpg");
        logo = loadImage("logo.jpg");
     createGUI();
        }

        // public methods go here
        // all GUI creation stuff goes here
        public void createGUI() {
            /* allows the three parts (top, middle and right) 
             * to be made through (north, center and right) */
                    setLayout(new BorderLayout());
                    // setting the background colour
                    bgColour = new Color(190, 186, 221); // makes the sky blue colour for the background.
                    setBackground(bgColour);
                                                        // now putting a panel for the east side
                    eastPanel = new JPanel();
                    eastPanel.setBackground(bgColour);
                    eastPanel.setLayout(new BorderLayout(8, 8));
                    eastPanel.setPreferredSize(new Dimension(100, 280));
                                    logoPanel = new JPanel();
    logoPanel.setBackground(Color.WHITE);
    logoPanel.setPreferredSize(new Dimension(100, 100));
    logoPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white));
    //JLabel label1 = new JLabel("Logos go here");
    //logoPanel.add(label1);
    eastPanel.add(logoPanel, "North");
    livesPanel = new JPanel();
    livesPanel.setBackground(Color.WHITE);
    livesPanel.setPreferredSize(new Dimension(100, 100));
    JLabel label2 = new JLabel("Lives go here");
    livesPanel.add(label2);
    eastPanel.add(livesPanel, "Center");
    scorePanel = new JPanel();
    scorePanel.setBackground(Color.WHITE);
    scorePanel.setPreferredSize(new Dimension(100, 80));
    JLabel label3 = new JLabel("Scores go here");
    scorePanel.add(label3);
    eastPanel.add(scorePanel, "South");
add(eastPanel, "East");
    tablePanel = new JPanel();
    tablePanel.setBackground(bgColour);
    tablePanel.setPreferredSize(new Dimension(200, 280));
    add(tablePanel, "Center");
    scrollTextPanel = new JPanel();
    scrollTextPanel.setPreferredSize(new Dimension(300, 20));
    scrollTextPanel.setBackground(Color.WHITE);
    scrollTextPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    add(scrollTextPanel, "North");
    // repaint();
                        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
        g.drawImage(logo, 5, 5, 90, 90, null);
        g.drawLine(0, 0, 300, 300); // for testing, does not work
              }

        // a little useful method for handling loading of images and stuff
        public BufferedImage loadImage(String filename) {
        BufferedImage image = null;
        try {
               URL url = new URL(pb.getCodeBase(), "images/" + filename);
               image = ImageIO.read(url);
            } catch (IOException e) {
            }
    return image;
        }

}

2 个答案:

答案 0 :(得分:2)

您的表JPanel由其他JPanel覆盖,可以没问题,但您将无法看到覆盖它的不透明组件,特别是tablePanel JPanel。例如,尝试:

  tablePanel = new JPanel();
  // tablePanel.setBackground(bgColour); //!! removed
  tablePanel.setOpaque(false); //!! added

看看会发生什么。

关于此代码的一个无关的问题:

   public void init() {
      setSize(300, 300);
      table = new Table(this);
      add(table);
      setContentPane(table); // makes our graphical JPanel container the content

为什么要将表对象添加两次,一次添加到JApplet的contentPane,然后再添加作为 JApplet的contentPane?

答案 1 :(得分:1)

运行然后检查此代码&看看你是否能发现问题的根源。

// <applet code='Pinball' width='300' height='300'></applet>
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*; // useful for the drawing side, also going to be a JApplet
import javax.imageio.ImageIO;

import java.net.*;
import java.io.IOException;

public class Pinball extends JApplet {
    // variables go here
    Table table;

    // further initialisation of the GUI
    public void init() {
        table = new Table(this);
        setContentPane(table); // makes our graphical JPanel container the content pane for the Applet
    }
}

class Table extends JPanel {
    // attributes go here
    Pinball pb;
    Color bgColour;
    JPanel eastPanel;
    JPanel logoPanel;
    JPanel livesPanel;
    JPanel scorePanel;
    JPanel tablePanel;
    JPanel scrollTextPanel;
    Image logo;

    // constructor goes here
    public Table(Pinball pb) {
        //setPreferredSize(new Dimension(300, 300));
        this.pb = pb;
        // this is not needed anymore, with the new loadImage class down the bottom
        //Toolkit tk = Toolkit.getDefaultToolkit(); // needed to get images
        //logo = tk.getImage(base + "images/logo.jpg");
        int size = 100;
        logo = //loadImage("logo.jpg");
            new BufferedImage( size,size, BufferedImage.TYPE_INT_RGB);
        Graphics g = logo.getGraphics();
        g.setColor(Color.red);
        g.fillRect(0,0,size,size);

        createGUI();
    }

    // public methods go here
    // all GUI creation stuff goes here
    public void createGUI() {
        /* allows the three parts (top, middle and right)
         * to be made through (north, center and right) */
        setLayout(new BorderLayout(20,20));
        // setting the background colour
        bgColour = new Color(190, 186, 221); // makes the sky blue colour for the background.
        setBackground(bgColour);
        // now putting a panel for the east side
        eastPanel = new JPanel();
        eastPanel.setBackground(bgColour);
        eastPanel.setLayout(new BorderLayout(8, 8));
        eastPanel.setPreferredSize(new Dimension(100, 280));
        logoPanel = new JPanel();
        logoPanel.setBackground(Color.WHITE);
        logoPanel.setPreferredSize(new Dimension(100, 100));
        logoPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.green));
        eastPanel.add(logoPanel, "North");
        livesPanel = new JPanel();
        livesPanel.setBackground(Color.WHITE);
        livesPanel.setPreferredSize(new Dimension(100, 100));
        JLabel label2 = new JLabel("Lives go here");
        livesPanel.add(label2);
        eastPanel.add(livesPanel, "Center");
        scorePanel = new JPanel();
        scorePanel.setBackground(Color.WHITE);
        scorePanel.setPreferredSize(new Dimension(100, 80));
        JLabel label3 = new JLabel("Scores go here");
        scorePanel.add(label3);
        eastPanel.add(scorePanel, "South");
        add(eastPanel, "East");
        tablePanel = new JPanel();
        tablePanel.setBackground(bgColour);
        tablePanel.setPreferredSize(new Dimension(200, 280));
        add(tablePanel, "Center");
        scrollTextPanel = new JPanel();
        scrollTextPanel.setPreferredSize(new Dimension(300, 20));
        scrollTextPanel.setBackground(Color.WHITE);
        scrollTextPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        add(scrollTextPanel, "North");
    }

    @Override
    public void paintComponent(Graphics g) {
        System.out.println("paintComponent");
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawImage(logo, 5, 5, 90, 90, this);
        g.drawLine(0, 0, 300, 300); // for testing, does not work
    }

    // a little useful method for handling loading of images and stuff
    public BufferedImage loadImage(String filename) {
        BufferedImage image = null;
        try {
            URL url = new URL(pb.getCodeBase(), "images/" + filename);
            image = ImageIO.read(url);
        } catch (IOException e) {
            // do NOT ignore exceptions in broken code.
        }
        return image;
    }
}