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

Thread: to print pattern

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Lightbulb to print pattern

    I got a question in a java quiz which is to Print the Pattern shown below but i could not crack...need ur help in this

    NITIN
    I I
    T T
    I I
    NITIN


    i did it by using charAt but it printed all like..
    NITIN
    I
    T
    I
    N
    I
    T
    I
    N
    .
    .
    .
    and so on can u please give me some idea to do so


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: to print pattern

    Please share the code you have

  3. The Following User Says Thank You to copeg For This Useful Post:

    Charanleen (October 2nd, 2010)

  4. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: to print pattern

    Thanks for ur soon reply sir, that was a mini problem and by Gods Grace i am solving that but this which i am going to post is simply unbreakable for me...i was making a ball game using applet but keyListener is not working...i want to move my plate using keys but there is a big halt...
    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    /*
    <applet code="moveball" width=500 height=500>
    </applet>
    */
     
     public class moveball  extends Applet implements KeyListener,Runnable
    {
     int p=0,q=0,dp = 3, dq = 3; Thread t;
     boolean left,right;
     
     
     public void init()
     {
       addKeyListener(this);
       setBackground(Color.cyan);
     
     }
     
     
     public void keyPressed(KeyEvent k)
     {
     
     
      switch(k.getKeyCode())
       {
         case KeyEvent.VK_LEFT:
         left = true;showStatus("Key pressed left");
         break;
     
         case KeyEvent.VK_RIGHT:
         right = true;showStatus("Key pressed right");
         break;
       }
     
     
     } 
     
     
     public void keyReleased(KeyEvent k)
     {
     
     
       switch(k.getKeyCode())
        {
        case KeyEvent.VK_LEFT:
        left = false;showStatus("Key released left");
        break;
     
        case KeyEvent.VK_RIGHT:
        right = false;showStatus("Key released right");
        break;
        }
     
     
     } 
     
     
     public void keyTyped(KeyEvent k)
     {
      showStatus("Key Typed");
     } 
     
     
     
     public void start()
     {
      t= new Thread(this,"Demo");
      t.start();
     }
     
     
     public void run()
     {
     for(;;)
      {
       try
      {
       repaint(); 
      Thread.sleep(10);
      }catch(InterruptedException e){}
     
      }
     
     }
     
     
     public void paint(Graphics g)
     { 
      Dimension d = this.getSize();
     
      int u=d.width/2,v=d.height-10,up=15;
     
      g.fillRect(u,v,40,6);
     
      if(left==true && u>0)
       {
        u-=up;
       }
     
       if(right==true && u<d.width)
       {
        u+=up;
       }
      g.fillRect(u,v,40,6);
     
      g.fillOval(p,q,16,16);
      p += dp;
      q += dq;
     
     
       if (p < 0)
       {
         p = 0;
         dp = -dp;
       }
     
       if (p  >= d.width)
       {
          p = d.width ;
          dp = -dp;
       }
     
       if (q < 0)
       {
          q = 0;
          dq = -dq;
       }
     
       if (q  >= d.height )
       {
          q = d.height ;
          dq = -dq;
       }
     
        g.fillOval(p, q, 16, 16); 
     
     
     } 
    }
    Last edited by helloworld922; October 3rd, 2010 at 10:31 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: to print pattern

    but there is a big halt.
    Why is there a halt? What variables control the movement of the ball? Are those variables being changed as you want?

    Try debugging your code by adding print outs to show the values of the variables as they are changed and as they are used. You should be able to see where they are being changed incorrectly and correct it so the ball moves as you want it to.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Charanleen (October 2nd, 2010)

  7. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: to print pattern

    ball is moving correctly but the plate at which it should strike is not ....if it doesn't move i won't be able to write the striking condition of ball onto the plate ... but that was a good idea to add SOP statements.. i'll just do it and let u know it worked or not..thanks..

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: to print pattern

    Also I notice you have two booleans: left and right for controlling the movement.
    Can they both be true? The code in the paint method looks like it expects that. Otherwise the test for right would be in an else if

    Does your applet have focus? It needs it for the keylistener. Click on the applet to give it focus while testing

  9. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: to print pattern

    Quote Originally Posted by Norm View Post
    Also I notice you have two booleans: left and right for controlling the movement.
    Can they both be true? The code in the paint method looks like it expects that. Otherwise the test for right would be in an else if

    Does your applet have focus? It needs it for the keylistener. Click on the applet to give it focus while testing

    Sir would you please check it by executing Key Listeners are now Ok(thanks for that) but the plate is having chaos ..would you please take a look

     
    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    /*
    <applet code="moveball" width=500 height=600>
    </applet>
    */
     
     public class moveball  extends Applet implements KeyListener,Runnable
    {
     int p=0,q=0,dp = 3, dq = 3; Thread t;
     boolean left=false,right=false;
     
     
     public void init()
     {
       addKeyListener(this);
       setBackground(Color.cyan);
     
     }
     
     
     public void keyPressed(KeyEvent k)
     {
      System.out.println("pressed");
     
      switch(k.getKeyCode())
       {
         case KeyEvent.VK_LEFT:
         left = true;showStatus("Key pressed left");
         break;
     
         case KeyEvent.VK_RIGHT:
         right = true;showStatus("Key pressed right");
         break;
     
     
     
       }
     
     
     } 
     
     
     public void keyReleased(KeyEvent k)
     {
       System.out.println("released");
     
       switch(k.getKeyCode())
        {
        case KeyEvent.VK_LEFT:
        left = false;showStatus("Key released left");
        break;
     
        case KeyEvent.VK_RIGHT:
        right = false;showStatus("Key released right");
        break;
        }
     
     
     } 
     
     
     public void keyTyped(KeyEvent k)
     {
      showStatus("Key Typed");
     } 
     
     
     
     public void start()
     {
      t= new Thread(this,"Demo");
      t.start();
     }
     
     
     public void run()
     {
     for(;;)
      {
       try
      {
       repaint(); 
       Thread.sleep(3);
      }catch(InterruptedException e){}
     
      }
     
     }
     
     
     public void paint(Graphics g)
     { 
        Dimension d = this.getSize();
     
        int u=d.width/2,v=d.height-10,up=15;
     
        g.fillRect(u,v,40,6);
     
        while(left==true && u>0)
        {u-=up; g.fillRect(u,v,40,6);}
     
        while(right==true && u<d.width)
        {u+=up; g.fillRect(u,v,40,6);}
        g.fillRect(u,v,40,6);
     
      g.fillOval(p,q,16,16);
      p += dp;
      q += dq;
     
     
       if (p < 0)
       {
         p = 0;
         dp = -dp;
       }
     
       if (p  >= d.width)
       {
          p = d.width ;
          dp = -dp;
       }
     
       if (q < 0)
       {
          q = 0;
          dq = -dq;
       }
     
       if (q  >= d.height )
       {
          q = d.height ;
          dq = -dq;
       }
     
        g.fillOval(p, q, 16, 16); 
     
     
     } 
    }
    Last edited by helloworld922; October 3rd, 2010 at 10:31 AM.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: to print pattern

    the plate is having chaos
    Please describe what the plate is doing.
    Try debugging your code by adding print outs to show the values of the variables as they are changed and as they are used. You should be able to see where they are being changed incorrectly and correct it so the plate moves as you want it to.

    Please put posted code in code tags. Info here: Java Forums - BB Code List

  11. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: to print pattern

    the plate should move left every time i press left key and right when i press right key by 3 units but when i press left or right key the plate just lengthens till the end of applet window i.e. if plate is at the center and i press "->" the plate will not move but instead its length increases till the last point of window but i want whenever i press that key the plate should move by 3 and remain there until i move it again.

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: to print pattern

    Try debugging your code by adding print outs to show the values of the variables as they are changed and as they are used. You should be able to see where they are being changed incorrectly and correct it so the plate moves as you want it to.

    YOU need to see what the code is doing. Print out the values to see why it is doing what it is doing.

Similar Threads

  1. Help with regex pattern
    By b_jones10634 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 24th, 2010, 03:59 PM
  2. a new pattern with a new architecture
    By mcgrow in forum Web Frameworks
    Replies: 0
    Last Post: August 4th, 2010, 02:28 PM
  3. Stuck -- Loop Pattern
    By CodeNewb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 11th, 2010, 03:02 AM
  4. [SOLVED] Need Regex for a pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 7
    Last Post: May 5th, 2010, 02:06 AM
  5. url pattern issue - please help me...
    By bharathik in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 9th, 2009, 04:28 AM