Results 1 to 7 of 7
  1. #1
    Peetahzee is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default Subclass name to be a parameter?

    For my AP Computer Science project, I needed to finish a chess game by the end of the school year (till June). We are supposed to have the board ready in OO and stuff in 2 days.

    I was almost done, except for putting images as pieces, which created a big mess for me. So right now, I have the following set up:

    Chessboard extends Applet (The main function is here)
    Chesspiece with subclasses:
    - Rook
    - Knight
    - Bishop
    - Queen
    - King
    - Pawn
    Chessgrid, grids on the chessboard. When Chesspiece is constructed, it will be given a Chessgrid to specify its location.

    Okay. I need to draw an image with g.drawImage(), however, since I can only access ImageObserver from Chessboard, I can't draw the Image in constructors of those Chesspieces. And, I want to write as less code as possible, so I decided to create such method in Chessboard:

    Java Code:
            public static Chesspiece putPiece(Chesspiece cp, int color, Chessgrid cg, Graphics g, ImageObserver o){
                    cp = new Pawn(color, cg, g);
                    int tempx = 25-cp.img.getWidth(o)/2;
                    int tempy = 25-cp.img.getHeight(o)/2;
                    g.drawImage(cp.img, cp.cg.positionx+tempx, cp.cg.positiony+tempy, o);
                    return cp;
            }
    Notice though, this is only for the class Pawn. Can I make it so that it takes in a subclass name as a parameter, and create an object of that subclass inputted? In other words, can I make it so that something like this is possible: putPiece(Rook) to instantiate a Rook and draw itself out; putPiece(Queen) to instantiate a Queen and draw itself out, etc. without creating several different methods?

    Thanks.

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

    Default

    Sure you can do this if your Chess pieces all inherit from a common ancestor that has the requisite methods or a common interface. I'm curious why your method returns a chesspiece? I think we may need to know more about your program to understand what you are doing.

    Perhaps your chess pieces can all be held in a a 2-D array (the board) or in an arraylist, and you could just iterate through the collection in your paint method telling each piece to draw itself.
    Last edited by Fubarable; 12-12-2009 at 05:59 AM.

  3. #3
    Peetahzee is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    Hmm, this is how I have the pieces set up.
    queens[], kings[], rooks[][], knights[][],bishops[][], pawns[][], and the method returns a Chesspiece because I want to assign that object as one of the values of these arrays.

    Java Code:
    for(int i = 0; i<2; i++){
    	if (i == 0){ tempy = 7; } else { tempy = 2; }
    	for (int a = 1; a<9; a++){ 
    		pawns[i][a] = putPiece(pawns[i][a], i, grids[a][tempy],g,this);
    	}
    }

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

    Default

    Java Code:
            public static Chesspiece putPiece(Chesspiece cp, int color, Chessgrid cg, Graphics g, ImageObserver o){
                    cp = new Pawn(color, cg, g);
                    int tempx = 25-cp.img.getWidth(o)/2;
                    int tempy = 25-cp.img.getHeight(o)/2;
                    g.drawImage(cp.img, cp.cg.positionx+tempx, cp.cg.positiony+tempy, o);
                    return cp;
            }

    On further review, this doesn't look good as it appears that you are trying to create a chess piece and draw it at the same time. You don't want to draw the chess pieces on creation but rather create their icons or bufferedimages on creation. You then use these images to draw passively in the paint method.

    In other words when you create your chess pieces, you upload the files that hold your images and with these files, create say a buffered image for each type of chess piece. then in your GUI's paint method, you iterate through all the pieces on the board and ask them to draw themselves using the stored buffered image. You will use the Graphics object provided to the paint method at the time of drawing.

    If you haven't read up on graphics and AWT and Swing, now would be the time to do it. Also, if you are not limited by your teacher, you'd be far better off programming this in Swing.

    Much luck!

  5. #5
    Peetahzee is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    On further review, this doesn't look good as it appears that you are trying to create a chess piece and draw it at the same time. You don't want to draw the chess pieces on creation but rather create their icons or bufferedimages on creation. You then use these images to draw passively in the paint method.
    Would you mind elaborating on the reason of this? When i planned to have the creation of the object and drawing to happen together was so that I don't need two lines of codes to "put the piece on the board", since I wouldn't need to face a situation where I only need to create the object but not draw itself, or vice versa.

    Thanks a lot for your help!

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

    Default

    I'm no chess programming expert, but am intrigued by it. I see that there are in fact at least two components to placing pieces on the board and both are quite distinct:

    1) Logically placing a pieces on the board: This means that in your chess model (or I believe they're called "engines" in chess), pieces are stored in a position on the board in memory. This has nothing to do with drawing the pieces and everything to do with the programming knowing where the pieces are, deciding if it's a legal position, and (if this AI) trying to figure out the next potential moves based the current set up of the board. For many high-powered chess programs, this is all they do, and they forgo any GUI representation.

    and
    2) Graphically drawing a piece on the board. This has nothing to do with the logic of the game and everything to do with your GUI program. Here the drawing part of the program queries the model about the location of each piece and then using this information draws the piece on the board in the correct position. It will have facilities for interacting with users allowing them to move pieces and such, and after a move, will then transmit the move to the model above. The model will then tell the GUI if this is a legal move or not.

    My guess is that your program would have an enum for the 6 chess pieces (KING, QUEEN, BISHOP, KNIGHT, ROOK, and PAWN) and then a class for the individual piece that holds an enum value, a boolean (for black or white), and a BufferedImage or ImageIcon for the individual piece's graphic representation, that you'd have a class for your chess square and that this object can hold 0 or 1 chess pieces, and a grid of these chess squares to represent your model.

    I'm not sure how this applies to your program without seeing more of it.
    Last edited by Fubarable; 12-12-2009 at 03:42 PM.

  7. #7
    Peetahzee is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    It will have facilities for interacting with users allowing them to move pieces and such, and after a move, will then transmit the move to the model above
    OH. RIGHT. I got my mind stuck on the "initialize chessboard" stage, and never thought about how pieces will move when the game starts. Thank you so much, that line there was almost the problem to everything.

    Your guess about my program is almost exactly correct, actually, lol. I would have showed you the code, but I felt like I was such a bother to ask somebody to read coupole dozen lines of codes for me for no reason. Well, the problem seems to be solved now, thanks A LOT!

Similar Threads

  1. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-20-2009, 11:46 PM
  2. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  3. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:03 PM
  4. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:31 PM
  5. SubClass problems
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 05:54 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
  •