Results 1 to 14 of 14
- 04-09-2014, 08:35 PM #1
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
[Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
I'm making my 2D Java game, in my own style, in my own way. I'm trying to make it as code efficient as possible and compact, it usually is easier to change that way. I'm going to use BufferedImage subdivision on tilesets to take out tiles that I need, rather than creating multiple images with a tile each. Now, I've made multiple water tiles, for how they would be arranged differently.
So let's say you have a single water spot, it will be circular, you have two water spots, you have a tile-thick line with circular ends like two little water spots connected, this makes the game look more advanced, better, more realistic and good in many other ways. But, I found that an avarage amount of shapes needed is 25. I thought, how could I make this more efficient?
Could it be possible to use like shape cutting tools or something that would take out a specific shape of a single water tile to leave out all the different shapes, this is a good idea because I could use these tools for all kinds of different tiles in order to make specific features. By tools I mean simple black shapes which will remove the unwanted parts of the tile to form the wanted shape.
Thanks
AugustasLast edited by augustas656; 04-09-2014 at 08:41 PM.
- 04-09-2014, 08:55 PM #2
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
Is it possible? Sure. Just use clipping: Clipping the Drawing Region (The Java™ Tutorials > 2D Graphics > Advanced Topics in Java2D)
Is this method better than just using a pre-drawn image set? Debatable.How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-09-2014, 09:50 PM #3
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
Well, I'm sure it is better because if I need 25 shapes for a single tile, why not use a 25 shape pattern for multiple tiles needed which would save me 25 * tileamount of drawings.
- 04-09-2014, 10:04 PM #4
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
Actually, one problem, I don't like using Graphics2D, could I turn a cut-out (clipped) shape into a buffered image which I could then draw using Graphics or something??
- 04-10-2014, 12:09 AM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
Actually, one problem, I don't like using Graphics2D, could I turn a cut-out (clipped) shape into a buffered image which I could then draw using Graphics or something??
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-10-2014, 03:49 AM #6
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
You see how you can Graphics g, g.drawImage(bufferedimage, int x, int y, int width, int height, null);
So it draws the bufferedimage, filling the width and height of the x and y top-left corner.
So g.drawImage(bufferedimage, 0, 0, 10, 10, null);
Would draw my image's top-left corner at (0, 0) and fit the image into a 10 long and 10 high space.
Yes?
Now, I do believe I can use Graphics 2D, in this exact same way, but how could I first of all cut the image
out and then draw it on x, y and fit it in the width and height aswell?
I'm thinking I'm barely going to use anything from Graphics 2D so why not just maybe using Graphics 2D or
not, make a BufferedImage already CUT-OUT so I can draw it with Graphics?
So, either how could I do x, y, width, height WITH cutting out, or how can I make a bufferedimage variable ALREADY cut out which I can then print with Graphics?
- 04-10-2014, 05:01 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
You asked a lot of questions which would be difficult to explain. Here is a small snippet
of code which I believe demonstrates what you want to do. I didn't have an image handy
so I made a checkerboard. This is basically in the tutorials except for setting a smaller clip
and scaling the image on the fly. Please read the tutorials. They can be very helpful.
Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; public class ImageDemo extends JPanel { private JFrame frame; private final static int width = 500; private final static int height = 500; private Image img; private Ellipse2D.Double circle; private int diameter = 150; public ImageDemo() { frame = new JFrame(); setPreferredSize(new Dimension(width, height)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { new ImageDemo().start(); } public void start() { circle = new Ellipse2D.Double(0, 0, diameter, diameter); img = createCheckerBoard(500); repaint(); } public void paintComponent(Graphics g) { if (img == null) return; super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setClip(circle); g2d.drawImage(img, 0, 0, diameter, diameter, null); g2d.dispose(); } private static Image createCheckerBoard(int boardSide) { BufferedImage img = new BufferedImage(boardSide, boardSide, BufferedImage.TYPE_INT_ARGB); boolean flag = true; Graphics g = img.getGraphics(); int squareSide = boardSide/20; for (int ww = 0; ww <= boardSide; ww += squareSide) { for (int hh = 0; hh <= boardSide; hh += squareSide) { g.setColor(flag ? Color.black : Color.white); g.fillRect(ww, hh, squareSide,squareSide); flag = !flag; } } g.dispose(); return img; } }
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-10-2014, 05:10 PM #8
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
Your questions indicate that you don't really know what you're doing (that's not an insult!). I highly recommend taking a step back until you really understand what's going on. Read the tutorials you've been linked to in other threads. Start smaller.
You're already using Graphics2D. Even if you use a BufferedImage, you're still going to need Graphics2D to draw that image. It doesn't make any sense to try avoiding Graphics2D, it's not like it adds any overhead to your project.
The only overhead it does add is calculating these images on the fly, which is why I said it isn't inherently better than just using pre-drawn images.How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-10-2014, 05:26 PM #9
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
If I use Graphics, I'm not using Graphics 2D, but if I'm using Graphics 2D I'm using Graphics.
The very reason for me not knowing a lot is because when I learnt java I wanted to make a game, so I straight away went to youtube Java Game Tutorials, eventhough the tutorials I watched were very (in my opinion) messy, and you almost had to copy and paste all of the time. So yeah, and now I'm starting to build up on my own.
All the tools that I need are already in Graphics, so I don't see why I need to use Graphics2D? There's a method in Graphics called clip, I could just make it a little more advanced if I needed and that's it! :)
Maybe I'm wrong, but this is what's on my mind.Last edited by augustas656; 04-10-2014 at 05:29 PM.
- 04-10-2014, 05:28 PM #10
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
What did the API tell you?
Java Platform SE 7How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-10-2014, 05:42 PM #11
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
I edited my post after you replied, read now. P:
- 04-10-2014, 05:47 PM #12
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
Graphics2D extends Graphics. However, if you're using a Graphics instance, that instance is probably actually an instance of Graphics2D. You wouldn't be able to cast it to Graphics2D if it wasn't. That's why people keep telling you that there is no overhead to using Graphics2D over Graphics: you're already doing it!
You need to stop reinventing the wheel and biting off more than you can chew. Start smaller, with tutorials that take you through the basics first: http://staticvoidgames.com/tutorials/How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-10-2014, 05:50 PM #13
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
Okay, okay I will read the tutorials, but I mean there literally isn't a single statement 2D in the whole of my game, and I think in the Graphics awt, Graphics2D doesn't exist as a variable or thing anywhere. I'm I right?
When I finish this debate or something, whatever it is, I'll go the tutorials, I want to have clarity on the things I already started debating / talking about. In this thread and also in canvas without extending it.
- 04-10-2014, 05:51 PM #14
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?
That may be true. But if you have one saw you may not believe you need another. Until you decide to rip a piece of wood with a cross cut saw. You can do it. But the rip saw is much easier (and gives a better cut). Same for programming. There are other methods you may want to try out that may make your task easier. RenderingHints comes to mind as well as the various AffineTransforms.
In fact, using rendering hints you should draw a filled circle with anti aliasing on and then off to see the difference.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Moving Shapes Program. Cannot remove first shape when new shape is clicked.
By VettesRus in forum New To JavaReplies: 3Last Post: 09-16-2013, 09:51 PM -
How to create JFrame having shape and size equel to image?
By pratik041 in forum AWT / SwingReplies: 7Last Post: 05-07-2012, 05:38 PM -
JScrollPane is Cutting off my Image upon Scrolling
By Ryan.Vincent in forum AWT / SwingReplies: 6Last Post: 04-19-2012, 06:16 AM -
Drawing a shape on an image
By Yoruichi in forum Java 2DReplies: 2Last Post: 03-29-2009, 03:49 PM -
Cutting parts of an image and reordering them
By pedjacar in forum Java 2DReplies: 2Last Post: 10-16-2008, 10:46 AM
Bookmarks