I try to get a subImage from the given image as shown below:
I know how to crop in rectangular shape like this
image = image.getSubImage(x, y, width, height);
but by using this method it is not possible for me to get different shape image from a given image, if anyone has suggestion can show me.
Thanks in advance!
You can do it via the Graphics class: http://docs.oracle.com/javase/tutorial/2d/advanced/clipping.html
Related
I am trying to draw a cushion like rectangle in processing like the pic shown. Is there any tricky way to use "light" to realize this? Does anyone have any idea about it? Thanks!
Pic reference: http://philogb.github.io/blog/2009/02/05/cushion-treemaps/
What you're talking about is called a radial gradient.
There are a number of ways to do it. One way would be to simply draw a bunch of circles. Here is a small example:
size(200, 200);
for(float diameter = 255; diameter > 0; diameter--){
noStroke();
fill(0, 255-diameter, 0);
ellipse(width/2, height/2, diameter, diameter);
}
You'll also have to limit your drawings to a rectangle shape. You might do that using the createGraphics() function to create a buffer, then draw the gradient to the buffer, then draw the buffer to the screen.
You should really break your problem down into smaller steps and take those steps on one at a time. First create a sketch that shows a simple gradient. Then create a sketch that uses a buffer. Get those both working by themselves before you combine them into one sketch. Good luck.
Another common method for implementing a collection of radial gradients of the type shown (a treemap) is:
create or acquire a single fairly high resolution image asset with a (jpeg/png) -- https://www.google.com/search?q=radial+gradient+box&tbm=isch
load the image
as you draw your boxes
optionally use tint() to tint the image green, red, etc. This works best with a grayscale source image.
scale your source image to the correct size for each box using the 5-argument image(img, x1, y1, x2, y2)
I need to crop an image starting at position x,y and it should be height,width in size (starting at x,y). I have the following code:
Thumbnails
.of(url)
.size(width, height)
.crop(new Coordinate(x,y))
.toOutputStream(os);
;
But it does not seem to be working. Any ideas?
Thumbnails
.of(url)
.scale(1)
.sourceRegion(x, y, width, height)
.toOutputStream(os);
did the trick! Too bad the library's documentation is so lacking. It is a very nice one.
I know that one can use g2d.setPaint(new TexturePaint(img,rect)) in order to fill a shape with an Image, but the problem is that the shape is tessellated with the image instead of rescaling the single image to the bounding box of the shape in order to fill the whole area.
The latter is what I want to achieve.
For example, if I have an image of 20x20 and I want to fill an Ellipse2D of width = 200 and height = 300, I would like to fill that Ellipse2D with a rescaled version of 200x300 of the image instead of tessellating it with the 20x20 image.
Is there a way to achieve this and hopefully in an efficient way?
Try this:
g2d.setPaint(new TexturePaint(img.getScaledInstance(200, 300, 0),rect));
I never tried this out before but it seems like it should work.
I want to add an image to another image at specific position either it would be pixels or cm or (x,y). I have to do it by java programming using JAI(Java Advanced Imaging).
I googled to get sample code.
I got replacing ,adding at the end only.But not at specific position.
Any idea is appreciatable.
Laxman chowdary
BufferedImage sourceImage = ImageIO.read("myImage");
BufferedImage overlayImage = ImageIO.read("myOverlay");
Graphics2D g2Source = sourceImage.createGraphics();
g2Source.drawImage(overlayImage, x, y, null);
g2Source.dispose();
So I am working on an Android game and I have two images on the screen. I currently have the pixel-perfect collision detection working great. My problem is when I rotate one of the images and I check the pixels for a collision. The pixels are still oriented in the original way the image was loaded, so it is not as perfect as it was... I can get all of the pixels in an array, or a 2d array. But I am currently just accessing them by the getPixel(x, y) method in the Bitmap class.
Does anyone know of an algorithm to rotate the values in an array based on an arbitrary number of degrees? Or any other way of solving this problem?
Have you looked at AffineTransform?
It's what I have used to rotate sprites and images in the past.
I used this code to rotate and get a pixel value from an image:
Image rotatedImage = new BufferedImage(imageToRotate.getHeight(null), imageToRotate.getWidth(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
// Set rotation here
g2d.rotate(Math.toRadians(90.0));
g2d.drawImage(imageToRotate, 0, -rotatedImage.getWidth(null), null);
g2d.dispose();
int pixelColor = ((BufferedImage)rotatedImage).getRGB(x, y);