簡體   English   中英

JApplet問題中的Java繪畫圖形

[英]Java painting graphics in JApplet issues

我希望這不會是一個新手問題。 我以前沒有做過圖形樣式編程。 我的目標是在小程序中創建彈球廣告游戲。 但是,我遇到了第一個障礙。 我的小程序沒有顯示Table類(擴展了JPanel)的paintComponent方法的結果。 我已經嘗試了幾件事,例如我如何加載圖像(當前使用雙緩沖,但之前確實使用過mediatracker),看看是否沒有其他GUI東西會允許繪畫發生(因為我想知道是否正在繪畫)畫在下面)和其他東西。 這個問題困擾着我,我開始懷疑(並希望)它是否被我忽略了,如果是,那么我很抱歉,但仍然會很感激,因為我不能很努力遠沒有解決此問題。 我的Pinball(小程序)和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;
        }

}

您的Table 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的?

運行,然后檢查此代碼並查看是否可以找到問題的根源。

// <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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM