
11-07-2009, 01:37 AM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 179
Rep Power: 1
|
|
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 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..
|
|

11-07-2009, 04:01 PM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 179
Rep Power: 1
|
|
|
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?
|
|

11-07-2009, 04:17 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
|
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
|
|

11-07-2009, 04:27 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
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.
|
|

11-07-2009, 05:22 PM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 179
Rep Power: 1
|
|
|
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 =)
|
|

11-07-2009, 05:32 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
Originally Posted by Addez
|
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
|
|

11-07-2009, 08:03 PM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 179
Rep Power: 1
|
|
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 ==
|
|

11-07-2009, 08:57 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
Originally Posted by Addez
|
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
|
|

11-07-2009, 10:06 PM
|
|
Senior Member
|
|
Join Date: Aug 2009
Posts: 179
Rep Power: 1
|
|
|
ehh what u meen?
|
|

11-07-2009, 10:10 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,393
Rep Power: 8
|
|
|
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 03:00 PM.
|
|