Results 1 to 6 of 6
  1. #1
    MagePwns is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Help with first program

    I am going to university for games programming, and thought i should give it a go myself and have a better knowledge for when i get there, and so decided to make a brick breaker game, now i have the start interface which loads the background image and 3 images which are going to be buttons. I've been trying for quite some time to figure out how to make it so that when i click on one of the buttons, it removes the startInterface and changes to the instructionsInterface. Here is the code for the graphics and the mouse events. Thanks for taking your time to read and help in advanced :)

    Java Code:
        public void paint(Graphics g) {
            dbImage = createImage(getWidth(), getHeight());//create image to the width and height of the image
            dbg = dbImage.getGraphics(); //set dbg to the image and get the graphics of the image
            draw(dbg);//draw the image
            g.drawImage(dbImage, 0, 0, this);//set up the drawImage method
        }
        public void draw(Graphics g){
            //constant
            g.drawImage(background, 0, 0, this);//draw the background to 0, 0
            //Starting Interface
            if(startInterface == true) {//if start interface is = to true do the following
            g.drawImage(startButton, 100, 110, this);//draw the start button to the screen
            g.drawImage(instructionButton, 100, 260, this);//draw the instructions button to the screen
            g.drawImage(highScoresButton, 100, 410, this);//draw the highscoresbutton to the screen
            repaint();
            }
            if (instructionsInterface == true) {//if the instructions interface is -= to true, do the following
            g.drawImage(instructions, 50, 20, this);//draw the instructions interface
            g.drawImage(startButton, 100, 512, this);//draw the back to main menu button to the screen
            }
        }
             
            public class MyMouseListener implements MouseListener, MouseMotionListener{
                @Override
                    public void mouseReleased(MouseEvent e) {
                    int xpos = getX();
                    int ypos = getY();
                        if(SwingUtilities.isLeftMouseButton(e)){
                            
        }
    }
            public void mouseClicked(MouseEvent e) {}
            @Override
            public void mousePressed(MouseEvent e) {
            int xpos = getX();
            int ypos = getY();
                        if(SwingUtilities.isLeftMouseButton(e)){
                            if(startInterface = true && xpos >= 100 && xpos <=300 && ypos >= 260 && ypos <=340) {
                                startInterface = false;
                                instructionsInterface = true;  
                                repaint();
                            }
            }
            }
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
            public void mouseDragged(MouseEvent e) {}
            public void mouseMoved(MouseEvent e) {}
            }
    I am a complete beginner however my thoughts on the mouse were get the mouse position and if it is in the area where the image is, it will stop the start interface from being true and instead make the instructions interface = to true.
    thanks again :)
    Last edited by MagePwns; 07-03-2013 at 12:28 AM.

  2. #2
    kratos9797 is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Help with first program

    Have you tried using an else or else if statement at line 17 instead of an if statement?

  3. #3
    MagePwns is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: Help with first program

    Just added the else in and this didn't help with making the interface switch :S Thanks for you help though (Y) anyone have any other idea's?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Help with first program

    For someone who is a beginner you are trying to do do much and not focusing on how Java works. I recommend you start off slowly and follow the tutorials at the link in my signature. There is so much essential stuff to learn before you develop any game or GUI.

    Here are some questions about your code.

    1. I assume you are overriding paint(Graphics g). Why? You only need to do that if you are using AWT? You should be using Swing and overriding paintComponent(Graphics g) within a JPanel. And the JPanel should be in a JFrame.
    2. Why are you drawing your own buttons? Are you really trying to do all that work? Why wouldn't you use a regular Swing button? There are many types to choose from. And they have there own event handlers.
    3. You are implementing the MouseListeners in you inner class. That will work but why not just extend MouseAdapter? That is what it is for. Then you don't need to include the unimplemented dummy methods.
    4. I don't really want to see all of your code but you should provide a Short, Self Contained, Correct Example to demonstrate the problem you are having. That is mentioned in the forum rules.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Help with first program

    Also, the whole point of double buffering is lost by calling the method that draws the offscreen image from a painting method override. An offscreen image should be updated when mandated by state changes, after which repaint() should be called.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Help with first program

    Its good that you want to get a head start, that proves that you're going to graduate easily. But unfortunately you're not going down a very productive path here. You want to learn about game programming and what are you now struggling with? Swing custom painting, not game programming. I'm pretty sure that when you go to school there will be no course dealing with Java game programming, it will be either C++ or C# (likely C# using the Unity engine). So your current efforts aren't getting you what you seek.

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  2. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  3. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  4. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •