Results 1 to 9 of 9
  1. #1
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default I need help with this for loop

    This is related to my last thread, but no longer about lists as the other one was titled. Since then I have tried to put the main loop in the paint() method, but it still doesn't help, something must be wrong with this for loop.

    Java Code:
    for(int x = 0; x >= tw.getInfo().size(); x++){
    				g.drawString(tw.getInfo().get(x), 100, lastY * 100);
    				lastY ++;
    			}
    Because it doesn't draw the strings even though I input data into the list in the TextWall class. Here is the full code:

    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import javax.swing.JFrame;
    
    
    public class Main extends Applet{
    	
    	public boolean gameOver = false;
    	public int gameState = 0;
    	public TextWall tw = new TextWall();
    	private int lastY = 0;
    	public Graphics g;
    	
    	public void paint(Graphics g){
    		while(!gameOver){
    			for(int x = 0; x >= tw.getInfo().size(); x++){
    				g.drawString(tw.getInfo().get(x), 100, lastY * 100);
    				lastY ++;
    			}
    			mainLoop();
    		}
    	}
    	
    	public void init(){;
    		tw.addInfo(" "); //I get IndexOutOfBoundsException if I don't add this.
    	}
    	
    	public static void main(String[] args){
    		
    		JFrame frame = new JFrame();
    		Main a = new Main();
    		frame.getContentPane().add(a, BorderLayout.CENTER);
    		frame.setSize(new Dimension(1360,768));
            frame.setVisible(true);
            
    	}
            //Main Loop
        public void mainLoop(){
            if(gameState == 0){ //Main Menu
            	tw.addInfo("Main Menu");
            } else if(gameState == 1){
            		
            } else if(!(gameState == 1 || gameState == 0)){
            		
            }
        }
    }
    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class TextWall {
    	
    	private List<String> info;
    	
    	public TextWall(){
    		info = new ArrayList<String>();
    	}
    	
    	public void addInfo(String s){
    		info.add(s);
    	}
    	
    	public void removeInfo(int i){
    		if(info.contains(i)){
    			info.remove(i);
    		}else{
    			System.err.println("String was not found.");
    		}
    	}
    	
    	public List<String> getInfo(){
    		return info;
    	}
    	
    	public void repaint(){
    		
    	}
    }

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

    Default Re: I need help with this for loop

    Does the loop ever execute? Is the value of tw.getInfo().size() less than 0? That is what the >= operator requires with x starting at 0;
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: I need help with this for loop

    Yeah you were right, I used the wrong symbol there xD

    Java Code:
    public void paint(Graphics g){
    		while(!gameOver){
    			for(int x = 0; x <= tw.getInfo().size(); x++){
    				g.drawString("Works" + x, 100, lastY * 100);
    				g.drawString(tw.getInfo().get(x), 100, lastY * 100);
    				lastY ++;
    			}
    			mainLoop();
    		}
    	}
    This line, however, doesn't work. But now the for loop will display "Works0" and "Works1" like it's supposed to

    Java Code:
    g.drawString(tw.getInfo().get(x), 100, lastY * 100);
    Exception in thread "AWT-EventQueue-1" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Main.paint(Main.java:18)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-1" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Main.paint(Main.java:18)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

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

    Default Re: I need help with this for loop

    java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    ArrayList indexes range in value from 0 to the size-1. An arraylist with 2 elements has indexes 0 and 1. The index 2 is past the end. Check that the code does not allow the index to get past size-1
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: I need help with this for loop

    Adding a -1 to the end of the .size() greatly fixed that problem xD

    So I guess I'm onto the next challenge; getting it to scroll.

    Before that, I need help with one last bug (At least for this portion of the process,) and that would be that because pretty much everything has to be in a main loop, when tw.addInfo() is called, it only get drawn once unless called again.


    This code doesn't work.

    Java Code:
    public void addInfo(String s){
    		if(!info.contains(s)){
    			info.add(s);
    		}
    	}

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: I need help with this for loop

    Quote Originally Posted by Darkzombies View Post
    This code doesn't work.
    That provides zero information. Does it generate errors? Does it show unexpected behavior? Does it create a black hole?

  7. #7
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: I need help with this for loop

    Well the sentence above that says: when tw.addInfo() is called, it only should get drawn once unless called again.

    and it fails that objective, it draws it continuously. I guess instead of sticking that for loop in the paint() method, I can have it be drawn in the addInfo() method, and set variables that will eventually result in scrolling.

    I'm also having trouble with optimization and the scrolling. It lags, and all it does is type text (It's probably lagging because it's printing infinitely, until I fix that) and with the scrolling, I feel like I'm adding unnecessary flags and variables.

    E.G.

    Java Code:
    public void paint(Graphics g){
    		while(!gameOver){
    			for(int x = 0; x <= tw.getInfo().size() - 1; x++){
    				g.drawString(tw.getInfo().get(x), 100, lastY * 20);
    				
    				if(lastY * 20 >= 650){
    					reachedBottom = true;
    				}
    				
    				if(!reachedBottom){
    					lastDeleted ++;
    					lastY ++;
    				}
    			}
    			mainLoop();
    		}
    	}
    Last edited by Darkzombies; 05-05-2013 at 07:48 AM.

  8. #8
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: I need help with this for loop

    you can just call repaint() and it will execute the paint method for you, so why does everything have to be in the main loop?

  9. #9
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: I need help with this for loop

    Quote Originally Posted by monkeyjr97 View Post
    you can just call repaint() and it will execute the paint method for you, so why does everything have to be in the main loop?
    repaint() can't be called from the main method, so it complicates things.

Similar Threads

  1. Replies: 7
    Last Post: 04-03-2013, 09:26 PM
  2. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  5. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 05:21 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
  •