1

The output I expect to have is a pipe scrolling across the screen, as shown below. The output that I'm currently getting seems to be only partly shown. Im really not sure if this problem is being caused by my code or not. If the problem is not in my code, then does anyone have any suggestions?

The expected output

Correct display

The output I am currently getting

enter image description here

Game Class

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Game {

    Pipes panel = new Pipes();

    public Game() {
        JFrame f = new JFrame();

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(panel);
        f.setTitle("Pipe Game");
        f.setResizable(false);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);


        Timer timer = new Timer(10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.move();
                panel.repaint();
            }
        });
        timer.start();
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Game();
            }
        });
    }
}

Pipes.class

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Pipes extends JPanel {
    //Declare and initialiaze variables
    int x1 = 754;               //xVal start
    int x2 = 75;                //pipe width
    int y1 = -1;                //yVal start
    int y2 = setHeightVal();    //pipe height

    int gap = 130;              //gap height

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.clearRect(0,0,750,500);                       //Clear screen
        g.drawRect(x1,y1,x2,y2);                        //Draw part 1
        g.drawRect(x1-3,y2-1,x2+6,25);                  //Draw part 2
        g.drawRect(x1-3,y2+25+gap,x2+6,25);             //Draw part 3
        g.drawRect(x1,y2+25+gap+25,x2,500-y2-49-gap);   //Draw part 4
    }

    public void move() {
        x1--;
    }

    public int getMyX() {
        return x1-3;
    }

    public int getMyY() {
        return y2+25;
    }

    public int setHeightVal() {
        int num = (int)(9*Math.random() + 1);
        int val = 0;
        if (num == 9)
        {
            val = 295;
        }
        else if (num == 8)
        {
            val = 246;
        }
        else if (num == 7)
        {
            val = 216;
        }
        else if (num == 6)
        {
            val = 185;
        }
        else if (num == 5)
        {
            val = 156;
        }
        else if (num == 4)
        {
            val = 125;
        }
        else if (num == 3)
        {
            val = 96;
        }
        else if (num == 2)
        {
            val = 66;
        }
        else
        {
            val = 25;
        }
        return val;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(751, 501);
    }
}
Cyber Storm
  • 217
  • 1
  • 13
  • Big improvement with your coding style :D – Paul Samsotha Feb 05 '14 at 02:27
  • Also, you may want to make use if images and animating images. An example can be seen [**here**](http://stackoverflow.com/a/21534549/2587435). Also take a look at the [**Custom Graphics**](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and [**Graphics2D tutorial**](http://docs.oracle.com/javase/tutorial/2d/) – Paul Samsotha Feb 05 '14 at 02:44
  • Thanks, I'll have to look at that – Cyber Storm Feb 05 '14 at 02:56
  • Wow...i really don't know if I should try to do that yet, I probably could, but I really want to be strong with what I have learned first, before I add to it. Ha, thanks to you, I have to learn some more techniques on drawing and making frames. :P @Peeskillet Honestly, you should just teach me Java or get me a list of where to look for everything and in what order I should learn things. – Cyber Storm Feb 05 '14 at 03:11

1 Answers1

4

The problem is definitely caused by your code: you're overriding JPanel's getX() and getY() methods without realizing it, messing up the JPanel's innate positioning. Change the names of these methods please, perhaps to getMyX() and getMyY(), and your code will likely work better.

Also you can almost always be guaranteed that similar errors are due to our code and not Java, at least that has been my experience. So start by assuming that the error is yours, and you will almost always be right.

Also, this problem illustrates one risk with extending a complex class, and why you will want to minimize inheritance and favor composition if possible.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373