I want to create a game where I want to have many image tiles which will respond to the arrow keys. Should I use BufferedImage to create every individual tile?
Refer to: Java Game Playing Area Difficulty
RobotChase is a tile-based game that uses BufferedImage in this way. Alternatives include these:
Implement the Icon interface, as shown in the examples cited here.
Set a component's text to a suitable Unicode glyph, as shown here.
Related
This is not a typical "what library do i use?" or "is there an API for that". Using lines of code I create not implementing them through a library of sorts. Is there any documentation on how to actually create a window, and within that window draw a line from pixels?
I just want to understand how its done, and not learn through a library which does it for me.
Previously tried:
JavaFx
Swing
If you want to draw a line in a window, you'll ultimately have to create a BufferedImage, set pixels in that BufferedImage according to some drawing algorithm (like Bresenham's), then put the BufferedImage in the window.
So basically I am building a GUI type interface for my website that needs to have multiple requirements.
Image to interact with i.e draw straight lines on it, curved lines, circles etc (different color lines too)
The image will need to have the option to "save" the current state and access it later
The image will have different objects on it that will have different "states" i.e active or not active etc.
What is the best way to accomplish this? I have looked into GUI builder and Canvas for HTML5 but have not found a solid guide to satisfy my needs. Any further assistance will be much appreciated!
You should be able to do that with the 2dContext provided by the canvas. This url gives you a list of all the methods
https://developer.mozilla.org/en/docs/Web/API/CanvasRenderingContext2D
That gives you options to draw circles, rectangles etc and you can set up click events and hit tests on those objects.
You can also save the canvas output to an image or an array.
I am a C/C++ programmer trying my hand at Java for the first time. I'm currently working on a program that reads in a bunch of data and builds a map. I want to give the user the option to toggle various features of the map using check boxes.
In the Win32 API, I was able to accomplish this by pre-building the features on transparent bitmaps and then BitBlt()ing them over top one another. Does Java Swing support something similar? I imagine I'm not the only person who has ever wanted to do this. Building the features is relatively slow, so I only want to generate the layers once and then block copy them to the JPanel I'm using as a display.
Thanks in advance!
You could dynamically create BufferedImage objects, with alpha channels, then only paint this on a frame if the checkbox is checked.
You can store images in Swing using the BufferedImage class, and then use that to later draw the final image.
http://docs.oracle.com/javase/6/docs/api/java/awt/image/BufferedImage.html
The final image can later be painted on to the JPanel (probably by overriding the JPanel's paintComponent method) by using the alpha values of the images.
I have to create a custom component on my JFrame, the Component will show the storage status of that user, the storage will be in percentage.
I need to create something like this:
I tried a custom JLabel to create a label and then coloring that label from left to right, but I was unable to create a cloud shape Label and then filling that label according to a variable value.
How should I do this and what is the best way to do it?
One time I thought I should use series of images to show the status of user storage.
Thanks!
I think you're going to need to use an image mask (examples here and here) if you are to replicate that cloud exactly.
The process will require 2 images:
The cloud outline (the blue parts)
An image mask is the shape of the cloud, probably black outside and white inside
Then your drawing process, which you'll have to do each time the % storage changes will be:
Create a new buffered image
Draw then green fill bar in the style you want (e.g. slanted as in this image)
Copy the image mask over this
Draw this new image to the screen, with the mask applied as described here
Draw the cloud outline image to screen
That's going to take an hour or so for you to put together, so I'm not going to do it for you. Have a go, and if you run in to problems (or don't understand anything I just described) then ask about that specifically.
You can use GlyphVector#getGlyphOutline() to get the shape of a Unicode character like ☁ \u2601 and fill it with a GradientPaint.
You can do that with a JLabel and a custom-implemented class derived from Icon.
doesn't this help? Java gradient label example
And if you understand (or can translate from) portuguese, there's also this discussion with a solution at the end
So I have this chart that's a little special. Kind of like an XY plot of points but my boss wanted to look like a bunch of boxes rather than dots connected by lines. And I basically made a chart using gridlayout and a whole bunch of cells that I'll be colouring in black or white depending on the data.
Now he sorta wants it to be outputted to a image file. Is there any way to save a Panel into a picture? He wants to display not only the data but also save a visual representation of the data into an image file.
Is there any way to save a Panel into
a picture?
Screen Image
You can create a Graphics context which paints to an image. This technique is often used with animations to prepare drawings offline and swap them in place to avoid artifacts.
Typically you can use the same paint methos as used to paint your canvas.
Here are more details