Results 1 to 5 of 5
  1. #1
    SkinnyK is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default X / Y Translation Problem

    I am working on a tetris clone to try and become familiar with Java. The first code example is a simple array to create the whole piece.

    Java Code:
    public class Tetromino {
        
        int mesh[][] = new int[5][5];
    
        public int[][] squareTetromino () {
            mesh[2][2] = 2;
            mesh[2][3] = 1;
            mesh[3][2] = 1;
            mesh[3][3] = 1;
            return mesh;
        }
    
    }
    The next code example is drawing the block to the screen. The array is being run through and each block is being drawn separately to create a whole piece. As you can see currently I am drawing each block in the same place. The problem I am having is that I cannot come up with a generic way to compute the X and Y values to translate each piece accordingly in a way that would work with any tetris piece.

    Java Code:
    public class Playfield extends JPanel {
    
        Tetromino tetromino = new Tetromino();
    
        @Override public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            for(int i = 0; i < tetromino.squareTetromino().length; i++) {
                for(int j = 0; j < tetromino.squareTetromino()[i].length; j++) {
                    if(tetromino.squareTetromino()[i][j] == 1) {
                        g.fillRect(100, 100, 10, 10);
                    } else if(tetromino.squareTetromino()[i][j] == 2) {
                        g.fill3DRect(100, 100, 10, 10, true);
                    }
                }
            }
        }
    }
    Any help at all would be very very appreciated.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    Until someone who knows more comes along, I was working on Printing a few days ago, I decided all of this is already pre-built in the Grapics2D and in paticular, translation is there.

    Somewhere.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    SkinnyK is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the suggestion, I am reading that trail right now. Hopefully some more ideas will come along, keep em coming guys.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PlayfieldRx extends JPanel {
        Tetromino tetromino = new Tetromino();
        int[][] grid = tetromino.squareTetromino();
    
        @Override protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();
            int rows = grid.length;
            int cols = grid[0].length;
            int xInc = w / cols;
            int yInc = h / rows;
            g.setColor(Color.RED);
            for(int i = 0; i < rows; i++) {
                for(int j = 0; j < cols; j++) {
                    int x = j * xInc;
                    int y = i * yInc;
                    if(grid[i][j] == 1) {
    //                    g.fillRect(100, 100, 10, 10);
                        g.fillRect(x, y, xInc, yInc);
                    } else if(grid[i][j] == 2) {
    //                    g.fill3DRect(100, 100, 10, 10, true);
                        g.fill3DRect(x, y, xInc, yInc, true);
                    } else {
                        g.drawRect(x, y, xInc, yInc);
                    }
                }
            }
            g.drawLine(w-1, 0, w-1, h-1);
            g.drawLine(0, h-1, w-1, h-1);
        }
    
        public static void main(String[] args) {
            PlayfieldRx panel = new PlayfieldRx();
            panel.setPreferredSize(new Dimension(300,300));
            JOptionPane.showMessageDialog(null, panel, "", -1);
        }
    }
    
    class Tetromino {
        int[][] mesh = new int[5][5];
    
        public int[][] squareTetromino() {
            mesh[2][2] = 2;
            mesh[2][3] = 1;
            mesh[3][2] = 1;
            mesh[3][3] = 1;
            return mesh;
        }
    }

  5. #5
    SkinnyK is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    I decided to check back, thank you for posting a possible solution Hardwired. This is what I came up with, not much different from my first post but I finally figured it out.

    Java Code:
    public class Tetromino {
        // [X][Y] Build top down, starting at the top left.
        int mesh[][] = new int[5][5];
    
        public int[][] squareTetromino() {
            mesh[2][2] = 2;
            mesh[2][3] = 1;
            mesh[3][2] = 1;
            mesh[3][3] = 1;
            return mesh;
        }
    
        public int[][] sTetromino() {
            mesh[2][2] = 2;
            mesh[3][2] = 1;
            mesh[2][3] = 1;
            mesh[1][3] = 1;
            return mesh;
        }
    
    }
    Java Code:
    public class Playfield extends JPanel {
    
        Tetromino tetromino = new Tetromino();
    
        @Override public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            for(int i = 0; i < tetromino.sTetromino().length; i++) {
                for(int j = 0; j < tetromino.sTetromino()[i].length; j++) {
                    if(tetromino.sTetromino()[i][j] == 1) {
                        [COLOR="Red"]g.fillRect(100 + (i * 10), 100 + (j * 10), 10, 10);[/COLOR]
                    } else if(tetromino.sTetromino()[i][j] == 2) {
                        [COLOR="Red"]g.fill3DRect(100 + (i * 10), 100 + (j * 10), 10, 10, true);[/COLOR]
                    }
                }
            }
        }
    }
    Last edited by SkinnyK; 01-16-2009 at 12:22 AM.

Similar Threads

  1. Transform Translation and Rotation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 10:58 PM
  2. Transform Rotation Translation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:53 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
  •