Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Extending problems...

  1. #1
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Extending problems...

    Well, I'm making a super basic game to race simple ovals around the frame, my problem is i have 2 classes, but I have to extend 2 things...here is my code.
    Also, ill be willing to combine classes if I could.
    The position of the oval needs to be the int PlayerX and PlayerY which will be your position once i get done.

    Java Code:
    package ballracer;
    
    import javax.swing.JFrame;
    
    public class BallRacer{
    
        public int PlayerX;
        public int PlayerY;
        
        public BallRacer(){}
        
        public static void main(String[] args) {
        //the frame
        JFrame GameFrame = new JFrame("BallRacer v1");
        GameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GameFrame.setSize(800,600);
        GameFrame.setVisible(true);
        GameFrame.setResizable(false);
        //The Graphics
        Graphics2D Gobject = new Graphics2D();
        GameFrame.add(Gobject);
        Gobject.paint();
        
        }
        
    }
    Java Code:
    package ballracer;
    
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class Graphics2D extends JPanel{
        
        public void paint(){
            repaint();
        }
        
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.fillOval(0,0,20,20);
        }
    }

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Extending problems...

    Does that code compile?

    I ask because of the line

    Java Code:
    GameFrame.add(Gobject);
    GameFrame is an instance of JFrame, and Gobject is an instance of Graphics2D. As far as I know frames do not have a method that allows you to add graphics contexts. So, unless I am missing something, that code won't compile. It is not clear what that line is supposed to do.

    -----

    You should use standard Java coding conventions and begin variables with a lowercase letter.

    Also it would be a good idea to make and display the application's frame on the event dispatch thread. See the discussion of Initil Threads in Oracle's Tutorial. An example of the code they mention there can be found in FrameDemo.java which can serve as a good "recipe" for frame creation and display.

    -----

    [Edit] Perhaps, also, the Tutorial may be helpful in its discussion of how painting occurs in Swing: where the graphics context comes from and how it is used in painting code.
    Last edited by pbrockway2; 02-26-2012 at 01:29 AM.

  3. #3
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: Extending problems...

    yes it complies and works with the oval on frame if i make the position a variable - im looking on how to make playerx and playery extend to the Graphics2D class

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

    Default Re: Extending problems...

    I think your code is confusing in that you're naming a class you've created "Graphics2D" and there already exists a Graphics2D class in the Java core libraries, and this class is used often when doing Swing graphics programming. So first off, to avoid confusing us, yourself and others, consider renaming this class to something else.

    Next, this is a very bad idea:
    Java Code:
        public void paint(){
            repaint();
        }
    If the JVM weren't smart, this could lead to a nasty recursive nightmare. If you want to create a "game-loop" for a Swing GUI, use a Swing Timer instead. You can find out more about this here: How to use Swing Timers

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Extending problems...

    Ah, I see! Graphics2D is a sort of panel!

    I would highly recommend that you don't use names from the standard Java library for your own classes. This will certainly confuse the likes of me. And it will very likely confuse your compiler at some point.

    -----

    The player positions are ints. They don't extend anything. That track panel might possibly contain the position variables as instance variables (instead of having them as unused variables of the other class)

    More generally though you might find it useful to have a class representing a RaceBall. An instance of such a class would have a position and velocity and be able to update the position based on the velocity.

    -----

    I'm thinking along the following lines. (There are *certainly* other ways of doing things)

    BallRacer
    main() method to put a TrackPanel in a frame and display it


    TrackPanel
    contains RaceBall instance
    contains RaceTrack instance
    updatePosition() method to move the ball to a new position on the track. Something interesting should happen if the new position is invalid
    (later you may want to animate this by having the updatePosition() method called every so often)

    RaceTrack
    isValid(x,y) - a method which reports if an x/y position is actully on the track


    RaceBall
    getX()/getY() - reports the ball's position
    updatePosition() - updates the ball's position based on its velocity
    (later you might want to have methods that alter the size and direction of the ball's velocity)

    I'm only really saying all this (because you haven't really asked a question...) to illustrate my thinking that your game needs more, simpler, smaller classes. And not extending classes.

    -----

    And, to repeat, please follow Java coding conventions: your own descriptive variables and classes. Variables beginning with a lowercase letter, and classes with a uppercase one.
    Fubarable likes this.

  6. #6
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 12-17-2010, 10:24 AM
  2. Extending Classes and What is Necessary
    By GavinCash in forum New To Java
    Replies: 10
    Last Post: 10-11-2010, 08:07 AM
  3. Extending java.net.Socket
    By DC200 in forum Networking
    Replies: 3
    Last Post: 06-10-2010, 05:23 PM
  4. Got confused with extending classes.
    By nethz13 in forum New To Java
    Replies: 7
    Last Post: 04-19-2010, 01:19 AM
  5. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 10:49 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
  •