yngwievanhendri
04-04-2010, 04:43 PM
Hello everyone , im new to this forum and to Java, i started learning it couple of weeks ago and i have a problem with Thread-Class.
i want to create a simple application("Main") that exteds Frame. in this Application i want to call a class("RectTest") that exteds Component and implements Runnable. i'Ve overriden all the methods (run() and paint()) .. the thing is when i start my application("Main") and create a "RectTest" class the Thread runs but the Graphics dont get updated.
can anyone help???
i managed to get it work when the "RectTest" in the "Main" implemented. but i dont want that i want separeted classes.
P.S. i forgot to uncomment lines (21,34,35)
Thanks
surreal5335
04-04-2010, 10:43 PM
It's best to just post the code on the thread here. You will get a lot more responses bc no one will have to download anything to view it.
yngwievanhendri
04-07-2010, 07:45 PM
surreal5335 Thanks for the tipp. So here is the code for
RectTest.java
package threadtest2;
import java.awt.Component;
import java.awt.Graphics;
/**
*
* @author a
*/
public class RectTest extends Component implements Runnable {
APunkt oL;
APunkt oR;
APunkt uL;
APunkt uR;
APunkt center;
Thread thread;
boolean runOK;
int sleepTime;
public RectTest(APunkt oL,APunkt oR, APunkt uL, APunkt uR){
sleepTime = 100;
this.oL = oL;
this.oR = oR;
this.uL = uL;
this.uR = uR;
center = new APunkt();
center = calcCenter();
runOK = true;
thread = new Thread(this);
thread.start();
}
public APunkt calcCenter() {
int tX = (oL.getX()+oR.getX())/2;
int tY = (oL.getY()+uL.getY())/2;
this.center.setX(tX);
this.center.setY(tY);
return this.center;
}
public APunkt moveStepXY(int x,int y){
this.oL.moveStepX(x);
this.oR.moveStepX(x);
this.uL.moveStepX(x);
this.uR.moveStepX(x);
this.oL.moveStepY(y);
this.oR.moveStepY(y);
this.uL.moveStepY(y);
this.uR.moveStepY(y);
this.center = calcCenter();
return this.center;
}
public APunkt moveStepY(int step) {
this.oL.moveStepY(step);
this.oR.moveStepY(step);
this.uL.moveStepY(step);
this.uR.moveStepY(step);
this.center = calcCenter();
return this.center;
}
public APunkt moveStepX(int step) {
this.oL.moveStepX(step);
this.oR.moveStepX(step);
this.uL.moveStepX(step);
this.uR.moveStepX(step);
this.center = calcCenter();
return this.center;
}
public void run() {
int counter = 0;
while(runOK) {
//counter++;
moveStepX(4);
try {
Thread.sleep(sleepTime);
}
catch(Exception e) {
}
if (counter < 20){
moveStepX(4);
}
if ((counter > 20) && (counter < 30)) {
moveStepY(4);
}
if ((counter > 30) && (counter < 40)) {
moveStepXY(4,2);
}
if (counter > 40 ) {
runOK = false;
System.exit(1);
}
counter++;
//System.out.println(counter);
this.center.show();
repaint();
}
}
@Override
public void paint(Graphics g) {
// oL ->> oR
g.drawLine(oL.getX(),oL.getY(),oR.getX(),oR.getY());
// oR ->> uR
g.drawLine(oR.getX(),oR.getY(),uR.getX(),uR.getY());
// uR ->> uL
g.drawLine(uR.getX(),uR.getY(),uL.getX(),uL.getY());
// uL ->> oL
g.drawLine(uL.getX(),uL.getY(),oL.getX(),oL.getY());
this.center.show();
}
}
and for Main.java
package threadtest2;
import java.awt.*;
/**
*
* @author a
*/
public class Main extends Frame {
public Main(){
//Frame wnd = new Frame("f");
super("test");
int side = 600;
//Frame wnd = new Frame("Test");
setSize(side,side);
setBackground(Color.lightGray);
setVisible(true);
}
public static void main(String[] args) {
//super("Test");
Main wnd = new Main();
APunkt oL = new APunkt(200,200);
APunkt oR = new APunkt(300,200);
APunkt uL = new APunkt(200,100);
APunkt uR = new APunkt(200,200);
RectTest r = new RectTest(oL,oR,uL,uR);
}
}
if anyone could help i will be very gratefull. Thanks
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.