Hallo zusammen, ich muss für meine Schule ein Minigolf programmieren, Aber ich hab ein Problem, mein Knopf "Schuss" zeigt keinerlei Reaktion.
Wäre genjal wenn sich einer mal den Code anschauen könnte und mir einen Tipp geben könnte ( Bitte keine zu hohe Java Kentnisse und leicht erklärt, bin noch ein relativer Neuling. ( Den Code habe ich zu 99% aus dem Kopf programmiert...)
Wer sich in den Code reingearbeitet hat, und mit mir Reden möchte kann mich auch bei Skype adden, bin den Ganzen Tag noch online ( @dd firemozzi)
Ich habe Kommentare gemacht, damit man es evtl versteht... Jegliche Tipps wären supernützlich. Ich erwarte nicht von euch, dass ihr den Code überhaupt anschaut, es ist nämlich schon ein richtig dicker Schinken ^^
Danke Schonmal, Gruss Roman
Wäre genjal wenn sich einer mal den Code anschauen könnte und mir einen Tipp geben könnte ( Bitte keine zu hohe Java Kentnisse und leicht erklärt, bin noch ein relativer Neuling. ( Den Code habe ich zu 99% aus dem Kopf programmiert...)
Java:
// Roman Egger
// IPA GIBZ
// Minigolf V.1.0
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import java.math.*;
import java.math.*;
public class Ball extends Applet
implements ActionListener,MouseListener {
int width, height;
int mx, my; // the mouse coordinates
boolean isButtonPressed = false;
//Variablendefinition
private Button Schuss; //Knopf, für schiessen
public int x = 80, xV = 1; //x = Ballposition X, xV = Bewegung richtung X
public int y = 80, yV = 2; //y = Ballposition Y, YV = Bewegung richtung Y
public int i=0; // i = Eingelocht?, wenn ja 1
public int durchmesser = 10; // durchmesser des Balls
public int time=10; // Pausenzeit zwischen den Frames
public int timme=0; // Erweiterte, unter Pausenzeit, ( time+1 erhöht sich zu schnell)
public int n=1; // n = Anzahl Durchläufe
public int testxvxy=1; // Variable, ob der Test, dass die Bewegung nicht zu schnell ist, gelungen ist.
public int stop=0; // ob der ball angehalten ist?
private Graphics g; // Hauptgrafik
public int beweg=1; // Zwischenvariable für das bewegen des Balls
private float verhaeltnis; //
public int xkathete;
public int ykathete;
public int kgvx;
public int kgvy;
public int kgvyold;
public int kgvxold;
public int kgvtmp;
public int once = 1;
public int rechteckLinksX = 10, rechteckRechtsX = 100; // Definition, der Rechtecke
public int rechteckObenY = 10, rechteckUntenY = 100; //
//public int rechteckLinksX2 = 61, rechteckRechtsX2 = 140; //
//public int rechteckObenY2 = 101, rechteckUntenY2 = 180; //
public void mouseEntered( MouseEvent e ) {
}//Vielleicht werden die MouseEvents später gebraucht.
public void mouseExited( MouseEvent e ) {
}
public void mouseClicked( MouseEvent e ) {// Wenn geklickt, dann Positionen aufnehmen.
mx = e.getX();
my = e.getY();
}// Englische hilfe
public void mousePressed( MouseEvent e ) { // called after a button is pressed down
}
public void mouseReleased( MouseEvent e ) { // called after a button is released
}
public void mouseMoved( MouseEvent e ) { // called during motion when no buttons are down
}
public void mouseDragged( MouseEvent e ) { // called during motion with buttons down
}
public void init(){
Schuss = new Button("Schuss");
add(Schuss);
Schuss.addActionListener(this);
addMouseListener(this);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == Schuss){
if (stop==1){//Wenn der Ball stillsteht
stop=0;
if (x > mx){
xkathete=x-mx;
}
if (x < mx){
xkathete=mx-x;
}
if (y > my){
ykathete=y-my;
}
if (y < my){
ykathete=my-y;
}
kgvx=x;
kgvy=y;
kgvxold=x;
kgvyold=y;
do {
kgvtmp = kgvx % kgvy;
kgvx = kgvy;
kgvy = kgvtmp;
} while (kgvtmp != 0);
if (xkathete > ykathete){
verhaeltnis=xkathete/ykathete;
verhaeltnis = Math.round(verhaeltnis);
xkathete =((kgvxold/kgvx)*kgvy);
xV = xkathete;
ykathete = xkathete*(int)verhaeltnis;
yV = ykathete;
}
else
{
verhaeltnis=ykathete/xkathete;
verhaeltnis = Math.round(verhaeltnis);
ykathete =((kgvxold/kgvx)*kgvy);
ykathete =((kgvxold/kgvx)*kgvy);
yV = ykathete;
xkathete = ykathete*(int)verhaeltnis;
xV = xkathete;
}
paint(g);
}
stop = 0;
i = 0;
n = 1;
time = 10;
timme = 0;
testxvxy=1;
}
}
public void paint(Graphics g){
g.setColor(Color.green);
g.fillRect(rechteckLinksX, rechteckObenY,rechteckRechtsX - rechteckLinksX,rechteckUntenY - rechteckObenY);//Rechteck 1 Zeichnen
//g.fillRect(rechteckLinksX2, rechteckObenY2,rechteckRechtsX2 - rechteckLinksX2,rechteckUntenY2 - rechteckObenY2);// Rechteck 2 Zeichnen
setBackground(Color.gray);
g.setColor(Color.black);
if(mx!=0 & my !=0){
g.drawLine(x+5,y+5,mx,my);
repaint();
Color hintergrundFarbe = getBackground();
g.setColor(Color.white);
g.fillOval(x,y,durchmesser,durchmesser);
}
n = 1;
for (int n = 1; n<1000000; n++) {
if(i==0){
Color hintergrundFarbe = getBackground();
g.setColor(Color.white);
g.fillOval(x,y,durchmesser,durchmesser);
}
if (x>45&&x<55&&y>45&&y<55||i==1){ // Wenn der Ball, im Bereich des Loches ist, dann Alles anhalten
g.setColor(Color.BLUE);
g.drawString("EINGELOCHT", 50, 80);
i=1;
n=1000001;
xV=0;
yV=0;
}
else{
if(xV<0){
for(int beweg=0; beweg>xV; beweg--){
if(x > rechteckLinksX+1){
x = x - 1;
}
else {
xV = -xV;
beweg=xV;
}
}
}
else{
for(int beweg=0; beweg<xV; beweg++){
if(x < rechteckRechtsX-10){
x = x + 1;
}
else {
xV = -xV;
beweg=xV;
}
}
}
if(yV<0){
for(int beweg=0; beweg>yV; beweg--){
if(y > rechteckObenY+1){
y = y - 1;
}
else {
yV = -yV;
beweg=yV;
}
}
}
else{
for(int beweg=0; beweg<yV; beweg++){
if(y < rechteckUntenY-10){
y = y + 1;
}
else {
yV = -yV;
beweg=yV;
}
}
}
n=1000001;
}
g.setColor(Color.lightGray);
g.fillOval(x,y,durchmesser,durchmesser);
g.setColor(Color.black);
g.fillOval(50,50,10,10);
repaint();
try {
Thread.currentThread().sleep(time);
}catch(InterruptedException e){};
timme=timme+time;
if(xV!=0&&yV!=0){
if(timme>=20){
timme=1;
time= time+1;
}
}
if(time>100){
stop=1;
time=1;
xV=0;
yV=0;
}
repaint();
}
}
public void update(Graphics g)
{
g.fillOval(x,y,durchmesser,durchmesser);
g.setColor(Color.black);
g.fillOval(50,50,10,10);
repaint();
paint(g);
repaint();
}
}
Wer sich in den Code reingearbeitet hat, und mit mir Reden möchte kann mich auch bei Skype adden, bin den Ganzen Tag noch online ( @dd firemozzi)
Ich habe Kommentare gemacht, damit man es evtl versteht... Jegliche Tipps wären supernützlich. Ich erwarte nicht von euch, dass ihr den Code überhaupt anschaut, es ist nämlich schon ein richtig dicker Schinken ^^
Danke Schonmal, Gruss Roman
Zuletzt bearbeitet: