Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2009, 01:37 AM
Senior Member
 
Join Date: Aug 2009
Posts: 179
Rep Power: 1
Addez is on a distinguished road
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
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 xImages){
                
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[] xanimations){
                
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+2400)){
              
Player.CharVel[1] += 2;
          } else {
              
Player.CharVel[0] = (Player.CharVel[0]/Player.Friction);
          }
        if (
Player.Y+Player.SizeY400){
            
            
Player.CharVel[1] = -Player.CharVel[1]/2;
            
Player.400-Player.SizeY;
        }
        
//System.out.println("Velocity: x: "+TestingArea.CharVel[0]+" y: "+TestingArea.CharVel[1]);
        
Player.+= Player.CharVel[1];
        
//if (TestingArea.CharVel[1]<5 && TestingArea.CharVel[1] >-5){
        //    TestingArea.Y = TestingArea.Y-TestingArea.CharVel[1];
            //TestingArea.CharVel[1] = 0;
            
        //}
        
Player.+= 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..
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-07-2009, 04:01 PM
Senior Member
 
Join Date: Aug 2009
Posts: 179
Rep Power: 1
Addez is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-07-2009, 04:17 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-07-2009, 04:27 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
Default
A quick peek at your code reveals:
Code:
if (Player.mode == "Walking"){
Much better:
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 04:29 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-07-2009, 05:22 PM
Senior Member
 
Join Date: Aug 2009
Posts: 179
Rep Power: 1
Addez is on a distinguished road
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 =)
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-07-2009, 05:32 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Quote:
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-07-2009, 08:03 PM
Senior Member
 
Join Date: Aug 2009
Posts: 179
Rep Power: 1
Addez is on a distinguished road
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 ==
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-07-2009, 08:57 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
Default
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 ==
OK. What about getting the program logic out of the paint/paintComponent method?
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-07-2009, 10:06 PM
Senior Member
 
Join Date: Aug 2009
Posts: 179
Rep Power: 1
Addez is on a distinguished road
Default
ehh what u meen?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-07-2009, 10:10 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dll Call doesnt work INFACT New To Java 1 10-04-2009 10:31 PM
My blackjack games random generator doesnt work! Addez New To Java 16 08-17-2009 06:29 AM
java doesnt allow vista to work 10rosas New To Java 5 12-22-2008 05:23 PM
mouse click do not work after repaint nobody Java 2D 8 12-07-2008 05:43 PM
My program doesnt display anything Bojevnik AWT / Swing 2 10-19-2007 03:50 PM


All times are GMT +2. The time now is 03:00 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org