Calling (re)paint method

This is code of my program

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.util.*;

import java.io.*;

publicclass MyPanelextends JPanel{

int V , E;

Vector P =new Vector();

MyVector x =new MyVector();

MyVector y =new MyVector();

MyVector z =new MyVector();

public MyPanel(){

x.setX(1);

x.setY(0);

y.setX(0);

y.setY(1);

z.setX(1.41 / 2);

z.setY(-1.41 / 2);

// ucitava podatke iz filea

try{

DataInputStream in =new DataInputStream(

new BufferedInputStream(

new FileInputStream("cude4d.dat")));

V = Integer.parseInt(in.readLine());

double x , y , z;

for (int i = 0 ; i < V ; i++){

StringTokenizer st =new StringTokenizer(in.readLine() ," ");

x = Double.parseDouble(st.nextToken());

y = Double.parseDouble(st.nextToken());

z = Double.parseDouble(st.nextToken());

MyPoint v =new MyPoint();

v.setX(x);

v.setY(y);

v.setZ(z);

P.addElement(v);

}

E = Integer.parseInt(in.readLine());

int a , b;

for (int i = 0 ; i < E ; i++){

StringTokenizer st =new StringTokenizer(in.readLine() ," ");

a = Integer.parseInt(st.nextToken());

b = Integer.parseInt(st.nextToken());

a--;

b--;

((MyPoint)(P.elementAt(a))).addNeighbour(b);

((MyPoint)(P.elementAt(b))).addNeighbour(a);

}

in.close();

}

catch(Exception ex){

ex.printStackTrace();

}

}

publicvoid paint(Graphics g){

super.paint(g);

for (int i = 0 ; i < P.size() ; i++){

MyPoint b = (MyPoint)(P.elementAt(i));

g.drawOval((int)(b.getX() * x.getX() + b.getY() * y.getX() + b.getZ() * z.getX() + 100),

(int)(b.getX() * x.getY() + b.getY() * y.getY() + b.getZ() * z.getY() + 100), 2 , 2);

}

for (int i = 0 ; i < P.size() ; i++){

MyPoint p = (MyPoint)P.elementAt(i);

for (int j = 0 ; j < p.getSize() ; j++){

int idx = p.getNeighbour(j);

MyPoint a = (MyPoint)(P.elementAt(idx));

MyPoint b = (MyPoint)(P.elementAt(i));

g.drawLine((int)(b.getX() * x.getX() + b.getY() * y.getX() + b.getZ() * z.getX() + 100),

(int)(b.getX() * x.getY() + b.getY() * y.getY() + b.getZ() * z.getY() + 100) ,

(int)(a.getX() * x.getX() + a.getY() * y.getX() + a.getZ() * z.getX() + 100),

(int)(a.getX() * x.getY() + a.getY() * y.getY() + a.getZ() * z.getY() + 100));

}

}

try{

Thread.currentThread().sleep(50);

System.out.println("repaint");

}catch(Exception ex){

ex.printStackTrace();

}

}

publicvoid update(Graphics g){

paint(g);

}

publicvoid rotation(int type,double sAngle){

double rot[][] =newdouble[3][3];

double angle = 3.14 / 180;

for (int a = 0 ; a < sAngle ; a += 2){

if (type == 1){

rot[0][0] = 1; rot[0][1] = 0; rot[0][2] = 0;

rot[1][0] = 0; rot[1][1] = Math.cos(angle); rot[1][2] = -Math.sin(angle);

rot[2][0] = 0; rot[2][1] = Math.sin(angle); rot[2][2] = Math.cos(angle);

}

elseif(type == 2){

rot[0][0] = Math.cos(angle); rot[0][1] = 0; rot[0][2] = -Math.sin(angle);

rot[1][0] = 0; rot[1][1] = 1; rot[1][2] = 0;

rot[2][0] = Math.sin(angle); rot[2][1] = 0; rot[2][2] = Math.cos(angle);

}

else{

rot[0][0] = Math.cos(angle); rot[0][1] = -Math.sin(angle); rot[0][2] = 0;

rot[1][0] = Math.sin(angle); rot[1][1] = Math.cos(angle); rot[1][2] = 0;

rot[2][0] = 0; rot[2][1] = 0; rot[2][2] = 1;

}

for (int i = 0 ; i < P.size() ; i++){

MyPoint p = (MyPoint)P.elementAt(i);

double x = p.getX() , y = p.getY() , z = p.getZ();

p.setX(rot[0][0] * x + rot[0][1] * y + rot[0][2] * z);

p.setY(rot[1][0] * x + rot[1][1] * y + rot[1][2] * z);

p.setZ(rot[2][0] * x + rot[2][1] * y + rot[2][2] * z);

}

System.out.println("pre-repaint");

repaint();

}

}

}

On the screen there writes more "pre-repaint" then "repaint". What I should do that method paint() be called each time I call repaint?

Thank you.

P.S. If someone wants, I can send code of all classes. It is code for rotating cube.

[7677 byte] By [boba5555a] at [2007-11-26 13:48:34]
# 1

1) Don't override update()

2) Don't override paint()

3) Custom painting is done by overriding the paintComponent(..) method.

4) Never add a sleep() call in the painting methods. If you are trying to animate the image then you should be using a Timer to fire an event to tell the component to repaint itself.

> On the screen there writes more "pre-repaint" then "repaint".

repaint() requests are handled by the RepaintManager. It will potentially consolidate multiple paint requests into one to be more efficient.

You may want to consider doing your drawing on a BufferedImage, which may make it more efficient. Take a look at the DrawOnImage example from this posting:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=591408

camickra at 2007-7-8 1:24:40 > top of Java-index,Desktop,Core GUI APIs...
# 2
But what about flickering applet? Should not then I overide update() and paint()?Does 1) and 2) mean never or in this case?Can you help me with step 4? I do not know how to implement that and use fire action or ever what.Thank you.
boba5555a at 2007-7-8 1:24:40 > top of Java-index,Desktop,Core GUI APIs...
# 3

> But what about flickering applet? Should not then I overide update() and paint()?

Overriding update() and paint() are tricks for custom painting on AWT components.

Swing works differently. Custom painting on Swing components is done by overriding the paintComponent() method. By default Swing applications use double buffering so you shouldn't have a problem with flickering.

> Can you help me with step 4?

[url http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html]How to Use Timers[/url]. You can also search the forum for examples. I've posted a couple.

camickra at 2007-7-8 1:24:40 > top of Java-index,Desktop,Core GUI APIs...