Results 1 to 14 of 14
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default [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
    Augustas
    Last edited by augustas656; 04-09-2014 at 07:41 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

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

  4. #4
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

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

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default 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??
    What's wrong with Graphics2D? Besides, you ARE using a Graphics2D instance because that is what is passed to the Graphics argument of the paint methods. Graphics2D subclasses Graphics. You're only using the super class methods when you use it as a Graphics type.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

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

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default 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;
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?

    Quote Originally Posted by augustas656 View Post
    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?
    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default 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 04:29 PM.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?

    Quote Originally Posted by augustas656 View Post
    Wait, which way round is it?
    Graphics extends Graphics2D or
    Graphics2D extends Grapihcs??
    What did the API tell you?

    Java Platform SE 7
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

    Default Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?

    I edited my post after you replied, read now. P:

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?

    Quote Originally Posted by augustas656 View Post
    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.
    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    133
    Rep Power
    0

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

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: [Idea] Cutting out a specific SHAPE from an IMAGE, is it possible?

    Quote Originally Posted by augustas656 View Post
    All the tools that I need are already in Graphics, so I don't see why I need to use Graphics2D?
    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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 09-16-2013, 08:51 PM
  2. Replies: 7
    Last Post: 05-07-2012, 04:38 PM
  3. JScrollPane is Cutting off my Image upon Scrolling
    By Ryan.Vincent in forum AWT / Swing
    Replies: 6
    Last Post: 04-19-2012, 05:16 AM
  4. Drawing a shape on an image
    By Yoruichi in forum Java 2D
    Replies: 2
    Last Post: 03-29-2009, 02:49 PM
  5. Cutting parts of an image and reordering them
    By pedjacar in forum Java 2D
    Replies: 2
    Last Post: 10-16-2008, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •