新版java中没有japplet了_java - JApplet问题中的Java绘画图形 - 堆栈内存溢出

我希望这不会是一个新手问题。 我以前没有做过图形样式编程。 我的目标是在小程序中创建弹球广告游戏。 但是,我遇到了第一个障碍。 我的小程序没有显示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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值