Results 1 to 9 of 9
  1. #1
    swalker326's Avatar
    swalker326 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default Java Coordinate System

    Java Coordinate System

    I'm trying to build a map overlay for a text based RPG I play, I'm in the process of build a coordinate system for the map, I have coordinate class I have a few methods in the class

    Java Code:
    public class Coord {
    
        String CoordName;
        String CurrentCoords;
    
        public Coord(String newName,int areaNumber,int roomNumber){
            int a = areaNumber;
            int r = roomNumber;
            this.CurrentCoords = "Current Coords: " +a +r;
            this.CoordName = newName;
        }
        public Coord(String newName){
            int a = 0;
            int r = 0;
            this.CurrentCoords= "Current Coords: " +a +r;
            this.CoordName = newName;
        }
        public Coord(){
            this.CoordName = "NewCoord";
            int a = 0;
            int r = 0;
            this.CurrentCoords = "Current Coors:" +a +r;
        }
        public void Coord(Coord newCoord){
    
        }
        public String getCoords(){
            return CurrentCoords;
        }
    
    
    }
    Where I am stuck is making the coordinate increment correctly when I add a room, my goal is, the game I play has “areas” then inside the areas “rooms” I want the first number to be an area number and the second number to be the room number inside that area. I'm really just looking for advise in which way I should even consider building a system like this.

    I'm pretty new to java I have played around with it in the past but I'm just now trying to actually understand in depth how to develop java. I'm sure I will get blowen up for something I did, but thanks in advance for any help.

    Thanks,
    Shane

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    I'm sure I will get blowen up for something I did
    Sure thing.

    1. Your class as written holds only two fields, both Strings. If the class is to encapsulate a coordinate, it needs to maintain a state which represents the two coordinates, doesn't it? and provide methods to access (and possibly to assign, unless the class is meant to be immutable) this state.
    2. Your getCoords() essentially returns a String representation of the instance of Coord, should override toString() -- that's what the method is for. Read the API for Object#toString().
    It is recommended that all subclasses override this method.
    3. You have a copy constructor that does nothing.

    Take a shot at those improvements before you go on to anything else. But your question as asked
    Where I am stuck is making the coordinate increment correctly
    In the light of the sample code you posted, any incrementing would be in the sction of code that creates new Coord objects. Not in the Coord class itself.

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quick example:
    Java Code:
    public class Coord {
    
        private String name;
        private int area;
        private int room;
    
        public Coord(String newName, int areaNumber, int roomNumber) {
            name = newName;
            area = areaNumber;
            room = roomNumber;
        }
    
        public Coord(String newName) {
            name = newName;
            area = 0;
            room = 0;
        }
    
        public Coord() {
            name = "NewCoord";
            area = 0;
            room = 0;
        }
    
        public void Coord(Coord newCoord) {
            name = newCoord.name;
            area = newCoord.area;
            room = newCoord.room;
        }
    
        public int getArea() {
            return area;
        }
    
        public int getRoom() {
           return room;
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public String toString() {
            return CurrentCoords;
        }
    }
    Also read
    Code Conventions for the Java(TM) Programming Language: Contents

    db

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Another, more concise, way of coding the constructors:
    Java Code:
    public class Coord {
    
        private String name;
        private int area;
        private int room;
    
        public Coord(String newName, int areaNumber, int roomNumber) {
            name = newName;
            area = areaNumber;
            room = roomNumber;
        }
    
        public Coord(String newName) {
            this(newName, 0, 0);
        }
    
        public Coord() {
            this("NewCoord");
        }
    
        public void Coord(Coord newCoord) {
            this(newCoord.name, newCoord.area, newCoord.room);
        }
    :
    :

  5. #5
    swalker326's Avatar
    swalker326 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks alot for the pointers man, really put me on the right track for my class construction in all my classes. One question, when i try to compile

    public void Coord(Coord newCoord) {
    this(newCoord.name, newCoord.area, newCoord.room);
    }
    I get an error, (call to this must be first statement in constructor) when I try to compile it, by removing the void keyword it will compile.

    -Shane-
    Last edited by swalker326; 12-03-2010 at 04:33 AM.

  6. #6
    swalker326's Avatar
    swalker326 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    public class Coord {

    String name;
    int area;
    int room;

    public Coord(String newName, int areaNumber, int roomNumber) {
    name = newName;
    area = areaNumber;
    room = roomNumber;
    }

    public Coord(String newName) {
    this(newName, 0, 0);
    }

    public Coord() {
    this("NewCoord");
    }

    @SuppressWarnings("static-access")
    public Coord(Coord newCoord) {
    this(newCoord.name, newCoord.area, newCoord.room);
    }

    public void setArea(int NewArea){
    this.area = NewArea;
    }
    public void setRoom(int NewRoom){
    this.room = NewRoom;
    }
    }

  7. #7
    swalker326's Avatar
    swalker326 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    A new Problem i'm running into now:

    Java Code:
    package map;
    
    import java.util.ArrayList;
    
    /**
     *
     * @author shane
     */
    
    public class Area {
    
    int AreaNumber;
    int AreaCount;
    int TotalRooms;
    
    String AreaName;
    
    ArrayList<String> RoomDescriptions = new ArrayList();
    ArrayList<String> SecretExits = new ArrayList<String>();
    
    public Area(String newName){
        AreaName = newName;
        this.AreaNumber = AreaCount;
        Coord.setArea(AreaCount)
        incAreaCount();
    }
    
    public void incAreaCount(){
        AreaCount++;
        }
    }
    Error is at Coord.setArea(AreaCount)

    Error is: None static method cannot be referenced from static context.

    I don't make the setArea method static I'm confused why it is static.

    Thanks
    -Shane-

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

    Default

    You're calling the setArea method on the Coord class as if it were a static method and not on a Coord object.

  9. #9
    swalker326's Avatar
    swalker326 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Cartesian coordinate system (X > 0, Y>0)
    By cselic in forum Java 2D
    Replies: 0
    Last Post: 07-02-2010, 02:54 PM
  2. java x,y coordinate
    By robc in forum Advanced Java
    Replies: 7
    Last Post: 05-29-2010, 07:11 AM
  3. Replies: 3
    Last Post: 02-07-2010, 09:34 PM
  4. set coordinate to an image
    By nuur in forum AWT / Swing
    Replies: 2
    Last Post: 04-01-2008, 09:08 AM
  5. X&Y Coordinate Drawing on jPanel
    By BHCluster in forum Java 2D
    Replies: 2
    Last Post: 03-27-2008, 11:47 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
  •