Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Roboter in BlueJ

Empfohlene Antworten

Veröffentlicht

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

  • 4 Wochen später...

Hi,

dein Code ist sehr übersichtlich, was immer gut ist! Aber zu verbessern gibt es schließlich immer etwas:

1. versuche deine Klassen-Variablen immer private und am besten noch mit final zu machen. Das hat den Vorteil, dass die Variablen nach der Definition nicht mehr verändert werden können. Weder ausversehen noch sonst wie. ;)

2. Wenn du Componenten zum Applet hinzufügen willst, musst du nicht zwingend getContentPane().add(...) schreiben, sondern kannst auch direkt add(...) machen

3. du könntest, um die Sache noch ein bischen abzurunden das g.fillRect... und g.drawRect... in jeweils eine Methode auslagern. (ist nicht zwingend notwendig, aber du kannst dir ein paar (ca. 80) mal das xPos+..., yPos+... sparen :))

hier mal eine kurze Codevorstellung von meiner Seite:


[U]zu 1.:[/U]

private final Roboter robbil;

private final Button btnLinks;

private final Button btnRechts;


[U]zu 2.:[/U]

public void init()

        {

                ...

                setLayout(null);

                ...

                add(btnLinks);

                ...

         }


[U]zu 3.:[/U]

private void drawRect(Graphics g, int width, int height, int x, int y)

{

    g.drawRect(xPos+width,yPos+height,x,y);

}

...

das gleiche für fillRect...

Ich hoffe ich konnte dir ein paar nützliche und hilfreiche Tipps geben.

Sporticus

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.