0

I watched a tutorial on YouTube on how to display an image in Java, and he used something called "Applet" and "Graphics" to display it, I've got it to work, and I am happy with it. Now what I was planning to make is a chrome logo in the center of my screen, transparent with no background, and then chrome opens. I've called the class CustomChrome cus you know. Custom and stuff. I just want a cool opening whenever I start chrome.

Here is the current code:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;

public class CustomChrome extends Applet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Image logo = null;

    public void paint(Graphics g) {

        this.setSize(960, 540);

        if (logo == null)
            logo = getImage("/chromelgo.png");

        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(logo, 0, 0, 960, 540, this);
    }

    public Image getImage(String path) {

        Image tempImage = null;
        try {

            URL imageURL = CustomChrome.class.getResource(path);
            tempImage = Toolkit.getDefaultToolkit().getImage(imageURL);
        } catch (Exception e) {

            System.out.println("An error occured - " + e.getMessage());

        }

        return tempImage;
    }


}

What I want to do, however, is the make the background + the window to vanish, Once that's was done I will set the image and window size to 1920x1080. How do I go forward on making the window behind the image disappear? I've heard something about implementing ActionListener, but still I ain't sure what to do.

Keep in mind! I have no experience with java so sorry for upsetting you if you try to help me :P

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Marius
  • 73
  • 1
  • 10
  • 1
    Welcome to the wonderful world of fast last technology - Applets are effectively deprecated and you shouldn't use them anymore. As a general feedback, paint is for painting, you should change the state of the component within it (and you shouldn't be calling setSize in an applet anyway); Consider using ImageIO.read over Toolkit – MadProgrammer Apr 20 '17 at 20:02
  • 1) 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. Note also that a `JApplet` is the Swing equivalent of `Applet`. 2) To expand on the comment of @MadProgrammer, see [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). – Andrew Thompson Apr 20 '17 at 22:45
  • Thanks for helping me! :) – Marius Apr 21 '17 at 21:17

1 Answers1

1

Okay, let's start with the really obvious issue, applets are effectively deprecated and you should stop using them, see Java Plugin support deprecated and Moving to a Plugin-Free Web for more details.

paintXxx is for painting, you should never change the state of a component from within a paint method (and you should not be calling setSize in an applet anyway). See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works and how you can make use of it

As a general piece of advice, I'd recommend ImageIO.read over Toolkit#getImage or ImageIcon for loading images, apart from throwing an Exception when the image can't be read (instead of failing silently), it's also a blocking call, meaning that when it returns, the image is fully loaded.

See Reading/Loading an Image for more details

A word of warning - What you're trying to do is not difficult per se, but it's involved and requires a certain amount of knowledge about how the API works.

Now what I was planning to make is a chrome logo in the center of my screen, transparent with no background, and then chrome opens.

Okay, well, this was never going to work with applets, as applets are intended to be displayed inside a browser, so you can't control the window, instead, you need something like a JFrame, see How to Make Frames (Main Windows)

What I want to do, however, is the make the background + the window to vanish, Once that's was done I will set the image and window size to 1920x1080. How do I go forward on making the window behind the image disappear?

Now, this is where the real fun begins. You're going to want to start by having a look at How to Create Translucent and Shaped Windows. This will allow you control the opacity of the frame, making it disappear, but allowing the content to remain, there are some tricks you need to do to pull it off, but that's the basic idea

So, this example will make a transparent window and display and image centered within it.

Example

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }

                    BufferedImage img = ImageIO.read(getClass().getResource("/Chrome.png"));
                    ImageIcon icon= new ImageIcon(img);
                    JLabel label = new JLabel(icon);

                    JFrame frame = new JFrame("Testing");
                    frame.setUndecorated(true);
                    frame.setBackground(new Color(0, 0, 0, 0));
                    frame.add(label);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

}

In this is example, I make use of a JLabel to display the image, in most cases, it's all you really need, see How to Use Labels for more details.

What I want to do, however, is the make the background + the window to vanish

Well, it just comes down to what you mean by vanish. You could just all dispose or setVisible on the window, which would close it, all you could use some animation to make it fade away ... and because I like animation...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }

                    BufferedImage img = ImageIO.read(getClass().getResource("/Chrome.png"));
                    ImageIcon icon = new ImageIcon(img);
                    JLabel label = new JLabel(icon);

                    JFrame frame = new JFrame("Testing");
                    frame.setUndecorated(true);
                    frame.setBackground(new Color(0, 0, 0, 0));
                    frame.add(label);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);

                    Timer timer = new Timer(40, new ActionListener() {
                        float opacity = 1.0f;
                        float delta = 0.05f;

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            opacity -= delta;
                            if (opacity < 0.0f) {
                                opacity = 0.0f;
                                ((Timer)(e.getSource())).stop();
                                frame.dispose();
                            }
                            frame.setOpacity(opacity);
                        }
                    });
                    timer.setInitialDelay(2000);
                    timer.start();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

}

Okay, there is a lot going on here, you'll want to have a read through Concurrency in Swing and How to use Swing Timers for more details, needless to say, it's a little complicated to get started with.

Basically, all this does is, waits 2 seconds and then every 40 milliseconds, decreases the frames opacity by a factor or 0.05 till it reaches 0 and it then disposes of the window.

Now, I might use a mixture of these techniques, for example, I might show the logo frame (maybe set to display on top), I would then show the main frame and then trigger the logo frame to fade out. This could be done from a controller class to simplify the code, but, that's up to you.

A would also suggest having a look at Creating a GUI With JFC/Swing

halfer
  • 19,824
  • 17
  • 99
  • 186
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • This works flawlessly! Thank you for your assistance, and thanks for helping me getting started! I will check out everything :) – Marius Apr 21 '17 at 21:16