Is there anyway to find the current position of a buffered image on jpanel?
I draw an image on buffer, named it currentImage. Now I want to move it around a Panel by using affine transform.
To do translation by using mouse, I have to know the current position, new position and update the new position.
But I have trouble in getting the current position of the BufferedImage. There is no method in the IDE's suggestion working.
Can anyone give me an idea how to do this ?? Thanks!
EDIT
this is my draw and paintComponent method:
private void draw(){
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);
int w = this.getWidth(); int h = this.getHeight();
if (currentImage == null || width != w || height != h){
width = w;
height = h;
currentImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
graphics = currentImage.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
drawClearAndGradedArea(graphics);
drawActualRunway(graphics, width, height, width, height);
drawLeftToRight(graphics);
drawRightToLeft(graphics);
drawCentralLine(graphics);
graphics.setComposite(ac);
}
}
paintComponent()
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.clearRect(0,0, this.getWidth(), this.getHeight());
RenderingHints rh = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
/*
* main components on panel
*/
this.draw();
this.animation();
g2d.drawImage(currentImage, transform, this);
drawNote(g2d);
g2d.dispose();
}
Related
I'm trying to make a 2D sprite game, and as of now, the JLabel on which the image draws starts at the top left. This presents a problem when I try to move the sprite laterally because if the sprite that's supposed to come next is taller than the previous, it won't be standing on the floor as it should. In this picture, the left is the starting, the middle is what it does now with the top-left as 0,0, and the right is what it should be, screen-grabbed from the actual game.
below is my paint for the model of Mario:
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(sprite, 0,0, null);
}
and below is my paintComponent that's in a class that when run, renders the image onto the screen:
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
marioSprite.paint(g2);
}
and how I extract an image from a file:
//generate URL
public static URL urlGenerator(String name){
URL u = lookup().lookupClass().getResource(name);
return u;
}
//return image with filtered color
public static BufferedImage generateAndFilter(BufferedImage b, URL u){
try{
b = ImageIO.read(u);
int width = b.getWidth();
int height = b.getHeight();
int[] pixels = new int[width * height];
b.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++) {
if (pixels[i] == 0xFFff00fe) {
pixels[i] = 0x00ff00fe;
}
}
BufferedImage newSprite = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
newSprite.setRGB(0, 0, width, height, pixels, 0, width);
b = newSprite;
}
catch(IOException e){
System.out.println("sprite not found");
e.printStackTrace();
}
return b;
}
each instance of Mario has a sprite set by:
public void setSprite(String spriteName){
URL spriteAtLoc = Utils.urlGenerator(spriteName);
this.sprite = Utils.generateAndFilter(sprite,spriteAtLoc);
this.currentSpriteName = spriteName;
}
I'm trying to rotate an object in java but i noticed that something is wrong.
When i rotate it of 180 degrees,i get a value of the angle of '90°',so in order to get an angle of 360 degrees i have to rotate it twice.
What's wrong?
0°,90°,180°
The code:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (rotate == true) {
ship.increaseDegress();
}
ship.draw(g);
}
Ship.java
public void increaseDegress() {
rotationAngle += 10;
if(rotationAngle>360) {
rotationAngle = 0;
}
}
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.setTransform(at);
g2.draw(at.createTransformedShape(this));
}
So, let's take a quick look at your code...
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.setTransform(at);
g2.draw(at.createTransformedShape(this));
}
So,
First you create AffineTransform, nice
You rotate the transformer, also nice
You apply the transformer to the Graphics context ... okay, there's a problem here, but let's move on
You apply the transformer to the shape!
So, on a single draw pass, you will rotate the shape by rotationAngle * 2! So, when the angle is 10°, the shape will be rendered at 20°, when it's 20°, it will rendered at 40°!
Okay, but there's another problem. Transformations applied to a Graphics context are compounding, this means, based on the available code, each time you call draw, the Graphics context is been rotated by rotationAngle. So if rotationAngle is 10°
On pass #1, the shape will be rotated to 20°
On pass #2, the shape will be rotated to 30°
On pass #3, the shape will be rotated to 40°
... so on and so forth ...
So, what's the answer?
When ever I pass a Graphics context off to some other method, I first create a copy it, because I don't trust anybody!
Graphics gCopy = g.create();
shape.draw(gCopy);
gCopy.dispose();
This ensures that the state of the Graphics context is returned to the state it was before I called draw, that will get rid of the compounding transformation.
The other solution is, don't transform the Graphics context. If you're transforming the shape, what's the point?
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.draw(at.createTransformedShape(this));
}
I have a function that draws a rotated picture on a g2 component but for some reason I can't give it any colored background...
I was trying to use methods .setColor() and .setBackground() but with no use.
I have seen a similiar case here Graphics2D: Drawing black on white?
but it didn't really help. Here is my function:
public void rotateImage1(double degrees, ImageObserver o){
double sin = Math.abs(Math.sin(Math.toRadians(degrees)));
double cos = Math.abs(Math.cos(Math.toRadians(degrees)));
ImageIcon icon = new ImageIcon(this.spiral);
int w = icon.getIconWidth();
int h = icon.getIconHeight();
int neww = (int)Math.floor(w*cos+h*sin);
int newh = (int)Math.floor(h*cos+w*sin);
BufferedImage blankCanvas = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D)blankCanvas.getGraphics();
if(PhotoEdit.black)
g2.setColor(Color.BLACK);
else
g2.setColor(Color.WHITE);
g2.translate((neww-w)/2, (newh-h)/2);
g2.rotate(Math.toRadians(degrees), icon.getIconWidth()/2, icon.getIconHeight()/2);
g2.drawImage(this.spiral, 0, 0, o);
this.spiral = blankCanvas;
}
PhotoEdit.black is a boolean variable that is true if the user selected the checkbox with black background option.
You are setting the color in graphics, but you aren't drawing it to the panel.
You can use g2.setColor(...) and g2.fillRect(...) and specify coordinates that cover the whole panel, and then draw your image on top.
Docs for fillRect: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillRect%28int,%20int,%20int,%20int%29
Is it possible to do draw a rectangle with a given border thickness in an easy way?
If you are drawing on a Graphics2D object, you can use the setStroke() method:
Graphics2D g2;
double thickness = 2;
Stroke oldStroke = g2.getStroke();
g2.setStroke(new BasicStroke(thickness));
g2.drawRect(x, y, width, height);
g2.setStroke(oldStroke);
If this is being done on a Swing component and you are being passed a Graphics object, you can downcast it to a Graphics2D.
Graphics2D g2 = (Graphics2D) g;
Here's how to do this : Border with colored line with thickness 5.
Border linebor = BorderFactory.createLineBorder(new Color(0xAD85FF), 5);
**Tested code with buffered image with different thickness values**:
Graphics2D g = bufferedImage.createGraphics();
int height = //image height
int width = //image height
int borderWidth = //border thickness
int borderControl = 1;
//set border color
g.setColor(Color.BLACK);
//set border thickness
g.setStroke(new BasicStroke(borderWidth));
//to fix issue for even numbers
if(borderWidth%2 == 0){
borderControl = 0;
}
g.drawLine(0, 0, 0, height);
g.drawLine(0, 0, width, 0);
g.drawLine(0, height – borderControl, width, height – borderControl);
g.drawLine(width – borderControl, height – borderControl, width – borderControl, 0);
I'm drawing a transparent rendered image on top of a background image which is then displayed on a JPanel.
I need to get the bounds of the rendered image after it has been scaled and rotated.
So that the next time I call repaint it can be with
the clip of the rendered image.
Any ideas? thanks for your help.
#Override public void paintComponent(java.awt.Graphics g)
{
if(g != null)
{
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), null);
Graphics2D g2 = (Graphics2D)g.create();
double cx = sprite.getWidth() / 2.0;
double cy = sprite.getHeight() / 2.0;
AffineTransform at = AffineTransform.getTranslateInstance(xPos, yPos);
at.concatenate(AffineTransform.getScaleInstance(scale, scale));
at.rotate(theta, cx, cy);
g2.drawRenderedImage(sprite, at);
//get width and height of rendered image here?
g2.dispose();
}
Something like
Rectangle resizedBounds = at.createTransformedShape(sprite.getBounds()).getBounds();