[Java] Applet -> Application

Nava

Cadet 2nd Year
Registriert
März 2005
Beiträge
26
Hallo!

Ich habe vor kurzem angefangen mich mit Java zu beschäftigen :freak: ;

Jedenfalls will ich ein Applet mit Thread (ganz normales Applet mit nem Auto das von links nach rechts fährt) zu einer Application machen und zwar in ein Frame. ich hab den applet text jetzt in ein leeres Frame eingebaut, aber der Compiler kommt mit dem Thread nich klar irgendwie.

Fehlermeldung:
C:\java>javac Frame1.java
Frame1.java:43: cannot resolve symbol
symbol : constructor Thread (Frame1)
location: class java.lang.Thread
thr = new Thread (this);
-------^------------------------------(die linien sind nur damit das ^ unterm n is)
Note: Frame1.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error


Muss ich da erst irgendeine Java platform package draufschieben oder so?

Das mit dem Thread sieht im script so aus:

Code:
 public void start () 
	{
	thr = new Thread (this);
	t = 0;
	thr.start();
	}

Oder muss ich noch ne eigene class für den Thread machn?

Mfg Nava
 
Zuletzt bearbeitet:
Einfach mal ganz genau die Fehlermeldung betrachten.

Du musst die Variable thr erstmal deklarieren, bevor Du sie zuweist, also

Thread thr = new Thread (this);


HTH


Eniac
 
Code:
 public void start () 
	{
	Thread thr = new Thread (this);
	t = 0;
	thr.start();
	}

Geht immer noch net... gleicher fehler

Sry wenn ichs nich peile aber wie gesagt, java is momentan noch :pcangry:

Außerdem hab ich thr schon deklariert gehabt:
Code:
public class Frame1 extends java.awt.Frame{

  final int breite = 600, hoehe = 400;
    Thread thr;
    double t;
    Image offscreen;
    Graphics g1, g2;
 
Zuletzt bearbeitet:
> Geht immer noch net... gleicher fehler

Versuchs mal so:

Code:
import java.awt.*;

public class Frame1 extends Frame implements Runnable
{
	final int breite = 600, hoehe = 400;
    Thread thr;
    double t;
    Image offscreen;
    Graphics g1, g2;
    
    public void run()
	{
		thr = new Thread (this);
		t = 0;
		thr.start();
	}
}


Das sollte sich ohne weiteres kompilieren lassen.


Eniac
 
Ok danke geht jetzt.
Wie konnte ich nur das implements Runnable vergessen?!
Ich hasse das wenn mir der compiler sagt "Fehler in Zeile 100", obwohl ich in Zeile 50 irgendeine kleinigkeit vergessen hab oder so... :grr:

Naja das Auto fährt grad noch nich wirklich aber das müsste ich hinbekommen... ;)

MfG Nava
 
hm irgendwie bekomm ich das mit dem Bug net hin...

hier is mal der ganze Text:


Code:
import java.awt.*;

public class Frame1 extends Frame implements Runnable
{
	final int breite = 600, hoehe = 400;			
	Thread thr;
	double t;
	Image offscreen;
	Graphics g1, g2;

  public static void main(String[] args)
	{
		Frame f1 = new Frame1();		
		f1.setSize(600,400);				
		f1.show();
	}
  public void init ()
	{
	g1 = getGraphics();
	offscreen = createImage (breite, hoehe);
	g2 = offscreen.getGraphics();
    	}
   public void start ()
	{
		thr = new Thread (this);
		t = 0;
		thr.start();
	}

   public void stop ()
	{
		thr.stop();
		thr = null;
	}

   public void run ()
	{
		long t0, t1;
		t0 = System.currentTimeMillis();
		while (true) 
			{
				paint(g2);
				g1.drawImage(offscreen,0,0,this);
				t1 = System.currentTimeMillis();
				t += (t1-t0)/1000.0;
				t0 = t1;
			}
	}

public void paint (Graphics g)
	{
		int x = hoehe - 100;
		int z = 500;

		g.setColor (Color.blue);
		g.fillRect (0,0,breite,hoehe-100);

		x = (int)Math.round(20*t);
		z = (int)Math.round(300+((20*t)*-1));
		if (z <= 50) z = 100+(z*-1);

		g.setColor(Color.yellow);
		g.fillOval(x,z,50,50);

		g.setColor(Color.black);
		g.drawLine(0,300,breite,300);
		g.setColor (Color.gray);
		g.fillRect(0,300,breite,hoehe-300);
		g.setColor (Color.white);
		g.fillRect (0,340,60,20);
		g.fillRect (100,340,60,20);
		g.fillRect (200,340,60,20);
		g.fillRect (300,340,60,20);
		g.fillRect (400,340,60,20);
		g.fillRect (500,340,60,20);

		g.setColor(Color.red);
		g.fillRect(x-50,330,100,50);
		g.setColor(Color.red);
		g.fillRect(x+50,350,26,30);

		g.setColor(Color.black);
		g.fillOval(x-40,350,40,40);

		g.setColor(Color.black);
		g.fillOval(x+40,360,30,30);
	}

	  public Frame1(String Titel)
	{
		super(Titel);
  	}

  public boolean handleEvent(Event e)
	{
		if (e.id == Event.WINDOW_DESTROY)
    		System.exit(0);
    		return super.handleEvent(e);
  	}

  public Frame1()
	{
		super("Auto");
	}


}

ich habs mal so gemacht, dass ich unter der paint, init usw mothode system.out.printl gemacht hab und es wurde nur bei void paint und main was ausgegeben...

Ich hab echt keine ahnung was da net geht jedenfalls bewegt sich das Auto keinen millimeter...


Mfg Nava
 
Zurück
Oben