BP Forums
RectangleImage - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+----- Forum: Code Snippets (https://bpforums.info/forumdisplay.php?fid=32)
+----- Thread: RectangleImage (/showthread.php?tid=782)



RectangleImage - brandonio21 - 05-09-2013

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 -->


Re: RectangleImage - andrei.xd23 - 05-09-2013

hmmm, very interesting. I'm looking forward for those tutorials <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->


Re: RectangleImage - andrei.xd23 - 05-11-2013

ok so i did everything like you and then implemented the keylistener into the main class. but i don't know how to define the "x" and "y" in the KeyPressed method

Edit:I figured it out <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->if someone is interested: http://pastebin.com/3VKU2Xt7


Re: RectangleImage - brandonio21 - 05-12-2013

andrei.xd23 Wrote:Edit:I figured it out <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->if someone is interested: http://pastebin.com/3VKU2Xt7
Awesome! I'm glad you were able to integrate my code into your own application. Good work!