Von JFrame zu Applet

iCox87

Mitglied
Thread Starter
Dabei seit
10.08.2006
Beiträge
298
Reaktionspunkte
5
Hi Leute...

Bin noch Java Anfänger...Ich und mein Partner ( Banknachbar) sollen "Das Haus vom Nikolaus" als Applet zum laufen bringen, unser Problem besteht darin das zu schreiben, haben desshalb mit Hilfe des Internets und des Lehrers einiges hinbekommen. Nur das Problem, dass das Material ( aus dem Internet ) für jFrames ist, weiss erstens nicht wirklich was das ist un weiss auch nicht wie wir das nun umschreiben sollen zu einem Applet.

Edit: Im moment läuft es ja super nur zu einem Applet zu machen geht nicht.

Ich hoffe ihr wisst was ich meine, hier der Quelltext:

import javax.swing.*;
import java.awt.*;

public class HalloWelt extends JFrame {

private boolean ani = false;

public HalloWelt() {

setSize(300, 400);
setVisible(true);
}

public void paint(Graphics g) {

super.paint(g);
if (ani) {
Graphics2D g2d = (Graphics2D)g;
paintStrich(250, 250, 350, 150, g2d);
paintStrich(250, 50, 150, 150, g2d);
paintStrich(50, 50, 150, 350, g2d);
paintStrich(50, 250, 350, 350, g2d);
paintStrich(250, 50, 350, 150, g2d);
paintStrich(50, 150, 150, 50, g2d);
paintStrich(150, 250, 50, 150, g2d);
paintStrich(250, 50, 150, 350, g2d);
ani = false;
}
}

private void paintStrich(int x1, int x2, int y1, int y2, Graphics2D g) {

if (x1 == x2) {
paintY(x1, y1, y2, g);
}
else if (y1 == y2) {
paintX(x1, x2, y1, g);
}
else {
paintDiagonal(x1, x2, y1, y2, g);
}
}

private void paintDiagonal(int x1, int x2, int y1, int y2, Graphics2D g) {

if (x1 < x2 && y1 < y2) {
for (int x = x1, y = y1; x < x2; x++, y++) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else if (x1 > x2 && y1 > y2){
for (int x = x1, y = y1; x > x2; x--, y--) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else if (x1 < x2 && y1 > y2){
for (int x = x1, y = y1; x < x2; x++, y--) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else if (x1 > x2 && y1 < y2){
for (int x = x1, y = y1; x > x2; x--, y++) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

private void paintY(int x1, int y1, int y2, Graphics2D g) {

if (y1 < y2) {
for (int i = y1; i < y2; i++) {
g.drawLine(x1, i, x1, i);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else {
for (int i = y1; i > y2; i--) {
g.drawLine(x1, i, x1, i);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

private void paintX(int x1, int x2, int y1, Graphics2D g) {

if (x1 < x2) {
for (int i = x1; i < x2; i++) {
g.drawLine(i, y1, i, y1);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
else {
for (int i = x1; i > x2; i--) {
g.drawLine(i, y1, i, y1);
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public void drawHouse() {
ani = true;
repaint();
}

public static void main(String[] args) {

new HalloWelt().drawHouse();
}
}

Danke im Voraus:cool:
 
ihr schafft es doch auch die klasse jframe zu erweitern...
warum könnt ihr das denn nicht mit der klasse applet? ;)

google doch einfach mal nach einem java applet tutorial...
 
Weil wir nicht wirklich wissen wie das geht, haben aber rausgefunden, dass wir der Anfang so schreiben müssen:

import javax.swing.*;
import java.awt.*;


public class HalloWelt extends JApplet >> und nicht JFrame

wenn wir aber ausführen passiert nix, öffnet zwar eine neues Fenster jedoch geschieht nix:eek:
 
Wenn du aus "extends JFrame" "extends JApplet" machst und ani gleich auf true setzt sollte das eigentlich schon klappen.
 
Also eine kalsse extends Applet machen.

Im applet überschreist du dann ein paar methonen:

public void init(){} //WIrd beim laden des applets ausgeführt

public void start(){}//wird beim starten ausgeführt

public void paint(Graphics g){} //die Paint Mehtode mit der du auf das Applet zeichnest

wenn du da deine eigenen methoden noch einfügst sollte das kein problem mehr sein


mfg
 
welche muss ich denn genau überschreiben, sind ja einige Methoden:eek:
 
ich überschreibe eigentlich immer alle, spielt keine rolle

bei der init kannst noch setBackground und setsSize für farbe und grösse machen
 
also irgendwie verstehe ich das nicht, es ist zwar leicht gesagt aber ich kann es nicht, habe schon einige freunde gefragt die sagen mir ich muss noh Panel einbauen ( weiss nicht mal was das ist ) außerdem is der Quelltext sowas von scheisse gecoded:eek: :confused:
 
Zurück
Oben Unten