0

I am trying to essentially make a slideshow using java. I have tried to display images using several different methods, including Buffered Image, GImage, and ImageIcon/JLabel. I want to be able to remove the picture when the spacebar is hit, so that I can display some text as well which is why I moved away from Buffered Image. I am using the ImageIcon/JLabel tight now, but I am getting an error. Can anyone help me resolve this error.

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

Original code

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

(New Code);

 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)
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
R.B
  • 1
  • Please post your code to figure out what is the problem. – Fady Saad Apr 17 '17 at 00:59
  • 1
    Are you trying to load `test` as an applet? Because that's impossible – MadProgrammer Apr 17 '17 at 01:00
  • @MadProgrammer hit the nail on the head. I also note use of the `acm` package aimed at students, which leads me to ask.. 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Apr 17 '17 at 22:39

0 Answers0