moving images in java applet - java

The question is about java applet programming
I used a java applet to draw an image in the paint method using the following code:
g.draw(Myimage,0,0,this);
The image was drawn on the screen, But what i want to do is to be able to change the position of this image without clearing the screen and without drawing blank image in the previous position of the image..
Thanks in advance.

You can simply draw another image in other place. Just change the parameters in your code. For more details about graphics object follow the link. http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics.html
Other wise why dont you simply extend GraphicsProgram class provided by acm. It has a move function which will exactly do what you want to do. Check out the link.
http://jtf.acm.org/rationale/graphics-package.html
Hope this helps.

But what i want to do is to be able to change the position of this image without clearing the screen and without drawing blank image in the previous position of the image..
Then use a JLabel. When you want to move the label you use the setLocation() method. The RepaintManager will repaint the location where the label was and then paint the label in its new location so you don't have to worry about calculating the area affected by the move.

Related

how can I create a custom clickable shape on an image in android?

I want to create a clickable image, my image has some different clickable parts in it, like this one:
I want to draw a custom shape like :
A,B,C,D,E,F
and make sure when user click on of this something happen.
the problem is I don't have any kind of idea to, how create shapes like the shapes in the image make sure it just fix on the image and in different screen size don't see a massed up thing.
Will there be more than many of such images?
If no I suggest you to create mask image for each region where black part of image represents the region and white part excludes rest.
To draw image:
create custom View
in constructor don't forget to use setWillNotDraw to true so you can do custom drawing
override View.onDraw method where you can draw main image and all others with some filters via setColorFilter.
To handle click events:
override onTouchEvent method
get touch position
compare touch position with point color in mask image
To optimise:
create mask image downscaled by some scale factor
during comparison divide touch position by scale factor
This is not ideal, but solution with vectors is non trivial I think
Take it as image and setOnclickListner for that image

Create "invisible" graphics

I need Your help in generating offscreen images from graphics class and converting them to binary data without setting them visible on screen.
I want to generate large amounts of labels and send them to a printer as binary data. If i create the image, show it on screen and then send it, everything is ok, but i generate 100 labels in one minute and it is annoying when they flicker on the screen of my java aplication.
I want it to be generated by a seperate thread, without visible efects.
If i don't show them, the labels are send black. I tried to generate them, and then show them off screen, that did not match my expectations.
Is there a way to generate "invisible" images from Graphics2d??
or
Is there another way You could suggest me?
Thank You in advance
Qba
You can use a BufferedImage and use getGraphics() to get hold of a Graphics2D object that paints onto this image.
If you're after painting GUI components (if your "label" refers to JLabel for instance) you could have a look at these questions:
How to get a BufferedImage from a Component in java?
Java/Swing offscreen rendering (Cobra HTMLPanel -> BufferedImage) Problem: Component doesn't finish redrawing first

Selecting a part of Image while Cropping

I'm cropping a image in java swing, but when I select a part of image for cropping, I need to make selection part visible to user.
Like a transparent rectangle on that image.
GraphPanel shows one approach to rendering such a selection rectangle, mouseRect. The rendering is controlled by the boolean selecting attribute.
Addendum: Once you know the bounds of the selection, you can use getSubimage() to clone the corresponding part of the image. There's a related example here.

How to create a custom Java Swing GUI Component with a shape and gradient

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

Java: Is it possible to take a GUI Panel and output it into a picture?

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

Categories

Resources