Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need a project

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Need a project

    I was wondering if anyone had any project ideas i could use I'm kind of bored, and can't think of any on my own.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Need a project

    I've always wanted to create my own Java based forums software but that might be a bit of a big project. I'm keen on doing a small game using OpenGL with you if you like. Just as minor as minor can get really, start small.

    // Json

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need a project

    Hehe, i found a small project to do: Tetris

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need a project

    Quote Originally Posted by helloworld922 View Post
    Hehe, i found a small project to do: Tetris
    That would be fun to do
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need a project

    It's not much yet, but here's what i've got so far:
    Main applet program
    package applet;
     
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import pieces.Piece;
    import board.Board;
     
    public class Tetris extends Applet implements Runnable, KeyListener
    {
        Board board;
     
        public void init ()
        {
            board = new Board(10, 20);
            /*
             * test code: uncomment to test painting
             *
             * for (int i = 0; i < board.getHeight(); i++)
             * {
             *  for (int j = (int) (Math.random() * 5); j < board.getWidth(); j += (int) (Math.random() * 3) + 1)
             *   {
             *      board
             *               .addPiece(new Piece(new Color((float) Math.random(), (float) Math.random(), (float) Math
             *                       .random())), i, j);
             *   }
             * }
            */
     
        }
     
        public void paint (Graphics g)
        {
            Rectangle properties;
            double width;
            double height;
            // determine if width or height is smaller
            if (this.getWidth() / board.getWidth() < this.getHeight() / board.getHeight())
            {
                width = this.getWidth();
                height = (double) board.getHeight() * this.getWidth() / board.getWidth();
            }
            else
            {
                width = (double) board.getWidth() * this.getHeight() / board.getHeight();
                height = this.getHeight();
            }
            g.setColor(Color.BLACK);
            g.drawRect(0, 0, (int) width - 1, (int) height - 1);
            width--;
            height--;
            properties = new Rectangle(1, 1, (int) width, (int) height);
            board.paint(g, properties);
        }
     
        /**
         * @param e
         */
        public void keyPressed (KeyEvent e)
        {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_LEFT)
            {
                // move left
            }
            else if (keyCode == KeyEvent.VK_RIGHT)
            {
                // move right
                if (board.moveCurrPieceRight())
                {
                    repaint();
                }
            }
            else if (keyCode == KeyEvent.VK_SPACE)
            {
                // hard drop
                board.hardDrop();
                repaint();
            }
            else if (keyCode == KeyEvent.VK_DOWN)
            {
                // soft drop
                if (board.moveCurrPieceDown())
                {
                    repaint();
                }
            }
            else if (keyCode == KeyEvent.VK_UP)
            {
                // rotate CW
                if (board.rotateCurrPieceCW())
                {
                    repaint();
                }
            }
        }
     
        /**
         * @param e
         */
        public void keyReleased (KeyEvent e)
        {}
     
        /**
         * @param e
         */
        public void keyTyped (KeyEvent e)
        {}
     
        /**
         * 
         */
        public void run ()
        {
        // TODO Auto-generated method stub
     
        }
    }
    Board class
    package board;
     
    import java.awt.*;
    import java.util.*;
    import pieces.Piece;
     
    public class Board
    {
        Point[]            currPieceLoc;
        Piece              currPiece;
        ArrayList<Piece[]> board;
        int                width;
        int                height;
     
        /**
         * Constructs a board with given width and height
         * 
         * @param width
         * @param height
         */
        public Board (int width, int height)
        {
            this.width = width;
            this.height = height;
            currPieceLoc = new Point[4];
            currPieceLoc[0] = new Point();
            currPieceLoc[1] = new Point();
            currPieceLoc[2] = new Point();
            currPieceLoc[3] = new Point();
            board = new ArrayList<Piece[]>();
            board.ensureCapacity(height);
            for (int i = 0; i < height; i++)
            {
                board.add(new Piece[width]);
            }
        }
     
        /**
         * @return board width
         */
        public int getWidth ()
        {
            return width;
        }
     
        /**
         * @return board height
         */
        public int getHeight ()
        {
            return height;
        }
     
        /**
         * Adds a piece to the board. If out of bounds coords given, nothing is done
         * 
         * @param piece
         * @param row
         * @param col
         */
        public void addPiece (Piece piece, int row, int col)
        {
            try
            {
                board.get(row)[col] = piece;
            }
            catch (ArrayIndexOutOfBoundsException e)
            {
     
            }
        }
     
        /**
         * Clears a column and adds an empty one to the top
         * 
         * @param col
         */
        public void clearCol (int col)
        {
            board.remove(col);
            board.add(new Piece[width]);
        }
     
        /**
         * Paints the board and the current piece being moved
         * 
         * @param g
         * @param properties
         */
        public void paint (Graphics g, Rectangle properties)
        {
            // paint all items on the board
            for (int row = 0; row < height; row++)
            {
                Piece[] currCol = board.get(board.size() - row - 1);
                for (int col = 0; col < width; col++)
                {
                    if (currCol[col] != null)
                    {
                        // something to paint
                        Rectangle props = new Rectangle(properties.x + col * properties.width / width, properties.y + row
                                * properties.height / height, properties.width / width, properties.height / height);
                        currCol[col].paint(props, g);
                    }
                }
            }
            // paint the current piece
            for (int i = 0; i < currPieceLoc.length; i++)
            {
                if (currPieceLoc[i] != null && currPiece != null)
                {
                    Rectangle props = new Rectangle(properties.x + currPieceLoc[i].x * properties.width / width,
                                                    properties.y + currPieceLoc[i].y * properties.height / height,
                                                    properties.width / width, properties.height / height);
                    currPiece.paint(props, g);
                }
            }
        }
     
        /**
         * Determines if a location on the board is empty.
         * 
         * @param row
         * @param col
         * @return True if empty and in-bounds. False otherwise.
         */
        private boolean isLocEmpty (int row, int col)
        {
            try
            {
                if (board.get(row)[col] != null)
                {
                    return false;
                }
            }
            catch (ArrayIndexOutOfBoundsException e)
            {
                return false;
            }
            return true;
        }
     
        /**
         * Moves the current piece left
         * 
         * @return true if succeeded
         */
        public boolean moveCurrPieceLeft ()
        {
            for (int i = 0; i < currPieceLoc.length; i++)
            {
                if (currPieceLoc[i] != null)
                {
                    if (!isLocEmpty(currPieceLoc[i].y, currPieceLoc[i].x - 1))
                    {
                        return false;
                    }
                }
            }
            for (int i = 0; i < currPieceLoc.length; i++)
            {
                if (currPieceLoc[i] != null)
                {
                    currPieceLoc[i].x--;
                }
            }
            return true;
        }
     
        /**
         * Moves the current piece right
         * 
         * @return true if succeeded
         */
        public boolean moveCurrPieceRight ()
        {
            for (int i = 0; i < currPieceLoc.length; i++)
            {
                if (currPieceLoc[i] != null)
                {
                    if (!isLocEmpty(currPieceLoc[i].y, currPieceLoc[i].x + 1))
                    {
                        return false;
                    }
                }
            }
            for (int i = 0; i < currPieceLoc.length; i++)
            {
                if (currPieceLoc[i] != null)
                {
                    currPieceLoc[i].x++;
                }
            }
            return true;
        }
     
        /**
         * Moves the current piece down
         * 
         * @return true if succeeded
         */
        public boolean moveCurrPieceDown ()
        {
            for (int i = 0; i < currPieceLoc.length; i++)
            {
                if (currPieceLoc[i] != null)
                {
                    if (!isLocEmpty(currPieceLoc[i].y - 1, currPieceLoc[i].x))
                    {
                        return false;
                    }
                }
            }
            for (int i = 0; i < currPieceLoc.length; i++)
            {
                if (currPieceLoc[i] != null)
                {
                    currPieceLoc[i].y--;
                }
            }
            return true;
        }
     
        /**
         * Rotates the current piece CW
         * 
         * @return true if succeeded
         */
        public boolean rotateCurrPieceCW ()
        {
            return false;
        }
     
        /**
         * Rotates the current piece CCW
         * 
         * @return true if succeeded
         */
        public boolean rotateCurrPieceCCW ()
        {
            return false;
        }
     
        /**
         * Hard-drops the current piece. A hard-drop is dropping the current piece all the way to the bottom and locking it.
         */
        public void hardDrop ()
        {
            // keep moving the piece down until unable to
            while (moveCurrPieceDown())
            {
     
            }
        }
    }

    Piece class
    package pieces;
     
    import java.awt.*;
     
    /**
     * @author Andrew
     * 
     */
    public class Piece
    {
        Color color;
     
        public Piece (Color color)
        {
            this.color = color;
        }
     
        /**
         * Paints this piece
         * 
         * @param x
         * @param y
         * @param item
         */
        public void paint (Rectangle properties, Graphics g)
        {
            g.setColor(color);
            g.fill3DRect(properties.x, properties.y, properties.width, properties.height, true);
        }
    }

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Need a project

    Cool, good stuff

    // Json

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need a project

    I was wondering if someone would develop the graphics/gui? Right now, I pretty much have place-holder objects. Sound/Music would be nice, too.