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
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