Java Animated transparent splash screen - java

I'd like to make a splash screen with an animated gif. My animated gif has a transparent background so I'd like to display only the visible part of my gif as a splash screen.
First of all, I must specify that I'm using Matlab so it is not possible (or I haven't found how) to override components/functions.
Here my sample code :
win = javax.swing.JWindow;
jl = javax.swing.JLabel(javax.swing.ImageIcon('C:\Users\ME\Documents\loader512-.gif'));
win.getContentPane.add(jl);
win.setAlwaysOnTop(true);
win.pack;
%% set the splash image to the center of the screen
screenSize = win.getToolkit.getScreenSize;
screenHeight = screenSize.height;
screenWidth = screenSize.width;
% get the actual splashImage size
imgHeight = 512;
imgWidth = 512;
win.setLocation((screenWidth-imgWidth)/2,(screenHeight-imgHeight)/2);
win.show
It works very well shwing the image, however its background is ether white or gray depending on the transparency of the window.
I also tried to play with the background of the JLabel without success.
Please help !

The problem with your code is that every given window has a particular defined shape. Without changing the shape every Window object has a rectangular canvas in which it draws upon. The transparency only applies if you draw a object behind your GIF image. If transparency is enabled the renderer merely draws the background colour (which is normally black or white or some light gray) if nothing else is present.
Since the release of Java 7 a new method called setShape(Shape); appeared for Window objects. If your animated GIF has a fixed transparency "zone" I advise creating a custom java.awt.Shape object in which you pass to the win object via set shape.
Example:
win = javax.swing.JWindow;
jl = javax.swing.JLabel(javax.swing.ImageIcon('C:\Users\ME\Documents\loader512-.gif'));
win.getContentPane.add(jl);
Shape S = createCustomShape(); //Create your shape
win.setShape(S);
win.setAlwaysOnTop(true);
//{rest of code...}
If the animated GIF has a changing transparency background, you must create a complex implementation in which the Shape object updates on a frame by frame basis in response to the GIF. In my opinion, if this is the case, I wouldn't bother. :)
Read more on the Shape object here.
N.B. The Shape "object" is actually a interface, either use one of the implementing subclasses or create your own.

Related

How to draw text in world coordinate space?

I'm creating a game in libGDX. I want to create some UI elements (buttons and stuff), because of my app design, I would like to draw them in the world space, as other game objects.
I'm using Freetype generator that generates a bitmap font from true type font files(.ttf). The problem is that the dimension of the font is in pixels.
Orthographic camera that I use to to render the world, has viewport size of approximately 10x10, so when I generate a font at the size of 10, it covers almost whole screen(too big) and also looks very ugly because generated bitmap for the font is too small (too few pixels).
What I want is to create sprite, draw it at same size(world space) and draw text over it, and basicly create a button.
Is there some well established way how to deal with this?
Thanks to clarifying comments, I've came up with the solution.
I took a point at which I wanted to draw the text, projected it to the screen space by my world camera. Then I flipped y axis by:
point.y = viewportHeight - point.y;
Then I unprojected it with ScreenViewport (separate viewport for drawing the text, is uses camera of the size of the screen so 1unit == 1pixel).
Now I can draw text in projection where 1unit = 1pixel, on the point that is at the same place on the screen as previously chosen point in world space.
I also wanted to be able to draw text inside rectangular boundaries. For this I chose another point. At this point text should end. Did the same procedure as with start point, and then calculated width
targetWidth = endpoint.x - startpoint.x;
Then I used GlypthLayout class to get actual width of my text at some(generated) font size.
actualWidth = glyphLayout.width;
And when I scaled font like this
font.getData().setScale(targetWidth / actualWidth);
my font get scaled so drawed text is wide as target width.
But be aware of another problem! When I generate bimap font via FreetypeGenerator with size bigger when approximately 300, some letters don't draw, and are missing. (probably bug).

Scaling graphics drawing area Java Swing/awt

I am developing vector graphics editor in Java Swing/AWT technology.
I am curious about solution for creating drawing area (workspace), which have size bigger than users screen resolution.
For example: Window of creating new file
public WorkspaceComponent() {
setPreferredSize(new Dimension(**WIDTH**,**HEIGHT**));}
User wants to create a document of size e.g 1920 x 1080[px], but his/her screen has only 1280 x 720 [px].
Does anybody have an idea or solution for scaling the drawing area to match screen and after export having original size?
Using an AffineTransform on the Graphics2D Context you can scale the drawing area. Using this, you could even implement something like a zoom in/out feature.
double scaledSize = ... // do calculations of the scale here
AffineTransform t = new AffineTransform();
t.scale(scaledSize, scaledSize);
g2d.setTransform(t);
// do your drawing after setting the transform
If your scale is 1, it wont change the scale. A value less than one will make it smaller and a value greater than 1 will make it bigger.

Java zoom in on hi-res image without pixelation or losing drawing

I'm using the approach I found in this answer to create zoom in/out functionality for an image labeling application. However, despite dealing with relatively high resolution images (order of 1500x1500 pixels), the scaling method for zoom leads to pixelation. I checked the image in Windows Photo Viewer and I could definitely zoom in without much pixelation until much more than the 200% zoom I have in my code.
If I resize only compared to the original image (i.e. not cascading each resize call), this works just fine (this is what's in the code snippet). But in order to do this, I lose any drawing functionality. Basically, I would like to be able to do the following:
Load an image
Draw lines/circles/shapes/etc on it
Zoom in (maintaining any drawing on the image)
Draw more
Zoom in/out at will for whatever reasons I need
In summary, the resizing-for-zoom works. It's preserving any additional drawing at each stage of the zoom that I can't seem to get.
Here's a code snippet of my resize function (heavily influenced by that answer cited before):
private void resizeImage()
{
int newImgW = (int)(this.zoom * this.imgW);
int newImgH = (int)(this.zoom * this.imgH);
BufferedImage resized = new BufferedImage(newImgW, newImgH, originalImage.getType());
graphics2D = resized.createGraphics();
graphics2D.drawImage(this.originalImage, 0, 0, newImgW, newImgH, null);
this.image = resized;
repaint();
}

Scrolling BufferedImage Java

I'm making a simple side scroller game in Java but am having trouble making the camera move with the person. My plan was to create a buffered image that could adjust the region of the image that is displaying and simply have it follow my character. However, I couldn't find any functions in the API to change the region of the buffered image that's displayed, so how could I do this? Thanks for any help provided.
//The level is created in an 800x400 pixel size, is there any way I can change
//the region myImage displays?
myImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
Well while you will be drawing your image you have a lot of options for example this function can draw any rectangle from your original image on any rectangle of the surface you get Graphics object from.

Drawing a Translucent .png in AWT

I'm trying to draw a transparent.png image over a TV signal (so blending before rendering is kinda out).
When I was drawing a transparent rect, I could just set SrcOver and specify an alpha for my background color and it would work:
((DVBGraphics) g).setDVBComposite(DVBAlphaComposite.SrcOver);
But now I'm actually trying to use a png with transparency set and that doesn't work (it's just solid).
If I set XOR mode:
g.setXORMode(Color.white);
after setting SrcOver, the entire image becomes translucent, not just the specified dots. Also the parts set to "Transparent" are still visible.
Can anyone point me to the magic set of calls that makes this work?
(Also, swing isn't available--just most of java 1.4).
I can only suggest you recheck everything; I have painted PNG images using AWT before and it worked fine, just doing the obvious:
private final Image image; //
protected void paintForeground(Graphics2D gc, int wid, int hgt) {
...
gc.drawImage(image,dx,dy,(dx+width),(dy+height),0,0,width,height,null);
...
}
This paints with the image transparency honored, except on J2ME 1.1 platforms.
Drawing mode is normal paint, not XOR.

Categories