Draw Rectangle Over Buffered Image Java - java

I have a JPanel on which I draw the image of a map using a BufferedImage. I also have a button which, when clicked will highlight a part of the map by drawing a red rectangle over that part.
However, when I zoom in on the image, the rectangle stays where it was before. I assume the reason behind this is that the rectangle is being drawn over the panel and over the Image.
Is there a way to adjust the rectangle's position in accordance with the zoom?
Here's how I draw the rectangle and the image:
//the variable years[activeIndex] is an array which stores my Buffered Images
Graphics2D g2=(Graphics2D)g.create();
g.drawImage( years[activeIndex],0, 0,(int)width,(int)height,null);
g2.setColor(Color.red);
g2.setStroke(new BasicStroke(3));
g2.drawRect(this.getWidth()/2,this.getHeight()/2, 20, 20);
MapViewer.imagePanel.revalidate();
This is the rectangle when there is no zoom
This is the rectangle with the zoom, it stays in its place because its on the JPanel and not on the BufferedImage

Related

Drawing a circle on a bufferedImage in Java

I want to draw a circle on a buffered image that act like a png
i want to use this circle in order to replace the mouse cursor for a paint application i am working on.
i cant download a circle png from google as i need to keep changing the size of this circle depending on the width of the tool i am using it for.
here is my attempt so far
public static BufferedImage getCircle() {
BufferedImage bufferedImage = new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB);
Color transparent = new Color(0x00FFFFFF, true);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
//trying to make the bufferedImage transparent
g.setComposite(AlphaComposite.Src);
g.setColor(transparent);
g.setBackground(transparent);
g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
//drawing the circle
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.black);
g.drawOval(0, 0, 200, 200);
return bufferedImage;
}
it should look like:
However my code currently only creates a white square.
Your code has two problems (as already shown in the comments). The first is that you draw a circle with a radius of 200px into an image of dimensions 30px. If you closely look you can barely see a black pixel in the lower right corner.
Fix it by adjusting your dimensions such that it fits inside, for example like:
BufferedImage bufferedImage = new BufferedImage(60, 60, BufferedImage.TYPE_INT_RGB);
...
g.drawOval(5, 5, 50, 50);
Next is that you want to achieve a transparent background. To do so you need to set the type of the image to a color model which supports transparency, like ARGB (A = Alpha = transparency) instead of RGB:
BufferedImage bufferedImage = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);
Last you probably want to increase the thickness of your border to achieve the image you showed. You do so by using g.setStroke(...):
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(5));
g.drawOval(5, 5, 50, 50);
With this setting you achieve the following result (with transparency):
Play with the values to adjust the circle to your exact needs.

Clipping polygon image from jpeg image Java

guys!
I have some problem. I need cutting some polygon image from jpg image and save it.
In this moment I use OpenSlideJNI.openslide_read_region, but OpenSlide can clip the only rectangle.
Do you have any idea?
The basic code would be:
// load the image
BufferedImage originalImage = ImageIO.read(...);
// create the polygon
Polygon polygon = new Polygon();
polygon.addPoint(50, 50);
polygon.addPoint(150, 50);
polygon.addPoint(250, 150);
polygon.addPoint(150, 150);
Rectangle bounds = polygon.getBounds();
// create a transparent clipped image based on the bounds of the Polygon
BufferedImage clippedImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = clippedImage.createGraphics();
polygon.translate(-bounds.x, -bounds.y);
g.setClip(polygon);
g.drawImage(originalImage, -bounds.x, -bounds.y, null);
// save the clipped image
ImageIO.write(...);
Of course the image will still be rectangular, but the non clipped areas will be transparent.

How to display the whole contents of a large JPanel in a small JFrame?

I have a JPanel with a size of 816 x 1056 pixels (size of a standard typewriting paper). Is there any way of displaying the whole JPanel and its content without altering its actual size, in a small JFrame? Is there a way of like zooming it out?
I can't think of an easy method to zoom a JPanel out, but you can do it via double-buffering:
private void paintPanel (Graphics2D g)
{
// here goes the code of your paint() or paintComponent() method
}
/** Draw content into an Image, shrink it, and display it. */
protected void paintComponent (Graphics g)
{
// paint image
BufferedImage img = new BufferedImage(MAX_WIDTH, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
paintPanel(img.createGraphics());
// shrink image and paint it
g.drawImage(img, 0,0, WIDTH,HEIGHT, 0,0, MAX_WIDTH,MAX_HEIGHT, null);
}
This will paint everything on a BufferedImage with the size MAX_WIDTH x MAX_HEIGHT. Then, the Image will be shrinked into the size WIDTH x HEIGHT and then displayed.

Is there any way of drawing a variable rectangle using the Graphics class

I have coded a simple Java game where there are two rectangles on the screen, one of the rectangles moves and the other stays still, the moving Rectangle moves with keyboard arrow input and can move either up, down, left or right. The problem I am having is drawing my rectangles on the screen, I mean I have the 2 rectangles set up with my variables as shown:
Rectangle rectOne = new Rectangle(shiftX, shiftY,90,90);
Rectangle rectTwo = new Rectangle(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
I have made a render method to draw the things on the screen which I want shown:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
}
The problem I am having is showing my rectangles on the screen by writing the code in the render method, I could do the following:
g.fillRect(x, y,90,90);
g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
Which makes 2 rectangles on the screen but I need the rectangles to be drawn using the Rectangle code written with the variables, I have been told this can be done using Graphics2D but I am wondering if there is a simpler way of just using the graphics function, if not could you please help me set this up?
Thank you in advance.
You can access attributes of the Rectangle instances easily:
g.fillRect(rectOne.getX(), rectOne.getY(), rectOne.getWidth(), rectOne.getHeight());
In any case mind that usually the Graphics object is a Graphics2D instance at runtime so this could work easily too:
Graphics2D g2d = (Graphics2D)g;
g2d.fill(rectOne);
Just use:
g.fillRect(myRect.getX(), myRect.getY(), myRect.getWidth(), myRect.getHeight());
where myRect is the rectangle you want to draw. You could even make a custom method drawRect(Graphics g, Rectangle myRect); if you have lots of rectangles to draw.
Using Graphics2D is not that difficult as well, as the provide Graphics object normally is a Graphics2D object:
Graphics2D g2d = (Graphics2D) g;
g2d.fill(myRect);

Problem in generating the border of a rectangle in Java?

I am using java.awt.geom.Rectangle2D.Double class to generate a rectangle. I want to generate a rectangle which is filled with a color (say green) and have a border (outline).
Now the problem is if I call
g2.draw(new Rectangle2D.Double(....)); // g2 is an instance of Graphics2D
then it doesn't fill the rectangle and when I call
g2.fill(new Rectangle2D.Double(....)); // g2 is an instance of Graphics2D
then id doesn't generate border.
To do this, render the rectangle twice, first the fill and then the border (draw).
Rectangle2D rect = new Rectangle2D.Double(...);
g2.setColor(Color.white);
g2.fill(rect);
g2.setColor(Color.black);
g2.draw(rect);
How about doing both? Draw the filled rectangle first and then draw the outline one over the top.

Categories

Resources