Results 1 to 10 of 10
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default repaint class doesnt work anymore... dunno why..

    Hai!
    I'v had this script and the paint thingy have worked before..
    But now It has stopped working.
    Cause I'v changed the consept of how it loads images and animations..
    But anyways, the timer works and everything b4 that do too.
    The only problem is, that I get no error when it does repaint, but if I put
    Java Code:
    System.out.println("work")
    at first or second line in the method paintComponent then it wont print either..
    It's realy reallly weard..
    I appriciate all help!

    Heres the code:
    PHP Code:
    /**
     * You must use Threading!
     */
     
    import src.LoadImages;
    import src.Animation;
    import javax.swing.*;
    import javax.imageio.*;
    import java.awt.*;
    import java.awt.event.*; 
    import java.io.*;
    import java.awt.image.*;
    import java.awt.image.BufferedImage;
    import java.util.*;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class TestingArea  {
    	
    
        static Vector <Image> list = new Vector<Image>();
        static gameFrame gf;
        public static void main(String[] args) {
        	gf = new gameFrame();
        	gf.addKeyListener(new MyKeyListener());
        	paintIt obj = new paintIt();
        	try{
        		BufferedImage Images[];
        		do{
        			Images = LoadImages.LoadCharacter();
        		}while (Images == null);
        		System.out.println("Images Loaded!");
        		int amount = 0;
        		for (BufferedImage x: Images){
        			amount++;
        			System.out.println(x.getHeight());
        		}
        		System.out.println("Loaded a total of "+amount+" Images!");
        		BufferedImage[][] animations = Animation.getAnimation(Images);
        		System.out.println("Animation Loaded!");
        		amount = 0;
        		for (BufferedImage[] x: animations){
        			amount++;	
        		}
        		System.out.println("Loaded a total of "+amount+" Animations!");
        		obj.addAnm(animations);
        		Player.SizeY = Images[0].getTileHeight(); //getHeight();
        	} catch (Exception e){
        	}
        	
        	gf.add(obj);
        	
        	javax.swing.Timer timer = new javax.swing.Timer(50,new MyTimerActionListener());
        	timer.start();
    
        }
        
        
    }
    
    class Player{
    	static int X = 0;
        static int Y = 0;
        static int[] CharVel = {10,10};
        static int SizeY = 81;
        static int Friction = 2;
        //CharWalk [x,y]
        static int[] CharWalk = {0,0};
        static String isPressed = "";
        static String mode = "Walking";
        static boolean forward = true;
    }
    
    class MyKeyListener extends KeyAdapter{
    	public void keyPressed(KeyEvent ke){
    		char i = ke.getKeyChar();
    	    if (i=='a'){
    	    	Player.isPressed = "a";
    	    	Player.mode = "Walking";
    	    	Player.CharWalk[0] = -10;
    	    	Player.forward = true;
    	    }
    	   	if (i=='d'){
    	   		Player.isPressed = "d";
    	   		Player.mode = "Walking";
    	    	Player.CharWalk[0] = 10;
    	    	Player.forward = false;
    	    } 
    	    if (i=='w' && Player.mode == "Walking"){
    	    	Player.CharVel[1] = -10;
    	    	Player.mode = "Jumping";
    	    	try{
    	    		Thread.currentThread().sleep(1000);
    	    	} catch (Exception e) {}
    	    	Player.mode = "Standing";
    	    } 
    	    	
    	}
    	public void keyReleased(KeyEvent ke){
    
    		char i = ke.getKeyChar();
    	    if (i=='a' && Player.isPressed == "a"){
    	    	Player.CharWalk[0] = 0;
    	    	Player.mode = "Standing";
    	    }
    	   	if (i=='d' && Player.isPressed == "d"){
    	    	Player.CharWalk[0] = 0;
    	    	Player.mode = "Standing";
    	    } 
    	}
    }
    
    	
    class MyTimerActionListener implements ActionListener {
      	public void actionPerformed(ActionEvent e) {
      		if (!(Player.Y+Player.SizeY+2> 400)){
      			Player.CharVel[1] += 2;
      		} else {
      			Player.CharVel[0] = (Player.CharVel[0]/Player.Friction);
      		}
        	if (Player.Y+Player.SizeY> 400){
        		
        		Player.CharVel[1] = -Player.CharVel[1]/2;
        		Player.Y = 400-Player.SizeY;
        	}
        	//System.out.println("Velocity: x: "+TestingArea.CharVel[0]+" y: "+TestingArea.CharVel[1]);
        	Player.Y += Player.CharVel[1];
        	//if (TestingArea.CharVel[1]<5 && TestingArea.CharVel[1] >-5){
        	//	TestingArea.Y = TestingArea.Y-TestingArea.CharVel[1];
        		//TestingArea.CharVel[1] = 0;
        		
        	//}
        	Player.X += Player.CharVel[0]+Player.CharWalk[0];
    
    
        	//Graphics g;
        	System.out.println("paintit again");
        	TestingArea.gf.repaint();
        	
      	}
      	
      	
    }
    
    class gameFrame extends JFrame { //implements Runnable
    	//public void run(){
    	//	paintIt p = new paintIt();
    	//	add(p);
    	//}
    	public gameFrame(){
    		
    		
    		
    		//paintIt p = new paintIt();
    		//add(p);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(600,450);
    		setTitle("Image test");
    		setVisible(true);
    	}
    }
    
    class paintIt extends JPanel{
    	BufferedImage[] stand;
    	BufferedImage[] walk;
    	BufferedImage[] jump;
    	BufferedImage[] currentAnm = null;
    	
    	int change = 0;
    	public void addAnm(BufferedImage[][] animation){
    		this.walk = animation[1];
    		//TestingArea.list.add(walk[0]);
    		this.stand = animation[0];
    		this.jump = animation[2];
    		System.out.println("Splitted animations!");
    	}
    	
    	public void paintComponent(Graphics g){
    		System.out.println("paint");
    		super.paintComponent(g);
    		
    		if (Player.mode == "Walking"){
    			currentAnm = this.walk;
    		}
    		if (Player.mode == "Standing"){
    			currentAnm = this.stand;
    		}
    		if (Player.mode == "Jumping"){
    			currentAnm = this.jump;
    		}
    		
    		if (currentAnm != null){
    			
    			change += 1;
    			if (Player.forward == false){
    				if (change <11){
    					
    					g.drawImage(currentAnm[0],Player.X,Player.Y,null);
    				}
    				if (change <20 && change >10){
    					
    					g.drawImage(currentAnm[1],Player.X,Player.Y,null);
    				}
    				if (change <30 && change >19){
    					
    					g.drawImage(currentAnm[2],Player.X,Player.Y,null);
    				}
    				
    			} else {
    				if (change <11){
    					
    					g.drawImage(currentAnm[3],Player.X,Player.Y,null);
    				}
    				if (change <20 && change >10){
    					
    					g.drawImage(currentAnm[4],Player.X,Player.Y,null);
    				}
    				if (change <30 && change >19){
    					
    					g.drawImage(currentAnm[5],Player.X,Player.Y,null);
    				}
    				
    			}
    			if (change==29){
    				change = 0;
    			}
    			
    			
    			
    			
    		}
    		g.setColor(Color.GREEN);
    		g.fillRect(1,400,1000,100);
    	}
    	
    }

    Sry if its long..

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I found out a new thing.
    When you increese the window size, it all starts working and it will repaint..
    Any idea why it doesnt repaint before I change the window size?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You will likely increase your chances of getting help if cut that code down to a reasonable size program that we can compile and test.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A quick peek at your code reveals:
    Java Code:
    if (Player.mode == "Walking"){
    Much better:
    Java Code:
    if (Player.mode.equals("Walking")){
    Do you understand why this is so?

    Also, I'm not able to compile your code as it has dependencies that I don't have access to, such as src.LoadImages and src.Animation. We'll be able to help you if you create small programs that demonstrate your problem but that don't have these dependences, allowing us to compile and run your code. Much luck.

    Next: you appear to have your animation logic within your paintComponent code. Don't do this as you have a lot less control over when paintComponent gets called then you think. Get the logic out, (such as changing the "change" variable from within this method), change change from within a Swing Timer, and then call repaint to ask paintComponent to redraw your JPanel using the new change value.
    Last edited by Fubarable; 11-07-2009 at 03:29 PM.

  5. #5
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    yea I knew I would have cutdown the code..
    Tho thats when people ask for the whole code..
    But anyways, I fixed it by after making the jframe window
    I increesed the size of it, and voila fixed XD
    Thanks for reading anyways =)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Addez View Post
    yea I knew I would have cutdown the code..
    Tho thats when people ask for the whole code..
    No, we ask for small compilable code, not the whole code.
    But anyways, I fixed it by after making the jframe window
    I increesed the size of it, and voila fixed XD
    Thanks for reading anyways =)
    you're welcome, but did you fix the other problems in your program that I've outlined above? They are pretty serious.

  7. #7
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    yes, those are fixed, but not using equals (always forget using that)
    But instead I converted it to characters and then I was allowed to use ==
    :D

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Addez View Post
    yes, those are fixed, but not using equals (always forget using that)
    But instead I converted it to characters and then I was allowed to use ==
    :D
    OK. What about getting the program logic out of the paint/paintComponent method?

  9. #9
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    ehh what u meen?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Right now, your animation steps every time paintComponent is called, and this is not good because you don't have full control of this. paintComponent can be suggested to be called by your program by calling repaint(), but also can be called by the JVM after the operating system tells it that a window is dirty and needs to be repainted. This will result in some very unreliable animations.

    Much better is to have you logic (for instance the incrementing of your animation steps) outside of paintComponent, say in a Swing Timer, and then calling repaint from within the timer's actionPerformed method. The paintComponent can then use the updated counter variable to draw the correct animation image, but paintComponent shouldn't update this variable itself.

Similar Threads

  1. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 09:31 PM
  2. My blackjack games random generator doesnt work!
    By Addez in forum New To Java
    Replies: 16
    Last Post: 08-17-2009, 05:29 AM
  3. java doesnt allow vista to work
    By 10rosas in forum New To Java
    Replies: 5
    Last Post: 12-22-2008, 04:23 PM
  4. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 04:43 PM
  5. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 02:50 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
  •