Hello everyone...

I have just a simple project where i have to draw a picture (Robot) and on a klick on a button, the Robot turns to the left.

My problem is, the Robot turns correctly but the new Robot drawing (position showing the Robot from the left side, back or right side) is drawn on top of the previews drawing.

In other words, the new drawing of the Robot should destroy the previews drawing of the robot.

My codes are here:

Robot Class:

import java.awt.*;

public class Roboter
{
    int xPos, yPos;
    int richtung = 1;


    public Roboter(int x, int y, int r)
    {
            xPos = x; yPos = y;
            richtung = r;
    }

    public void linksUm()
    {
        richtung++;
        if (richtung > 4) richtung = 1;

    }

    public void zeigen(Graphics g)
    {
        if (richtung == 1) zeigenVorn(g);
        else if (richtung == 2) zeigenRechts(g);
        else if (richtung == 3) zeigenHinten(g);
        else if (richtung == 4) zeigenLinks(g);
    }



    public void zeigenVorn(Graphics g)
    {
        g.drawRect( xPos, yPos,50,50); // Kopf
        g.drawRect( xPos+10,yPos+ 50,30,30); // Hals
        g.drawRect( xPos-20,yPos+ 80,90,90); // Rumpf
        g.drawRect( xPos-10,yPos+170,20,40); // linkes Bein
        g.drawRect( xPos+40,yPos+170,20,40); // rechtes Bein
        g.drawRect( xPos-40,yPos+ 80,20,60); // linker Arm
        g.drawRect( xPos+70,yPos+ 80,20,60); // rechter Arm
        g.drawOval( xPos+10,yPos+ 10,14,14); // linkes Auge
        g.drawOval( xPos+26,yPos+ 10,14,14); // rechtes Auge
        g.drawOval( xPos+10,yPos+ 35,30, 6); // Mund
    }

    public void zeigenHinten(Graphics g)
    {
        g.drawRect( xPos, yPos,50,50); // Kopf
        g.drawRect( xPos+10,yPos+ 50,30,30); // Hals
        g.drawRect( xPos-20,yPos+ 80,90,90); // Rumpf
        g.drawRect( xPos-10,yPos+170,20,40); // linkes Bein
        g.drawRect( xPos+40,yPos+170,20,40); // rechtes Bein
        g.drawRect( xPos-40,yPos+ 80,20,60); // linker Arm
        g.drawRect( xPos+70,yPos+ 80,20,60); // rechter Arm
        g.drawOval( xPos+10,yPos+130,30,30); // Steckdose
        g.drawOval( xPos+13,yPos+140,10,10); // Steckdose
        g.drawOval( xPos+27,yPos+140,10,10); // Steckdose
    }

    public void zeigenLinks(Graphics g)
    {
        g.drawRect(xPos ,yPos, 30,50); // Kopf
        g.drawRect(xPos+ 5,yPos+ 50,20,30); // Hals
        g.drawRect(xPos-10,yPos+ 80,50,90); // Rumpf
        g.drawRect(xPos+ 5,yPos+170,20,40); // Beine
        g.drawRect(xPos, yPos+ 80,20,60); // linker Arm
        g.drawRect(xPos+30,yPos+ 10, 5,14); // Augen
    }

    public void zeigenRechts(Graphics g)
    {
        g.drawRect(xPos ,yPos, 30,50); // Kopf
        g.drawRect(xPos+ 5,yPos+ 50,20,30); // Hals
        g.drawRect(xPos-10,yPos+ 80,50,90); // Rumpf
        g.drawRect(xPos+ 5,yPos+170,20,40); // Beine
        g.drawRect(xPos+10,yPos+ 80,20,60); // rechter Arm
        g.drawRect(xPos- 5,yPos+ 10, 5,14); // Augen
    }




}

Now my Terrain Class:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;  
import javax.swing.JPanel;



public class Territorium extends JApplet implements ActionListener




{
    Roboter robbi1, robbi2;

    Button btnLinks;


    public void init()
    {
            robbi1 = new Roboter(50, 100, 1);


            btnLinks = new Button("Links Um");

            getContentPane().setLayout(null);

            getContentPane().add(btnLinks);
            btnLinks.setBounds(140,420,100,50);

            btnLinks.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() == btnLinks)
        {
            robbi1.linksUm();

            System.out.println("repaint");
            repaint();

        }
    }




    public void paint(Graphics g)
    {
        robbi1.zeigen(g);
    }
}

I was thinking to maybe draw the terrain new after each new drawing of the Robot? But i don´t know how. Or maybe there is another method to do it.

Thank you for taking your time for helping me out here.

Recommended Answers

All 4 Replies

Well, you can either call g.setColor(), followed by g.fillRect() to redraw the background. Or you can simply use g.clearRect() to redraw the background colour.

You havr a nunber of options - redraw ther background is one, put the robot in it's own swing component is another. This tutorial is essential reading, not too long, and will give you the info you need.

So i will just draw over the previous drawings? I think it is not very good for your memory :)

Isn´t there any chance to refresh the background before a new Robot is drawn?

Thank you JamesCherrill... I will go through this tutorial.

Thanks everyone...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.