Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RectangleImage
#1
I have developed a small class which syncs a rectangle with an image, allowing the seamless detection of collisions within an image using an invisible rectangle, if you will. This is extremely useful for game objects such as players, bullets, enemies, etc.

Hopefully you find it useful:
[code2=java]class RectangleImage
{
private Image img = null;
private Rectangle rect = null;

public RectangleImage(Image img, int x, int y)
{
this.img = img;
ImageIcon icon = new ImageIcon(img);
this.rect = new Rectangle(x, y, icon.getIconWidth(), icon.getIconHeight());
}

public Rectangle getRect()
{
return this.rect;
}

public Image getImg()
{
return this.img;
}

public void move(int x, int y)
{
this.rect.setBounds(x, y, rect.width, rect.height);
}

public void draw(Graphics2D g2, ImageObserver o)
{
g2.drawImage(this.img, this.rect.x, this.rect.y, this.rect.width, this.rect.height, o);
}

public boolean intersects(Rectangle r)
{
return this.rect.intersects®;
}

public Rectangle intersection(Rectangle r)
{
return this.rect.intersection®;
}

public void rotateImage(double degrees, ImageObserver o)
{
ImageIcon icon = new ImageIcon(this.img);
BufferedImage blankCanvas = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D)blankCanvas.getGraphics();
g2.rotate(Math.toRadians(degrees), icon.getIconWidth() / 2, icon.getIconHeight() / 2);
g2.drawImage(this.img, 0, 0, o);
this.img = blankCanvas;
}
}[/code2]
Pastebin link: <!-- m --><a class="postlink" href="http://pastebin.com/AC4aKBvx">http://pastebin.com/AC4aKBvx</a><!-- m -->
Rotation Pastebin Link: <!-- m --><a class="postlink" href="http://pastebin.com/6S9ADmXH">http://pastebin.com/6S9ADmXH</a><!-- m -->

The first tutorial on this code (moving images) can be seen here:
<!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=0MKLBh0fBbg&feature=youtu.be">http://www.youtube.com/watch?v=0MKLBh0f ... e=youtu.be</a><!-- m -->

The second tutorial on this code (collisions/intersections) can be seen here:
<!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=iO_RpUuDyeg&feature=youtu.be">http://www.youtube.com/watch?v=iO_RpUuD ... e=youtu.be</a><!-- m -->

The third tutorial on this code (rotation) can be seen here:
<!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=vxNBSVuNKrc&feature=youtu.be">http://www.youtube.com/watch?v=vxNBSVuN ... e=youtu.be</a><!-- m -->

Although the above code is used in the first three tutorials, I have since revised the code. The updated version is:

[code2=java]class RectangleImage extends Rectangle
{
//RectangleImage class created by brandonio21 --
//allows seamless integration of positioning/rotation
//with images. Extremely useful for games.
//http://brandonsoft.com
private Image img = null;

public RectangleImage(Image img, int x, int y)
{
this.img = img;
ImageIcon icon = new ImageIcon(img);
this.setBounds(x, y, icon.getIconWidth(), icon.getIconHeight());
}

public Image getImg()
{
return this.img;
}

public void move(int x, int y)
{
this.setBounds(x, y, width, height);
}

public void draw(Graphics2D g2, ImageObserver o)
{
g2.drawImage(this.img, x, y, width, height, o);
}

public void rotateImage(double degrees, ImageObserver o)
{
ImageIcon icon = new ImageIcon(this.img);
BufferedImage blankCanvas = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D)blankCanvas.getGraphics();
g2.rotate(Math.toRadians(degrees), icon.getIconWidth() / 2, icon.getIconHeight() / 2);
g2.drawImage(this.img, 0, 0, o);
this.img = blankCanvas;
}
}[/code2]
Revised Pastebin Link: <!-- m --><a class="postlink" href="http://pastebin.com/EdwqJE1g">http://pastebin.com/EdwqJE1g</a><!-- m -->
Revised Class Video Discussion:
<!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=JZSs_bqRij8&feature=youtu.be">http://www.youtube.com/watch?v=JZSs_bqR ... e=youtu.be</a><!-- m -->
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
RectangleImage - by brandonio21_phpbb3_import2 - 05-09-2013, 03:08 PM
Re: RectangleImage - by andrei.xd23 - 05-09-2013, 09:49 PM
Re: RectangleImage - by andrei.xd23 - 05-11-2013, 12:51 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)