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 2 of 2

Thread: Possible misuse of timer?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Possible misuse of timer?

    I am a bit inexperienced when it comes to using timers, especially in graphical programs.
    I am trying to code a simple Tunnel game where the player is moving through a randomly generated tunnel and has to move left and right to avoid touching the walls.
    Here is my code:
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class Tunnel extends JFrame{
     
    	public static final  boolean empty = true;
    	public static final boolean full = false;
     
    	public static boolean [][] board = new boolean[400][400];
     
    	public Tunnel(){ 
     
     
    		add(new DrawTunnel());
     
    		setTitle("Tunnel");
    		setLocation(300,100);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().setSize(410,425);
    		setSize(417,440);
     
     
     
     
     
    		}
     
    	public static void main (String[] args){
    		Tunnel frame = new Tunnel();
    		frame.setVisible(true);
    	}
     
    	static class DrawTunnel extends JPanel{
     
    		Timer timer;
    		int middle = 200;
    		int width = 100;
     
    		public DrawTunnel(){
     
    			//create timer
    			timer = new Timer (5, new TimerListener());
    			timer.start();
     
    			emptyBoard();
     
    		}
     
    		public void paintComponent(Graphics g){
     
    			super.paintComponent(g);
     
    			g.setColor(Color.black);
    			for (int row= 0; row<board.length; row++){
    				for (int col=0; col<board[row].length; col++){
    					if (!board[row][col])
    						drawSquare(g, row,col);
    				}
    			}
    		}
     
    		public void drawSquare(Graphics g, int x, int y){
    			g.drawRect(x, y, 1, 1);
    		}
     
    		public void emptyBoard(){
    			for (int r=0; r<board.length; r++){
    				for (int c=0; c<board[r].length; c++){
    					board[r][c] = true;
    				}
    			}
    		}
     
    		class TimerListener implements ActionListener{
     
    			public void actionPerformed(ActionEvent e){
     
     
    				for (int r = board.length-2;r>=0;r--){
    					setRow(r,r+1);
    				}
     
    				drawTopLine(middle, width, 0);
     
    				repaint();
    				validate();
     
     
     
    			}
     
    			public void setRow(int rowOverwritten, int rowBeingCopied){
    				for (int c=0; c<board[rowBeingCopied].length; c++){
    					board[rowOverwritten][c] = board[rowBeingCopied][c];
    				}
    			}
     
    			public void setRow(boolean[][] board, int rowNum, boolean [] row){
    				for (int c=0; c<board[rowNum].length; c++){
    					board[rowNum][c] = row[rowNum];
    				}
    			}
     
    			public void drawTopLine(int gapMiddle, int gapWidth, int row){
     
    				for (int c=0; c<board[row].length; c++){
    					board[c][row] = false;
    				}
    				//forming gap
    				for (int c = gapMiddle - (gapWidth/2); c<gapMiddle + (gapWidth/2); c++){
    					board[c][row] = true;
    				}
    			}
    		}
    	}
    }

    When I run the program, only 1 row of pixelated "tunnel" appears at the top of the screen. Since I am not quite familiar with the Timer class and uses it for repetition, I guessed that I was misusing the timer. Where do I put the code to move all "tunnel walls" down and generate a new random one at the top?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Possible misuse of timer?

    Why don't beginners comment their code? What are all of these classes and methods supposed to do? Are we supposed to read your mind?

Similar Threads

  1. (Timer?) Problems
    By tyeeeee1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 10th, 2012, 03:24 PM
  2. game timer
    By powder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2012, 09:15 AM
  3. Timer digit
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 2nd, 2011, 08:07 PM
  4. Updating timer
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 26th, 2011, 07:02 AM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM

Tags for this Thread