Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Fliping image horizontally  (Read 1873 times)
0 Members and 1 Guest are viewing this topic.
Offline xxdaggerxx

Junior Newbie





« Posted 2006-01-09 11:24:01 »

Hi guys, i need help, im a java newbi and i have been trying this for days now and with out any luck.
I need to flip an image horizontally about its x axis. I tried to negative the width (-image.getWidth())
but i get really wierd results, the colour gets all messed up.

Im using  Accelerated Java 2D, based on this tutorial found here.
http://www.cokeandcode.com/node/6

I just need to figure out to flip images that are Accelerated. See the tutorial for more info.
thx for your help guys, i really appreciate it.
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #1 - Posted 2006-01-09 11:42:58 »

Hm. Well, you can flip horizontally with that drawImage method that takes two rect coords.

javadoc

弾幕 ☆ @mahonnaiseblog
Offline xxdaggerxx

Junior Newbie





« Reply #2 - Posted 2006-01-09 11:57:07 »

Nope, tried that already, the colour gets all messed up again.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #3 - Posted 2006-01-09 12:02:17 »

Colors were fine here with that. I used that technique in JM4k.

弾幕 ☆ @mahonnaiseblog
Offline Ask_Hjorth_Larsen

Junior Duke




Java games rock!


« Reply #4 - Posted 2006-01-09 19:02:56 »

I've seen the problem before on windows using NVidia drivers. I believe it was in 1.5.0, but I'm not sure. On the same system I've used AffineTransforms that worked quite well, so I'm really not sure what the problem might be.  There is something called java.awt.AlphaComponsite , it could have something to do with that. I think that was one of the differences between the case when it worked and when it didn't.
Offline xxdaggerxx

Junior Newbie





« Reply #5 - Posted 2006-01-10 05:42:14 »

So theres nothing i can do about this? I am using a nividia driver. I really need help with this.
Offline tusaki

Junior Duke


Medals: 1


In a mad world only the mad are sane.


« Reply #6 - Posted 2006-01-10 09:46:50 »

try this:

1  
g.drawImage(myImage, AffineTransform.getScaleInstance(1,-1), null);


if it doesn't work, please provide your source code, so we can have a closer look.
Offline xxdaggerxx

Junior Newbie





« Reply #7 - Posted 2006-01-10 12:17:33 »

I cant i get an error message.
G:\JFiles\WorkingTitle\Sprite.java:55: cannot find symbol
symbol  : method drawImage(java.awt.Image,java.awt.geom.AffineTransform,<nulltype>)
location: class java.awt.Graphics
                g.drawImage(image,AffineTransform.getScaleInstance(1,-1),null);
                 ^


This is in the Sprite class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
public class Sprite {
   /** The image to be drawn for this sprite */
   private Image image;
   
   /**
    * Create a new sprite based on an image
    *
    * @param image The image that is this sprite
    */

   public Sprite(Image image) {
     
      this.image = image;
   }
   
   /**
    * Get the width of the drawn sprite
    *
    * @return The width in pixels of this sprite
    */

   public int getWidth() {
      return image.getWidth(null);
   }

   /**
    * Get the height of the drawn sprite
    *
    * @return The height in pixels of this sprite
    */

   public int getHeight() {
      return image.getHeight(null);
   }
   
   /**
    * Draw the sprite onto the graphics context provided
    *
    * @param g The graphics context on which to draw the sprite
    * @param x The x location at which to draw the sprite
    * @param y The y location at which to draw the sprite
    */

   public void draw(Graphics g,int x,int y) {
     
      g.drawImage(image,x,y,image.getWidth(null),image.getHeight(null),null);
         
   }
}

This, the Sprite storage
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

/**
 * A resource manager for sprites in the game. Its often quite important
 * how and where you get your game resources from. In most cases
 * it makes sense to have a central resource loader that goes away, gets
 * your resources and caches them for future use.
 * <p>
 * [singleton]
 * <p>
 * @author Kevin Glass
 */

public class SpriteStore {
   /** The single instance of this class */
   private static SpriteStore single = new SpriteStore();
   
   /**
    * Get the single instance of this class
    *
    * @return The single instance of this class
    */

   public static SpriteStore get() {
      return single;
   }
   
   /** The cached sprite map, from reference to sprite instance */
   private HashMap sprites = new HashMap();
   
   /**
    * Retrieve a sprite from the store
    *
    * @param ref The reference to the image to use for the sprite
    * @return A sprite instance containing an accelerate image of the request reference
    */

   public Sprite getSprite(String ref) {
      // if we've already got the sprite in the cache

      // then just return the existing version

      if (sprites.get(ref) != null) {
         return (Sprite) sprites.get(ref);
      }
     
      // otherwise, go away and grab the sprite from the resource

      // loader

      BufferedImage sourceImage = null;
     
      try {
         // The ClassLoader.getResource() ensures we get the sprite

         // from the appropriate place, this helps with deploying the game

         // with things like webstart. You could equally do a file look

         // up here.

         URL url = this.getClass().getClassLoader().getResource(ref);
         
         if (url == null) {
            fail("Can't find ref: "+ref);
         }
         
         // use ImageIO to read the image in

         sourceImage = ImageIO.read(url);
      } catch (IOException e) {
         fail("Failed to load: "+ref);
      }
     
      // create an accelerated image of the right size to store our sprite in

      GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
      Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
     
      // draw our source image into the accelerated image

      image.getGraphics().drawImage(sourceImage,0,0, sourceImage.getWidth(),sourceImage.getHeight(),null);
     
      // create a sprite, add it the cache then return it

      Sprite sprite = new Sprite(image);
      sprites.put(ref,sprite);
     
      return sprite;
   }
   
   /**
    * Utility method to handle resource loading failure
    *
    * @param message The message to display on failure
    */

   private void fail(String message) {
      // we'n't available
      // we dump the message and exit the game

      System.err.println(message);
      System.exit(0);
   }
}
Offline Ask_Hjorth_Larsen

Junior Duke




Java games rock!


« Reply #8 - Posted 2006-01-10 13:05:16 »

Oh, I didn't mean to say there's nothing to do about this. On the contrary, it worked on my computer though I didn't record the change in circumstances that <i>made</i> it work. So there exists a solution Smiley
Offline Ask_Hjorth_Larsen

Junior Duke




Java games rock!


« Reply #9 - Posted 2006-01-10 13:09:29 »

oh, and the above errorMSG likely stems from the Graphics object not being cast to Graphics2D.

1  
2  
3  
4  
5  
6  
import java.awt.Graphics2D

....
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(...)
....
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trembovetski

Senior Duke




If only I knew what I'm talking about!


« Reply #10 - Posted 2006-01-11 00:27:49 »

I believe that the messed-up colors thing is a bug which should have been fixed since 5.0.

There's pleny of samples on the net on how to do this..

For example, run Java2Demo from the jdk demo/jfc directory and go to Transform tab. Check out
the duke image being flipped when "Scale" button is selected.
Or go to Images tab. The source for the demo is in the same dir.

Thanks,
  Dmitri
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (45 views)
2014-10-17 03:59:02

Norakomi (36 views)
2014-10-16 15:22:06

Norakomi (27 views)
2014-10-16 15:20:20

lcass (31 views)
2014-10-15 16:18:58

TehJavaDev (61 views)
2014-10-14 00:39:48

TehJavaDev (61 views)
2014-10-14 00:35:47

TehJavaDev (51 views)
2014-10-14 00:32:37

BurntPizza (67 views)
2014-10-11 23:24:42

BurntPizza (39 views)
2014-10-11 23:10:45

BurntPizza (81 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!