-2

In my comp sci class we are supposed to create a project that uses jswing. I want to create a desktop pet.My current ideas is using "setUndecorated(bool)" to make everything but the image panel blank, and then swapping between photos in an array list while moving the frame on the users the screen by using "setLocation(int, int)" to create the illusion that the pet is walking.

The only problem with this is that A: Its seems very inefficient and there's most likely a better way of doing this that i just have not found, And B: the square shape means that the background of the image is included in the frame. for ex:no, tom Brady is not the pet, this is just an example Id like for only tom to be visible here, no white space.

Is there a way to do this? or is there a better way of doing this whole project that i haven't thought of?

pretty sure the answer is "no, and your a complete psychopath who needs to be exiled to Siberia for even thinking of doing this on jswing" but i thought id ask.

  • 1. No, essentially it's fine and is very common idea of animation (see flip book or sprite animation); 2. Use an image which is already transparent – MadProgrammer Mar 15 '23 at 20:41
  • For [example](https://stackoverflow.com/questions/21771215/how-to-draw-images-on-transparent-window/21771615#21771615); [example](https://stackoverflow.com/questions/14927980/how-to-make-a-transparent-jframe-but-keep-everything-else-the-same/14928212#14928212); [example](https://stackoverflow.com/questions/13588786/jframe-the-same-shape-as-an-image-program-running-in-background/13594794#13594794); [example](https://stackoverflow.com/questions/26205164/java-custom-shaped-frame-using-image/26208568#26208568); – MadProgrammer Mar 15 '23 at 20:49
  • [example](https://stackoverflow.com/questions/42867413/are-transparent-jframe-backgrounds-not-possible-in-windows-10/42868181#42868181); [example](https://stackoverflow.com/questions/43528495/java-applet-make-the-background-transparent/43529285#43529285); – MadProgrammer Mar 15 '23 at 20:49
  • Instead of moving the `JFrame`, maximize the `JFrame`. Create a drawing `JPanel` and move the image around the drawing `JPanel`. Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) section. – Gilbert Le Blanc Mar 15 '23 at 21:00
  • @GilbertLeBlanc That could prevent mouse events from falling through the window, but I could be wrong, personally, I like the idea of moving the window, but that's me – MadProgrammer Mar 15 '23 at 21:01
  • Sprite animation for [example](https://stackoverflow.com/questions/35472233/load-a-sprites-image-in-java/35472418#35472418) and [example](https://stackoverflow.com/questions/27933159/how-to-draw-an-bufferedimage-to-a-jpanel/27933189#27933189) – MadProgrammer Mar 15 '23 at 21:01
  • *"your a complete psychopath"* and yes, but it's going to help ... and welcome to the club! – MadProgrammer Mar 15 '23 at 21:03

1 Answers1

-1

You could use this and add an Image which supports transparency (e.g. a png, jpeg does not support transparency)

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class TransparentWindow extends JFrame {

    public TransparentWindow() {
        super("Transparent Window");

        setUndecorated(true);

        setBackground(new Color(0, 0, 0, 0));// only the alpha is important

        setSize(500, 500);

        this.getContentPane().setBackground(new Color(255, 0, 0, 0));

        // add an image which has transparency instead of the label
        JLabel comp = new JLabel("test");
        comp.setFont(comp.getFont().deriveFont(200f));
        this.getContentPane().add(comp);

        // Show the window
        setVisible(true);

    }

    public static void main(String[] args) {
        // Create a new instance of the window
        TransparentWindow window = new TransparentWindow();
    }
}
JavaMan
  • 1,142
  • 12
  • 22
  • 1
    `this.getContentPane().setBackground(new Color(255, 0, 0, 0));` is not how you make a component transparent - Swing component's can't handle alpha based colors – MadProgrammer Mar 15 '23 at 20:39
  • @MadProgrammer actually, if you try the code the Background will be transparent, and as soon as you remove the line you've mentioned it will no longer be transparent – JavaMan Mar 16 '23 at 15:39
  • Which is irrelevant, using a alpha based color is NOT how you make Swing components transparent (I accept the alpha color of the frame as this is how IT works). Swing components do not know how to make use of alpha based colors and this approach will cause no end of issues and weird side effects. Take a look at any of the many linked examples in the comments – MadProgrammer Mar 16 '23 at 20:21