Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-04-2010, 04:43 PM   PM User | #1
yngwievanhendri
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
yngwievanhendri is an unknown quantity at this point
problem with Threads

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
Attached Files
File Type: zip threadtest2.zip (17.0 KB, 48 views)

Last edited by yngwievanhendri; 04-04-2010 at 04:55 PM..
yngwievanhendri is offline   Reply With Quote
Old 04-04-2010, 10:43 PM   PM User | #2
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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.
surreal5335 is offline   Reply With Quote
Old 04-07-2010, 07:45 PM   PM User | #3
yngwievanhendri
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
yngwievanhendri is an unknown quantity at this point
surreal5335 Thanks for the tipp. So here is the code for

RectTest.java
Code:
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


Code:
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
yngwievanhendri is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 04:06 AM.


Advertisement
Log in to turn off these ads.