Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Image Placement with Precision - Graphics2D  (Read 2631 times)
0 Members and 1 Guest are viewing this topic.
Offline bcolter

Junior Newbie





« Posted 2009-01-10 18:42:38 »

Hi,
This is my first post!

I would like to load an image and control the placement/movement in a JPanel with precision (float). After a gazzilion searches and paging through books it seems that I can only position an image using (int).

Is there a trick I can use... Like somehow attaching the image to a rect, or another image method I can use?

Cheers & Thanks
Offline SimonH
« Reply #1 - Posted 2009-01-10 18:49:01 »

The smallest unit for placement is a pixel which is a discrete value (int) but there's nothing to stop you casting a float to an int;

1  
2  
3  
4  
for (float i=0;i<100;i+=0.5f)
{
    g.drawImage(image,(int)i,(int)i,null);
}

People make games and games make people
Offline bcolter

Junior Newbie





« Reply #2 - Posted 2009-01-10 18:51:06 »

Indeed... but then I loose the precision. Sorry if this is silly question. ( Hence Newless Clubies)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 38



« Reply #3 - Posted 2009-01-10 19:41:37 »

To help us (and probably yourself) to better understand the problem try to answer the following questions:

- What do you want to achieve in your application?
- Why would you need to place that image with float coordinates?
- What do you expect to gain?

Mathias - I Know What [you] Did Last Summer!
Offline bcolter

Junior Newbie





« Reply #4 - Posted 2009-01-10 19:57:57 »

I'm writing a simple app to simulate gravity on an object for a Physics project

The problem occurs in the Maths when the ball is at the top of the fall... because I have to cast to (int) for movement, it stalls until the value is greater than 0.5 or less than -0.5... So it just hangs there.

Here is the code for the movement of the ball...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// Variables
private final long DELAY = 20;                                  // Timer delay (10 ms)
   
    private float a = 9.8f;                                         // Default acceleration 9.80 m/s/s
   private float t = DELAY / 1000.0f;


public void cycle(){
        // Increase velocity based on rate of acceration
       myBall.setY((int)(myBall.getY() + (t * (myBall.getVY() + a * t))));
        myBall.setVY(myBall.getVY() + a * t);
        //System.out.println(myBall.getY());
       //System.out.println(myBall.getVY());
       
        if(myBall.getY() > getHeight() - myBall.getRadius() * 2){
            myBall.setVY(-myBall.getVY()*myBall.getElast());
        }


/Edit - Added the variables... note getVY returns a float (myball current velocity)
Offline jezek2
« Reply #5 - Posted 2009-01-10 21:14:07 »

You could try TexturePaint and filling a rectangle with it.
Offline SimonH
« Reply #6 - Posted 2009-01-10 21:18:45 »

Indeed... but then I loose the precision.

Make sure to keep all variables as floats throughout your calculations and only cast to int in the actual drawImage() call.
That should sort it!

People make games and games make people
Offline bcolter

Junior Newbie





« Reply #7 - Posted 2009-01-10 21:38:25 »

Many many thanks to everyone who replied!

Cheers
Offline brackeen

Junior Member





« Reply #8 - Posted 2009-01-10 22:12:51 »

You want to draw an image at sub-pixel locations? I think Project SceneGraph has that built-in. I'm not sure about JavaFX, I saw one demo (the puzzle demo) it looked like some pixel-snapping was going on.
For straight Java2D, this method that might work by using transforms:
Graphics2D#drawImage(Image, AffineTransform, ImageObserver)
You may have to set up some RenderingHints to get it to work correctly (bilinear filtering, etc)
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #9 - Posted 2009-01-11 03:18:06 »

Have a look at this:
http://www.otcsw.com/planetarysimulator.php

All the masses, positions, and velocities of the planets are doubles. Then when I draw it I just say g.fillOval(int)x,(int)y,(int)radius*2,(int)radius*2).

There is no reason to draw at a sub-pixel level. Your monitor can only show things on a pixel-by-pixel basis; nothing can be drawn in between two pixels. SimonH is right - as long as you keep everything stored as a float or double and then merely draw as int, it will look fine.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 38



« Reply #10 - Posted 2009-01-11 12:47:23 »

I think his problem is the visual imperfection of very small position changes, so the ball "hops" to the next pixel from time to time. Either way is to accept this limitation or use subpixel placement, that recalculates the real pixels by multisampling.

Mathias - I Know What [you] Did Last Summer!
Offline brackeen

Junior Member





« Reply #11 - Posted 2009-01-11 18:06:30 »

There is no reason to draw at a sub-pixel level. Your monitor can only show things on a pixel-by-pixel basis; nothing can be drawn in between two pixels.
Well.... not exactly. Drawing at sub-pixel level makes anti-aliased images appear to have smoother animation when moving slowly. It can be accomplished with bilinear interpolation. Of course, it makes pixel-art look like crap.

Flash has a pixelSnapping on/off property for Bitmap objects. Apple's Core Animation always enables sub-pixel placement. JavaFX, I couldn't find any info about it, but I think Nodes always snap to the nearest pixel.
Offline bcolter

Junior Newbie





« Reply #12 - Posted 2009-01-11 18:23:43 »

I used the cast method as suggested by SimonH (gave me the answer in the first post, but I didn't quite grasp it)...
1  
g2.drawImage(myBall.getBall(),(int)myBall.getX(),(int)myBall.getY(),null);


... and everything works perfectly. Thank you very much, a wonderful first experience on this forum.

/ happily goes back to coding
Offline brackeen

Junior Member





« Reply #13 - Posted 2009-01-11 21:55:39 »

I used the cast method as suggested by SimonH (gave me the answer in the first post, but I didn't quite grasp it)...
1  
g2.drawImage(myBall.getBall(),(int)myBall.getX(),(int)myBall.getY(),null);


... and everything works perfectly. Thank you very much, a wonderful first experience on this forum.

/ happily goes back to coding
Welcome to java-gaming.org, where someone like myself typically pops into a thread and derails the conversation into something completely different. Smiley
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #14 - Posted 2009-01-11 23:22:16 »

> I'm not sure about JavaFX

JavaFX uses floats everywhere, and there's no "pixel snapping" for shapes.
Try placing a Rectangle on 1.1,1.1 you will see that becomes AA-ed to achieve that.

For Images, there's a hint in the scenegraph to allow subpixel positioning (also depends on filtering mode set in ImageView: ImageView.smooth),
but I don't think it's exposed on the FX level right now.

By default non-integer translations for images is off since it's slow on some earlier (1.5) jdks.

Dmitri
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #15 - Posted 2009-01-11 23:25:50 »

To answer the original question: use affine transform to place the image with high precision. Also make sure to set bilinear rendering hint (the default is nearest neighbor). But be aware that with earlier jdks (pre-1.6) performance will suck.

Dmitri
Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #16 - Posted 2009-01-13 03:57:19 »

Could use g.translate(float x, float y) !!!

Example:

g.translate(myImageX, myImageY);
g.drawImage(myImage, 0, 0);
g.translate(-myImageX, -myImageY);


Thank you thank you!  Grin

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #17 - Posted 2009-01-13 05:38:07 »

Either that, or as suggested by brakeen (http://www.java-gaming.org/topics/image-placement-with-precision-graphics2d/19689/msg/156523/view.html#msg156523) use drawImage with affine transform.

Don't forget to set the rendering hint to bilinear:
1  
2  
3  
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(image, AffineTransform.getTranslateInstance(doubleX, doubleY), null);


Dmitri
Offline brackeen

Junior Member





« Reply #18 - Posted 2009-01-13 16:05:25 »

> I'm not sure about JavaFX

JavaFX uses floats everywhere, and there's no "pixel snapping" for shapes.
Try placing a Rectangle on 1.1,1.1 you will see that becomes AA-ed to achieve that.

For Images, there's a hint in the scenegraph to allow subpixel positioning (also depends on filtering mode set in ImageView: ImageView.smooth),
but I don't think it's exposed on the FX level right now.

By default non-integer translations for images is off since it's slow on some earlier (1.5) jdks.

Dmitri

Okay, that explains why I was seeing pixel-snapping on my Mac+Java 1.5 on the puzzle demo. Yup, that "smooth" variable is exposed in ImageView - good to know, 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.

pw (12 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!