Results 1 to 7 of 7
  1. #1
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default JPanel Co-ordinates

    Hi there!,

    Second post in my first day, but i've hit another major problem with my program.

    the co-ordinates for the JPanel start with 0,0 in the top left.

    I need them to be bottom left.

    Is there an easy way to do this?

    One solution i've thought of is rotating the JPanel counterclockwise 90degrees, and then just using the X as the Y and Y as the X.

    The problem with that is

    I don't know how to rotate the panel.
    Flipping x and y over could end up causing me lots of silly issues later on when i have more code to deal with.


    Any ideas folks?

    As always, thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    How are you using these coordinates? What is this for? To draw on the JPanel? To place other components? You know it is easy to calculate the JPanel's dimensions after it has been rendered via the getSize method.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Are you tying to make a graph plotter? If so, just accept that y++ is going down and y-- is going up, x behaves normally.

  4. #4
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    i am using a JPanel as a 'scene'

    I am using Java to create a level editor for an iphone game im working on.

    Images get placed into the scene, when a new item is added. It gets added to the top of a list.

    Each item in the list knows its position, and knows the 'gap' between it and the item above it.

    I have some script which uses this data and generates some Objective-C code for me.

    Essentially, rather than having to place sprites, load the app, load the game, check the level, close the app, alter a int value to move an item, repeat, over and over and over. I'm making a visual editor.

    On an iPhone, the map runs from bottom to top. 0,0 being bottom left.

    If i use the JPanel co-ordinates, my map would 'start' from the top and go down, and so essentially be upside down.
    Does that information help?

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    This is pretty similar to a problem I had, mainly being if you store coordinates in an array, you can't print them out directly to the screen, since in a 2d array, the 'x' or first coordinate denotes height, while screen coordinates have their 'x' denote length. I solved that simply by mirroring the coordinates of the array, so on-screen coordinates 300,400 would become array[400][300]. In your case, if you simply want the y coordinate reversed, when storing the coordinates of your objects you could do something like:
    Java Code:
    yCoord = panel.getHeight() - y; //y denotes jPanel coordinate
    Not sure about the getHeight() method, should see JPanels API to confirm. So, in essence, if your object is almost at the top-left corner with coordinates lets say 30,20 with a panel height of 200, you'd get 30,180, as if the 0,0 point was in the bottom-left.

  6. #6
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    genius. <3

    ^ thats raw manlove up there. haha thx guys

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Java Code:
    Graphics.translate(...);

Similar Threads

  1. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 10:24 AM
  2. I need some help with JPanel
    By bantes in forum AWT / Swing
    Replies: 7
    Last Post: 11-03-2009, 07:58 PM
  3. drawing in JPanel ,beyond the co-ordinates
    By anandnarayan891 in forum AWT / Swing
    Replies: 4
    Last Post: 04-05-2009, 03:42 AM
  4. How to use Jpanel
    By Manfizy in forum NetBeans
    Replies: 0
    Last Post: 02-19-2009, 12:34 PM
  5. Adding JPanels to JFrames based on x-y co-ordinates
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 07-24-2008, 10:41 PM

Posting Permissions

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