Hi,
ich habe jetzt seit etwa einem halben Jahr Informatik in der 11 Klasse. Wir haben mit Java angefangen und arbeiten in BlueJ.
Jetzt haben wir angefangen einen Roboter zu programmieren, der mithilfe von Buttons was machen soll.
Ich fände es nett, wenn mal jemand über meinen Quelltext gucken würde und mit evtl. noch ein paar Tipps geben könnte, wie ich das besser schreiben könnte.
Danke schonmal.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Territorium extends JApplet implements ActionListener
{
Roboter robbi1;
Button btnLinks;
Button btnRechts;
Button btnGehen;
Button btnLaser;
public void init()
{
//Robbi
robbi1 = new Roboter(50,200);
//Links um
btnLinks = new Button("Links Um!");
getContentPane().setLayout(null);
btnLinks.setBounds(40,420,100,50);
getContentPane().add(btnLinks);
btnLinks.addActionListener(this);
//Rechts um
btnRechts = new Button ("Rechts Um!");
btnRechts.setBounds(150,420,100,50);
getContentPane().add(btnRechts);
btnRechts.addActionListener(this);
//Gehen
btnGehen = new Button ("Gehen!");
btnGehen.setBounds(260,420,100,50);
getContentPane().add(btnGehen);
btnGehen.addActionListener(this);
//Laserschwer einfahren
btnLaser = new Button ("Laser an/aus");
btnLaser.setBounds(370,420,100,50);
getContentPane().add(btnLaser);
btnLaser.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == btnLinks) robbi1.linksUm();
if (event.getSource() == btnRechts) robbi1.rechtsUm();
if (event.getSource() == btnGehen) robbi1.gehen();
if (event.getSource() == btnLaser) robbi1.laser();
repaint();
}
public void paint(Graphics g)
{
robbi1.zeigen(g);
}
}
import javax.swing.*;
import java.awt.*;
public class Roboter
{
int xPos, yPos, richtung, laser, a;
public Roboter(int x, int y)
{
xPos = x; yPos = y;
richtung = 1;
a = 30;
laser = 1;
}
public void gehen(){
if (xPos<=390){
if (richtung == 2) xPos=xPos+a;
}
if (xPos>=0) {
if (richtung ==4) xPos=xPos-a;
}
}
public void laser(){
if (laser == 2) laser = laser-1;
else
laser ++;
}
public void linksUm(){
richtung ++;
}
public void rechtsUm(){
richtung --;
}
public void zeigen(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0,0,500,500);
g.setColor(Color.BLACK);
if(richtung > 4)richtung =1 ;
else if (richtung < 1) richtung = 4;
if (richtung ==1) zeigenVorn(g);
else if (richtung ==2) zeigenLinks(g);
else if (richtung ==3) zeigenHinten(g);
else zeigenRechts(g);
}
public void zeigenVorn(Graphics g)
{
if (laser ==1){
g.setColor (Color.BLACK);
g.fillRect(xPos+40,yPos+30,40,40); //Kopf
g.fillRect(xPos+30,yPos+70,60,60); //Körper
g.fillRect(xPos+90,yPos+70,40,5); //Armr
g.fillRect(xPos+25,yPos+70,5,40); //Arml
g.fillRect(xPos+80,yPos+130,10,40); //BeinRechts
g.fillRect(xPos+30,yPos+130,10,40); //BeinLinks
g.setColor (Color.RED);
g.fillOval (xPos+50,yPos+42,5,5); //AugeLinks
g.fillOval (xPos+65,yPos+42,5,5); //AugeRechts
g.fillRect (xPos+130,yPos+10,5,70); //Schwert
g.setColor (Color.GRAY);
g.fillRect (xPos+130,yPos+60,5,20); //LAserschwerhalterung
g.setColor (Color.GREEN);
g.fillRect (xPos+50,yPos+58,20,2); //Le Mund
g.setColor (Color.ORANGE);
g.fillRect (xPos+10,yPos+20,100,10);
g.fillRect (xPos+30,yPos+10,60,10);
g.fillRect (xPos+40,yPos+0,40,10);
g.fillRect (xPos+50,yPos+-9,20,10);
g.setColor (Color.WHITE);
g.drawRect(xPos+40,yPos+30,40,40); //Kopf
g.drawRect(xPos+30,yPos+70,60,60); //Körper
g.drawRect(xPos+90,yPos+70,40,5); //Armr
g.drawRect(xPos+25,yPos+70,5,40); //Arml
g.drawRect(xPos+80,yPos+130,10,40); //BeinRechts
g.drawRect(xPos+30,yPos+130,10,40); //BeinLinks
g.drawOval (xPos+50,yPos+42,5,5); //AugeLinks
g.drawOval (xPos+65,yPos+42,5,5); //AugeRechts
g.drawRect (xPos+50,yPos+58,20,2); //Le Mund
g.drawRect (xPos+130,yPos+10,5,70); //Schwert
}
else
{
g.setColor (Color.BLACK);
g.fillRect(xPos+40,yPos+30,40,40); //Kopf
g.fillRect(xPos+30,yPos+70,60,60); //Körper
g.fillRect(xPos+90,yPos+70,40,5); //Armr
g.fillRect(xPos+25,yPos+70,5,40); //Arml
g.fillRect(xPos+80,yPos+130,10,40); //BeinRechts
g.fillRect(xPos+30,yPos+130,10,40); //BeinLinks
g.setColor (Color.RED);
g.fillOval (xPos+50,yPos+42,5,5); //AugeLinks
g.fillOval (xPos+65,yPos+42,5,5); //AugeRechts
g.setColor (Color.GRAY);
g.fillRect (xPos+130,yPos+60,5,20); //LAserschwerhalterung
g.setColor (Color.GREEN);
g.fillRect (xPos+50,yPos+58,20,2); //Le Mund
g.setColor (Color.ORANGE);
g.fillRect (xPos+10,yPos+20,100,10);
g.fillRect (xPos+30,yPos+10,60,10);
g.fillRect (xPos+40,yPos+0,40,10);
g.fillRect (xPos+50,yPos+-9,20,10);
g.setColor (Color.WHITE);
g.drawRect(xPos+40,yPos+30,40,40); //Kopf
g.drawRect(xPos+30,yPos+70,60,60); //Körper
g.drawRect(xPos+90,yPos+70,40,5); //Armr
g.drawRect(xPos+25,yPos+70,5,40); //Arml
g.drawRect(xPos+80,yPos+130,10,40); //BeinRechts
g.drawRect(xPos+30,yPos+130,10,40); //BeinLinks
g.drawOval (xPos+50,yPos+42,5,5); //AugeLinks
g.drawOval (xPos+65,yPos+42,5,5); //AugeRechts
g.drawRect (xPos+50,yPos+58,20,2); //Le Mund
}
}
public void zeigenLinks (Graphics g)
{
if (laser ==1)
{
g.setColor (Color.BLACK);
g.fillRect (xPos+35,yPos+30,20,40); //Kopf
g.fillRect (xPos+30,yPos+70,30,60); //Körper
g.fillRect (xPos+45,yPos+70,5,5); //Armr
g.fillRect (xPos+35,yPos+130,10,40); //Bein
g.setColor (Color.RED);
g.fillRect (xPos+45,yPos+10,5,70); //Schwert
g.setColor (Color.GRAY);
g.fillRect (xPos+45,yPos+60,5,20); //LAserschwerhalterung
g.setColor (Color.ORANGE);
g.fillRect (xPos-2,yPos+20,100,10);
g.fillRect (xPos+18,yPos+10,60,10);
g.fillRect (xPos+28,yPos+0,40,10);
g.fillRect (xPos+38,yPos+-9,20,10);
g.setColor (Color.WHITE);
g.drawRect (xPos+35,yPos+30,20,40); //Kopf
g.drawRect (xPos+30,yPos+70,30,60); //Körper
g.drawRect (xPos+45,yPos+70,5,5); //Armr
g.drawRect (xPos+35,yPos+130,10,40); //Bein
g.drawRect (xPos+45,yPos+10,5,70); //Schwert
g.setColor (Color.RED);
g.fillRect (xPos+45,yPos+10,5,70); //Schwert
g.setColor (Color.GRAY);
g.fillRect (xPos+45,yPos+60,5,20); //LAserschwerhalterung
}
else
{
g.setColor (Color.BLACK);
g.fillRect (xPos+35,yPos+30,20,40); //Kopf
g.fillRect (xPos+30,yPos+70,30,60); //Körper
g.fillRect (xPos+45,yPos+70,5,5); //Armr
g.fillRect (xPos+35,yPos+130,10,40); //Bein
g.setColor (Color.RED);
g.setColor (Color.GRAY);
g.fillRect (xPos+45,yPos+60,5,20); //LAserschwerhalterung
g.setColor (Color.ORANGE);
g.fillRect (xPos-2,yPos+20,100,10);
g.fillRect (xPos+18,yPos+10,60,10);
g.fillRect (xPos+28,yPos+0,40,10);
g.fillRect (xPos+38,yPos+-9,20,10);
g.setColor (Color.WHITE);
g.drawRect (xPos+35,yPos+30,20,40); //Kopf
g.drawRect (xPos+30,yPos+70,30,60); //Körper
g.drawRect (xPos+45,yPos+70,5,5); //Armr
g.drawRect (xPos+35,yPos+130,10,40); //Bein
g.setColor (Color.RED);
g.setColor (Color.GRAY);
g.fillRect (xPos+45,yPos+60,5,20); //LAserschwerhalterung
}
}
public void zeigenHinten(Graphics g)
{
if (laser ==1)
{
g.setColor (Color.BLACK);
g.fillRect(xPos+40,yPos+30,40,40); //Kopf
g.fillRect(xPos+30,yPos+70,60,60); //Körper
g.fillRect(xPos+90,yPos+70,5,40); //Armr
g.fillRect(xPos-10,yPos+70,40,5); //Arml
g.fillRect(xPos+80,yPos+130,10,40); //BeinRechts
g.fillRect(xPos+30,yPos+130,10,40); //BeinLinks
g.setColor (Color.RED);
g.fillRect (xPos-15,yPos+10,5,70); //Schwert
g.setColor (Color.GRAY);
g.fillRect (xPos-15,yPos+60,5,20); //LAserschwerhalterung
g.setColor (Color.ORANGE);
g.fillRect (xPos+10,yPos+20,100,10);
g.fillRect (xPos+30,yPos+10,60,10);
g.fillRect (xPos+40,yPos+0,40,10);
g.fillRect (xPos+50,yPos+-9,20,10);
g.setColor (Color.WHITE);
g.drawRect(xPos+40,yPos+30,40,40); //Kopf
g.drawRect(xPos+30,yPos+70,60,60); //Körper
g.drawRect(xPos+90,yPos+70,5,40); //Armr
g.drawRect(xPos-10,yPos+70,40,5); //Arml
g.drawRect(xPos+80,yPos+130,10,40); //BeinRechts
g.drawRect(xPos+30,yPos+130,10,40); //BeinLinks
g.drawRect (xPos-15,yPos+10,5,70); //Schwert
}
else
{
g.setColor (Color.BLACK);
g.fillRect(xPos+40,yPos+30,40,40); //Kopf
g.fillRect(xPos+30,yPos+70,60,60); //Körper
g.fillRect(xPos+90,yPos+70,5,40); //Armr
g.fillRect(xPos-10,yPos+70,40,5); //Arml
g.fillRect(xPos+80,yPos+130,10,40); //BeinRechts
g.fillRect(xPos+30,yPos+130,10,40); //BeinLinks
g.setColor (Color.RED);
g.setColor (Color.GRAY);
g.fillRect (xPos-15,yPos+60,5,20); //LAserschwerhalterung
g.setColor (Color.ORANGE);
g.fillRect (xPos+10,yPos+20,100,10);
g.fillRect (xPos+30,yPos+10,60,10);
g.fillRect (xPos+40,yPos+0,40,10);
g.fillRect (xPos+50,yPos+-9,20,10);
g.setColor (Color.WHITE);
g.drawRect(xPos+40,yPos+30,40,40); //Kopf
g.drawRect(xPos+30,yPos+70,60,60); //Körper
g.drawRect(xPos+90,yPos+70,5,40); //Armr
g.drawRect(xPos-10,yPos+70,40,5); //Arml
g.drawRect(xPos+80,yPos+130,10,40); //BeinRechts
g.drawRect(xPos+30,yPos+130,10,40); //BeinLinks
}
}
public void zeigenRechts(Graphics g)
{
g.setColor (Color.RED);
g.fillRect (xPos+45,yPos+20,5,70); //Schwert
g.setColor (Color.BLACK);
g.fillRect (xPos+35,yPos+30,20,40); //Kopf
g.fillRect (xPos+30,yPos+70,30,60); //Körper
g.fillRect (xPos+45,yPos+70,5,40); //Armr
g.fillRect (xPos+45,yPos+130,10,40); //Bein
g.setColor (Color.ORANGE);
g.fillRect (xPos-2,yPos+20,100,10);
g.fillRect (xPos+18,yPos+10,60,10);
g.fillRect (xPos+28,yPos+0,40,10);
g.fillRect (xPos+38,yPos+-9,20,10);
g.setColor (Color.WHITE);
g.drawRect (xPos+35,yPos+30,20,40); //Kopf
g.drawRect (xPos+30,yPos+70,30,60); //Körper
g.drawRect (xPos+45,yPos+70,5,40); //Armr
g.drawRect (xPos+45,yPos+130,10,40); //Bein
// g.drawRect (xPos+45,yPos+20,5,70); //Schwert
}
}
Wäre sehr nett