JAva错误无法转换为java.applet.Applet

时间:2017-04-17 00:54:08

标签: java image applet

我正在尝试使用java进行幻灯片演示。我尝试使用几种不同的方法显示图像,包括Buffered Image,GImage和ImageIcon / JLabel。我希望能够在空格键被击中时删除图片,这样我也可以显示一些文本,这就是我离开缓冲图像的原因。我现在正在使用ImageIcon / JLabel,但是我收到了一个错误。任何人都可以帮我解决这个错误。

import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import java.awt.Color;
import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;
import acm.util.*;
import java.util.Random;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class firstSlide extends Applet {

    private BufferedImage img;
    private static final int IMG_WIDTH = 50;
    private static final int IMG_HEIGHT = 50;
    private Image picture;
    private double scalefactor;
    GLabel text;

    public void init() {
        setBackground(Color.BLACK);
        getMouseListeners();
        getKeyListeners();
        try {
            URL url = new URL(getCodeBase(), "/home/rylie/Pictures/Capstone/HockeyPlayer.jpg");
            img = ImageIO.read(url);
        } catch (IOException e) {
        }
        String filename = this.getParameter("/home/rylie/Pictures/Capstone/HockeyPlayer.jpg");
        if (filename != null) {
            this.picture = this.getImage(getDocumentBase(), filename);
        }
        try {
            scalefactor = Double.valueOf(this.getParameter("scalefactor")).doubleValue();
        } catch (Exception e) {
            this.scalefactor = 1.0;
        }
    }

    public void paint(Graphics g) {
        g.drawImage(img, 90, 55, img.getWidth(this) / 2, img.getHeight(this) / 2, this);
    }

    public Dimension getPreferredSize() {
        if (img == null) {
            return new Dimension(50, 50);
        } else {
            return new Dimension(IMG_WIDTH, IMG_HEIGHT);
        }
    }

    public void stateChanged(ChangeEvent e) {
        Graphics g = img.getGraphics();
        g.setColor(Color.black);
        g.fillRect(0, 0, img.getWidth(null), img.getHeight(null));
        g.dispose();
        repaint();
    }

    public void keyPressed(KeyEvent k) {
        int key = k.getKeyCode();
        if (key == KeyEvent.VK_W) {
            stateChanged(null);
            text = new GLabel("This picture has been influenced by being open to growth. I have had to become much more open to growth through my hockey experience. When I first came to Regis, I was not very good at hockey. I had to be open and willing to change in order to get better. I had to trust that my coach was telling me the correct plays. By trusting my coach and being open to growth, I was able to improve a lot. I was able to be apart of the State Championship Varsity Tier II Hockey Team. Hockey has effected my Regis carrer as it has fiven me something to strive to get better in and helped me meet people who could be there for me in times of need.");
            text.setColor(Color.RED);
            Graphics g = img.getGraphics();
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, img.getWidth(null), img.getHeight(null));
            g.dispose();
            repaint();
            this.setVisible(false);
        }
    }
}

原始代码

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;
import acm.util.*;
import java.util.Random;
import javax.swing.*;
import java.awt.image.*;
import java.awt.Image;
import acm.graphics.GImage;

import acm.graphics.GObject;

public class test extends JFrame {

    ImageIcon hockeyPlayer = new ImageIcon("/home/rylie/Pictures/Capstone/HockeyPlayer.jpg");

    public void init() {
        Image hockey = hockeyPlayer.getImage();
        Image player = hockey.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH);
        hockeyPlayer = new ImageIcon(player);;
        JLabel pic = new JLabel();
        pic.setIcon(hockeyPlayer);
        add(pic, 0, 0);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int one = (dim.width);
        int two = (dim.height);
        setLocation(one, two);
        setSize(dim.width, dim.height);
    }
}

(新守则);

 java.lang.ClassCastException: test cannot be cast to java.applet.Applet     
     at sun.applet.AppletPanel.createApplet(AppletPanel.java:793)
     at sun.applet.AppletPanel.runLoader(AppletPanel.java:722)     
     at sun.applet.AppletPanel.run(AppletPanel.java:379)
     at java.lang.Thread.run(Thread.java:744)" (error)

0 个答案:

没有答案