Results 1 to 6 of 6
  1. #1
    Growler is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default Tetris Applet Help

    Hey guys,

    to keep my coding skills sharp this summer, I decided to program Tetris for fun.
    I'm having trouble getting the falling tetris pieces to respond to keyboard input (e.g. moveLeft(), moveRight(), rotate() etc...)
    Right now the piece is falling down at an interval set by my timer...

    So I have:

    KeyHandler - handles all key events (Right now I just have it handling moveLeft... once I get that to work, I'll add in the rest of the inputs):

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.JApplet;
    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.net.URL;
    import java.net.MalformedURLException;
    import java.awt.event.*;
    
    public class KeyHandler implements KeyListener {
        private TetrisPanel _tetrisPanel;
        
        public KeyHandler(TetrisPanel tp) 
        {
            _tetrisPanel = tp;
        }
        public void keyTyped(KeyEvent e) 
        {
            System.out.println("Key was typed.");
        }
        public void keyPressed(KeyEvent e) 
        {
            System.out.println("Key was pressed.");
            int keyCode = e.getKeyCode();
            switch(keyCode) 
            {
                case KeyEvent.VK_LEFT:
               _tetrisPanel.moveLeft();
                break;
                
                case KeyEvent.VK_P:
                _tetrisPanel.pause();
                break;
            }
        }
        public void keyReleased(KeyEvent e)
        {
        }
    }
    PieceFactory: generates tetris pieces from 12 rectangles (for each piece, say a Z, 4 out of the 12 rectangles would appear)
    This also has my move method in it:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    
    public class PieceFactory extends SmartRectangle 
    {
        protected SmartRectangle _rec1,_rec2,_rec3,_rec4,_rec5,_rec6,
                                _rec7,_rec8,_rec9,_rec10,_rec11,_rec12;
        private int _changeX, _changeY;
        private int _yDir, _xDir;
        private double _X, _Y;
        //getX is the location of this piece
        
        public PieceFactory(double x, double y, Color aColor)
        {
            super(aColor);
          // _aPiece = new SmartRectangle(x,y,150,200,Color.WHITE);
           _rec1 = new SmartRectangle(x, y, 25, 25, aColor);
           _rec2 = new SmartRectangle(x, y+25, 25, 25, aColor);
           _rec3 = new SmartRectangle(x, y+50, 25, 25, aColor);
           _rec4 = new SmartRectangle(x, y+75, 25, 25, aColor);
           _rec5 = new SmartRectangle(x+25, y, 25, 25, aColor);
           _rec6 = new SmartRectangle(x+25, y+25, 25, 25, aColor);
           _rec7 = new SmartRectangle(x+25, y+50, 25, 25,aColor);
           _rec8 = new SmartRectangle(x+25, y+75, 25, 25, aColor);
           _rec9 = new SmartRectangle(x+50, y, 25, 25, aColor);
           _rec10 = new SmartRectangle(x+50, y+25, 25, 25, aColor);
           _rec11 = new SmartRectangle(x+50, y+50, 25, 25, aColor);
           _rec12 = new SmartRectangle(x+50, y+75, 25, 25, aColor);
           
           _X = x; //this is how I am passing x parameter to this _X for the getX method
           _Y = y;
    
     public int getChangeY() 
        {
            return(_changeY);
        }
        public int getChangeX()
        {
            return(_changeX);
        }
    
        
        public void setChangeY(int yDir) {
           _changeY = yDir;
        }
        public void setChangeX(int xDir) { 
           _changeX = xDir; 
        }
        
     public void move(int changeinX, int changeinY)
            {
                _rec1.setLocation(_rec1.getX() + changeinX, _rec1.getY() + changeinY);
                _rec2.setLocation(_rec2.getX() + changeinX, _rec2.getY() + changeinY);
                _rec3.setLocation(_rec3.getX() + changeinX, _rec3.getY() + changeinY);
                _rec4.setLocation(_rec4.getX() + changeinX, _rec4.getY() + changeinY);
                _rec5.setLocation(_rec5.getX() + changeinX, _rec5.getY() + changeinY);
                _rec6.setLocation(_rec6.getX() + changeinX, _rec6.getY() + changeinY);
                _rec7.setLocation(_rec7.getX() + changeinX, _rec7.getY() + changeinY);
                _rec8.setLocation(_rec8.getX() + changeinX, _rec8.getY() + changeinY);
                _rec9.setLocation(_rec9.getX() + changeinX, _rec9.getY() + changeinY);
                _rec10.setLocation(_rec10.getX() + changeinX, _rec10.getY() + changeinY);
                _rec11.setLocation(_rec11.getX() + changeinX, _rec11.getY() + changeinY);
                _rec12.setLocation(_rec12.getX() + changeinX, _rec12.getY() + changeinY);
               }
    
        public void paint(Graphics2D pen)
        {
            //_aPiece.fill(pen);
        }
    }
    }
    TetrisPanel: Pulls tetris pieces at random from my PieceFactory and paints them to the screen. Also has my timer for animation.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    import java.applet.Applet;
    
    public class TetrisPanel extends JPanel implements ActionListener
    {
       private PieceFactory _piece;
       private final int INTERVAL = 1000;
       private javax.swing.Timer _timer;
       private Applet _thisApplet;
       
        public TetrisPanel(java.applet.Applet applet)
        {
           super();
           _thisApplet = applet;
           setBackground(Color.GRAY);
           _piece = new PieceFactory(125,0, Color.BLACK);
          
           change();
           
           
           _timer = new javax.swing.Timer(INTERVAL, this);
           _timer.start();
      
        }
        public void paint(Graphics pen)
        {
            super.paint(pen);
            Graphics2D aBetterPen = (Graphics2D)pen;
            _piece.paint(aBetterPen);
        }
        
        public void change()
        {
            _piece = TetrisPanel.newPiece(100,0);
            repaint();
        }
        
        /**-------------------------------MOVE METHODS --------------------------------*/
        public void moveLeft() {
            System.out.println("You've pressed \"Move Left\"");
            if(_piece.getChangeX() == 0) {
                _piece.setChangeX(-25);
                _piece.setChangeY(0);
            }
        }
        
       public void pause() {
            if(_timer.isRunning()) {
                _timer.stop();
            }
            else {
                _timer.restart();
            }
        }
                
        /**-----------------------------------------------------------------------------*/      
        
        public static PieceFactory newPiece(int x, int y)
        {
            Random random = new Random();
            Color aColor = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
            int random1 = (int)(Math.random() * 7) + 1;
            PieceFactory temp = new PieceFactory(x,y,aColor);// = new PieceFactory(x,y,aColor);
            
            switch(random1)
            {
                case 1: temp = new Z(x,y-50,aColor); break;
                case 2: temp = new L(x,y-25,aColor); break;
                case 3: temp = new S(x,y-50,aColor); break;
                case 4: temp = new T(x,y-50,aColor); break;
                case 5: temp = new Bar(x,y,aColor); break;
                case 6: temp = new Box(x,y-50,aColor); break;
                case 7: temp = new RevL(x,y-25,aColor); break;
                            
            }
            return temp;
        }
        public void actionPerformed(java.awt.event.ActionEvent e)
        {
            _piece.move(0, 25);
           // if 
           // _piece.moveLeft(-25, 0);
    
            this.repaint();
        }
    }
    TetrisApplet: which runs everything:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class TetrisApp extends JApplet
    {
        TetrisPanel _tetrisPanel;
        ButtonPanel _buttonPanel;
        
        public void init()
        {
            this.setSize(250, 500);
            
            _tetrisPanel = new TetrisPanel(this);
            _buttonPanel = new ButtonPanel(_tetrisPanel);
            this.add(_tetrisPanel, BorderLayout.CENTER);
            this.add(_buttonPanel, BorderLayout.SOUTH);
            this.addKeyListener(new KeyHandler(_tetrisPanel));
            this.setVisible(true);
        }
    }
    TETRIS PIECES:

    L:

    Java Code:
    import java.awt.*;
    
    public class L extends PieceFactory
    {
        private double _locX, _locY;
    
        public L(double x, double y, Color aColor)
        {
            super(x,y, aColor);
            _locX = x;
            _locY = y;
            
        }
        public void paint(Graphics2D pen)
        {
            super.paint(pen);
            _rec2.fill(pen);
            _rec3.fill(pen);
            _rec4.fill(pen);
            _rec8.fill(pen);
        }
        public void moveDown()
        {
            _rec2.setLocation(_locX, _locY +25);
            _rec3.setLocation(_locX, _locY +25);
            _rec4.setLocation(_locX, _locY +25);
            _rec8.setLocation(_locX, _locY +25);
        }
    }
    ReverseL:

    Java Code:
    import java.awt.*;
    public class RevL extends PieceFactory
    {
        private double _locX, _locY;
        
        public RevL(double x, double y, Color aColor)
        {
            super(x,y, aColor);
            _locX = x;
            _locY = y;
        }
        public void paint(Graphics2D pen)
        {
            super.paint(pen);
            _rec4.fill(pen);
            _rec6.fill(pen);
            _rec7.fill(pen);
            _rec8.fill(pen);
        }
        public void moveDown()
        {
            _rec4.setLocation(_locX, _locY +25);
            _rec6.setLocation(_locX, _locY +25);
            _rec7.setLocation(_locX, _locY +25);
            _rec8.setLocation(_locX, _locY +25);
        }
      
    }
    Bar:

    Java Code:
    import java.awt.*;
    public class Bar extends PieceFactory
    {
        private double _locX, _locY;
        
        public Bar(double x, double y, Color aColor)
        {
            super(x,y,aColor);
            _locX = x;
            _locY = y;
        }
        public void paint(Graphics2D pen)
        {
            super.paint(pen);
            _rec1.fill(pen);
            _rec2.fill(pen);
            _rec3.fill(pen);
            _rec4.fill(pen);
        }
        public void moveDown()
        {
            _rec1.setLocation(_locX, _locY +25);
            _rec2.setLocation(_locX, _locY +25);
            _rec3.setLocation(_locX, _locY +25);
            _rec4.setLocation(_locX, _locY +25);
        }
      
    }
    T:

    Java Code:
    import java.awt.*;
    public class T extends PieceFactory
    {
        private double _locX, _locY;
        
        public T(double x, double y, Color aColor)
        {
            super(x,y, aColor);
            _locX = x;
            _locY = y;
        }
        public void paint(Graphics2D pen)
        {
            super.paint(pen);
            _rec4.fill(pen);
            _rec7.fill(pen);
            _rec8.fill(pen);
            _rec12.fill(pen);
        }
        public void moveDown()
        {
            _rec4.setLocation(_locX, _locY +25);
            _rec7.setLocation(_locX, _locY +25);
            _rec8.setLocation(_locX, _locY +25);
            _rec12.setLocation(_locX, _locY +25);
        }
      
    }
    Z:

    Java Code:
    import java.awt.*;
    public class Z extends PieceFactory
    {
        private double _locX, _locY;
        
        public Z(double x, double y, Color aColor)
        {
            super(x,y, aColor);
            _locX = x;
            _locY = y;
        }
        public void paint(Graphics2D pen)
        {
            super.paint(pen);
            _rec3.fill(pen);
            _rec7.fill(pen);
            _rec8.fill(pen);
            _rec12.fill(pen);
        }
        public void moveDown()
        {
            _rec3.setLocation(_locX, _locY +25);
            _rec7.setLocation(_locX, _locY +25);
            _rec8.setLocation(_locX, _locY +25);
            _rec12.setLocation(_locX, _locY +25);
        }
    }
    S:

    Java Code:
    import java.awt.*;
    public class S extends PieceFactory
    {
        private double _locX, _locY;
        
        public S(double x, double y, Color aColor)
        {
            super(x,y, aColor);
            _locX = x;
            _locY = y;
        }
        public void paint(Graphics2D pen)
        {
            super.paint(pen);
            _rec4.fill(pen);
            _rec7.fill(pen);
            _rec8.fill(pen);
            _rec11.fill(pen);
        }
        public void moveDown()
        {
            _rec4.setLocation(_locX, _locY +25);
            _rec7.setLocation(_locX, _locY +25);
            _rec8.setLocation(_locX, _locY +25);
            _rec11.setLocation(_locX, _locY +25);
        }
    }
    Box:

    Java Code:
    import java.awt.*;
    public class Box extends PieceFactory
    {
        private double _locX, _locY;
        
        public Box(double x, double y, Color aColor)
        {
            super(x,y, aColor);
            _locX = x;
            _locY = y;
        }
        public void paint(Graphics2D pen)
        {
            super.paint(pen);
            _rec3.fill(pen);
            _rec4.fill(pen);
            _rec7.fill(pen);
            _rec8.fill(pen);
        }
        public void moveDown()
        {
            _rec3.setLocation(_locX, _locY +25);
            _rec4.setLocation(_locX, _locY +25);
            _rec7.setLocation(_locX, _locY +25);
            _rec8.setLocation(_locX, _locY +25);
        }
    }
    Button Panel:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ButtonPanel extends JPanel implements ActionListener
    {
        private JButton _button;
        private JButton _quit;
        private TetrisPanel _tetrisPanel;
        public ButtonPanel(TetrisPanel tp)
        {
            _button = new JButton("Change Peice");
            _quit = new JButton("Quit");
            _button.addActionListener(this);
            _quit.addActionListener(this);
            _tetrisPanel = tp;
            this.add(_button);
            this.add(_quit);
        }
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == _button)
            {
                _tetrisPanel.change();
            }
            else if(e.getSource() == _quit)
            {
                System.exit(0);
            }
        }
    }
    SmartRectangle:

    Java Code:
    /**
     * Chapter 7: SmartRectangle.java
     * Extends Java's Rectangle2D.Double class, adding the capabilities to
     * set color, rotation, location, and size, to move to a specified
     * location, and to display itself on a panel.
     */
    public class SmartRectangle extends java.awt.geom.Rectangle2D.Double {
        private java.awt.Color _borderColor, _fillColor; // attributes
        private int _rotation;
        private final int STROKE_WIDTH = 2;
    
        public SmartRectangle(java.awt.Color aColor){ 
        _borderColor = aColor;
        _fillColor = aColor; // solid color to start
        _rotation = 0; // no rotation for now
        }
        
         public SmartRectangle(double x, double y, int width, int height, java.awt.Color aColor){ 
            // solid color to start
            _borderColor = aColor;
            _fillColor = aColor;        
            // no rotation for now
            _rotation = 0;
            setLocation(x,y);
            setSize(width, height);
        }
        // methods not provided by Java
        public void setBorderColor (java.awt.Color aColor) {
        _borderColor = aColor;
        }
        public void setFillColor (java.awt.Color aColor) {
        _fillColor = aColor;
        }
        public void setRotation (int aRotation) {
        _rotation = aRotation;
        }
        
    
        // more readable versions of methods provided by Java
        public void setLocation (double x, double y) {
        this.setFrame (x, y, this.getWidth(), 
                   this.getHeight());
        }
        public void setSize (int aWidth, int aHeight) {
        this.setFrame(this.getX(), this.getY(), 
                  aWidth, aHeight);
        }
        public void move (double aChangeInX, double aChangeInY) {
        this.setFrame(this.getX()+aChangeInX,
                  this.getY()+aChangeInY,
                  this.getWidth(),
                  this.getHeight());
        }
    
        public void fill (java.awt.Graphics2D aBetterBrush){
        java.awt.Color savedColor = aBetterBrush.getColor();
        aBetterBrush.setColor(_fillColor);
        aBetterBrush.fill(this); // paint a solid rectangle
        aBetterBrush.setColor(savedColor);
        }
        public void draw (java.awt.Graphics2D aBrush) {
        java.awt.Color savedColor = aBrush.getColor();
        aBrush.setColor(_borderColor);
        java.awt.Stroke savedStroke = aBrush.getStroke();
        aBrush.setStroke(new java.awt.BasicStroke(STROKE_WIDTH));
        aBrush.draw(this);
        aBrush.setStroke(savedStroke);
        aBrush.setColor(savedColor);
        }
    }
    Last edited by Growler; 08-04-2010 at 06:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Can you post all of the code if you expect anyone to compile execute and test it.

  3. #3
    Growler is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Okay it's all there.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Thanks for posting all that.
    Its way too much code to wade thru to find your bug.

    Can you write a small program that demonstrates the problem that compiles and executes?


    Another way for you to debug your program is to add some println() statements to your code to show how variables are changed and how the code is executing. Then you can watch the print out and see if the code and variables change as you expect them to.
    Last edited by Norm; 08-04-2010 at 07:33 PM. Reason: How to debug your code

  5. #5
    Growler is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Thanks for posting all that.
    Its way too much code to wade thru to find your bug.

    Can you write a small program that demonstrates the problem that compiles and executes?


    Another way for you to debug your program is to add some println() statements to your code to show how variables are changed and how the code is executing. Then you can watch the print out and see if the code and variables change as you expect them to.
    I've got println() statements everywhere.. haha.

    It's just I can't syntactically get the object to respond to my key inputs.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I can't syntactically get the object to respond to my key inputs.
    "syntactically" sounds like a compiler error. Are you getting any?

    Otherwise, when the code enters a method due to a key press, print out what is happening.
    Show how the variables are changed as a result. The variables are what controls the rest of the program.
    When the execution now gets to where something is drawn, show the values of the variables that control how the code draws. Some where there should be an inconsistency or error that you can see in the print out.
    If all the output is as you expect but the output isn't then you are not outputting the correct variables to demonstrate what is happening. Go back and find which ones are not being shown and add them the to println()s.

    Its a slow and iterative process. You can spend hours and days going around and around until you catch the error.

    Good luck and good hunting.

Similar Threads

  1. Java Tetris 1.0
    By levent in forum Java Software
    Replies: 1
    Last Post: 01-07-2012, 09:07 AM
  2. Replies: 6
    Last Post: 12-15-2009, 03:33 AM
  3. Calling another applet on click of button in one applet
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 02-19-2009, 01:54 PM
  4. show a high score from tetris
    By stessie in forum Java Applets
    Replies: 0
    Last Post: 03-19-2008, 04:00 AM
  5. Replies: 4
    Last Post: 06-08-2007, 07:15 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
  •