Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default My java game problem!? Help!!!

    Hello, i am haveing a problem with my game, i am trying to move a square around the screen named sci.jpg the problem i am haveing, is i cant get it to show up. i am not sure, but i do think its something wrong with my Board object's paint class, because i can not get anything to show up by painting a string from the paint class. but, i can get the keuy pressed/realeased to work because, i can add a system out print to the event and it will show when i press/release the key. i REALLY need help, please dont give me a response like "go search this" or "look this up" if i could find it by searching, i wouldnt be here asking. here are my 3 objects.

    Main:
    Java Code:
    package game;
    
    import javax.swing.*;
    
    public class Main extends JFrame {
    
        public static void main(String[] args) {
            Board b = new Board();
            b.gameSetup();
    
    
    
        }
    }
    Board:
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class Board extends JPanel implements ActionListener {
    
        private Timer timer;
        private Player play;
        public JFrame f1;
        public JPanel p1;
    
        public void gameSetup() {
    
            f1 = new JFrame();
            f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f1.setSize(500, 500);
            f1.setLocationRelativeTo(null);
            f1.setResizable(false);
            f1.setTitle("Jake's Game");
            f1.setVisible(true);
    
            p1 = new JPanel();
            f1.add(p1);
    
    
            p1.addKeyListener(new aAdapter());
    
            p1.setFocusable(true);
            p1.setBackground(Color.white);
            p1.setDoubleBuffered(true);
    
            play = new Player();
    
            timer = new Timer(5, this);
            timer.start();
    
        }
    
         public void paint(Graphics g) {
            super.paint(g);
    
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(play.getImage(), play.getX(), play.getY(), this);
    
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    
        
    
        public void actionPerformed(ActionEvent e) {
    
            play.move();
            repaint();
        }
    
        private class aAdapter extends KeyAdapter {
    
            public void keyReleased(KeyEvent e) {
                play.keyReleased(e);
            }
    
            public void keyPressed(KeyEvent e) {
                play.keyPressed(e);
            }
        }
    }
    Player:
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Player {
    
        private String play = "sci.jpg";
        private int vx;
        private int vy;
        private int x;
        private int y;
        public Image image;
    
        public Player(){
            ImageIcon ii = new ImageIcon(this.getClass().getResource(play));
            image = ii.getImage();
    
            x = 40;
            y = 60;
        }
    
        public void move(){
            x += vx;
            y += vy;
        }
    
        public int getX(){
            return x;
        }
        public int getY(){
            return y;
        }
        public Image getImage(){
            return image;
        }
    
        public void keyPressed(KeyEvent e){
            int key = e.getKeyCode();
    
            if(key == KeyEvent.VK_LEFT){
                vx = -1;
                System.out.println("Hello");
            }
            if(key == KeyEvent.VK_RIGHT){
                vx = 1;
            }
            if(key == KeyEvent.VK_UP){
                vy = -1;
            }
            if(key == KeyEvent.VK_DOWN){
                vy = 1;
            }
        }
        public void keyReleased(KeyEvent e){
            int key = e.getKeyCode();
    
            if(key == KeyEvent.VK_LEFT){
                 System.out.println("Goodbye");
                vx = 0;
            }
            if(key == KeyEvent.VK_RIGHT){
                vx = 0;
            }
            if(key == KeyEvent.VK_UP){
                vy = 0;
            }
            if(key == KeyEvent.VK_DOWN){
                vy = 0;
            }
        }
    
    }
    Ps ~ Sorry for my bad spelling.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    Where do you add the component that displays the image to a container that is being displayed in the GUI?

    Also with Swing it is better to override the paintComponent method instead of the paint method.

  3. #3
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Where do you add the component that displays the image to a container that is being displayed in the GUI?

    Also with Swing it is better to override the paintComponent method instead of the paint method.

    Im not sure what you men by the component that displayes the image for the gui, wich, could very much soo be the problem, please alaberate.

    also, i added the paintComponent overide, and it did nothing diferently, that i can see. what is the diference between, paint, paint with @overide, paintComponent, and paintComponent with @overide?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    the component that displayes the image is the Board class. Its paint method draws/displays the image.

    Where do you add an instance of the Board class to the JFrame that is displaying (ie showing the GUI)?

    The paint method is part of the old AWT classes. The paintComponent() method is what is recommended for use with Swing classes.

    @Override asks the compiler to check that the override you are trying to do is correct.

  5. #5
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    yes, board displays the image, that is made by the player object. at the start of board, i make extend JPanel to the board class. te i create a JFrame, and add the panel to the JFrame. what would i have to do to properly add JPanel to JFrame, assuming you are saying the way i did it is incorect?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    You use the JFrame's add method to add a JPanel to the JFrame. Does your code add an instance of the Board class to the JFrame?

    Your program construction is weird. The building of the JFrame should NOT be done inside of the Board class.

  7. #7
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Well, probably not, because i dont know how to do that. so if i like, in my Main class, extended JPanel and set the jpanel up there, then did something like add(Board()); is that what you mean? how would i add an instance to the board class from the JFrame in the board class?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    an instance to the board class from the JFrame in the board class
    Use the this variable.

  9. #9
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ok, so i took all of the JFrame stuff out of the board object, and just left the extended Jpanel stuff. now, i added extend JFrame to my main class, then i put everything in a new meathod called frameSetup. then, i i called frameSetup from my main meathod. how to i add Board and gameSetup to frameSetup.

    this is my new Main Object:
    Java Code:
    package game;
    
    import javax.swing.*;
    
    public class Main extends JFrame {
    
        public void frameSetup() {
    
            
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 500);
            setLocationRelativeTo(null);
            setResizable(false);
            setTitle("Jake's Game");
            setVisible(true);
    
    
    
        }
    
        public static void main(String[] args) {
            Main m = new Main();
            m.frameSetup();
    
    
    
        }
    }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    how to i add Board and gameSetup to frameSetup.
    Look at how you had it in the original main() method.

  11. #11
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ok, so i added Board b = new Board(); then called gameSetup like this, b.gameSetup(); and now it doesnt work. now it doesnt even add JPanel, or listen to the keyListener events?


    New Main:
    Java Code:
    public class Main extends JFrame {
    
        public void frameSetup() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 500);
            setLocationRelativeTo(null);
            setResizable(false);
            setTitle("Jake's Game");
            setVisible(true);
    
            Board b = new Board();
            b.gameSetup();
    
    
        }
    
        public static void main(String[] args) {
            Main m = new Main();
            m.frameSetup();
    
    
    
        }
    }
    New Board:
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class Board extends JPanel implements ActionListener {
    
        private Timer timer;
        private Player play;
        
        public void gameSetup() {
    
           
           addKeyListener(new aAdapter());
    
            setFocusable(true);
            setBackground(Color.BLACK);
            setDoubleBuffered(true);
    
            play = new Player();
    
            timer = new Timer(5, this);
            timer.start();
    
        }
    
        
        @Override
         public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(play.getImage(), play.getX(), play.getY(), this);
    
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    
        
    
        public void actionPerformed(ActionEvent e) {
    
            play.move();
            repaint();
        }
    
        private class aAdapter extends KeyAdapter {
    
            public void keyReleased(KeyEvent e) {
                play.keyReleased(e);
            }
    
            public void keyPressed(KeyEvent e) {
                play.keyPressed(e);
            }
        }
    }

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    now it doesnt even add JPanel,
    You need to code that. OR do you have an IDE that adds statements to your code for you?
    There is no magic here. You have to write all the code and in the correct order, to get your program to do what you want it to do.
    If you want a component added to a container, you must write the statements to do that.

  13. #13
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    And, how do i do it? i would like to add it all so i can get the damn box moveing around haha.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    Use the JFrame's add method to add a component.

  15. #15
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    So, like, f.add(Board); you have me confused slightly.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    Is Board the variable for the Board object you create?
    Here is example code from your program that shows you know how to use the add method:
    p1 = new JPanel();
    f1.add(p1);

  17. #17
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    i know how to add things, but i dont know how to add things like this. where, the JFrame is in a deferent meathod, and diferent class and object. am i adding the object board itself to the JFrame, or am i adding the meathod gameSetup to JFrame. show me please. i think im over complicateing it.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    am i adding the object board itself to the JFrame, or am i adding the meathod gameSetup to JFrame.
    What do you want to add to the JFrame? Which component?
    You add instances of objects that extend the Component class.
    You do NOT add methods.

  19. #19
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    So, i would defenately want to add board not gameSetup. wich would look like add(Board); right? it doesnt work, because i am missing something.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    You add instances of objects.
    What is "Board"? Is that an instance of the Board class? Replace Board with the variable that points to a Board class instance. Look at this statement:
    Board b = new Board();
    What is the variable that points to a Board class object?

    What kind of text are you using to learn java? These are very beginning concepts that are covered in the Tutorials about how to build Swing GUI. Have you read them? If not, maybe you should.

Page 1 of 2 12 LastLast

Similar Threads

  1. Huge problem. PacMan game.
    By Bullfrog in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 02:39 PM
  2. Need smart solution on java game lag problem..
    By Addez in forum New To Java
    Replies: 0
    Last Post: 11-13-2009, 12:48 PM
  3. SUDOKU game problem
    By bumblyb33 in forum AWT / Swing
    Replies: 2
    Last Post: 05-05-2009, 11:18 PM
  4. Problem with an assignment: Backgammon game
    By Poddy in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 05:32 AM
  5. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 04:04 AM

Tags for this Thread

Posting Permissions

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