We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,987 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Repaint method does not destroy previews drawing

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.

3
Contributors
4
Replies
1 Hour
Discussion Span
8 Months Ago
Last Updated
5
Views
Question
Answered
HelloMe
Junior Poster in Training
90 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

nmaillet
Posting Pro
537 posts since Aug 2008
Reputation Points: 111
Solved Threads: 103
Skill Endorsements: 4

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.

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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?

HelloMe
Junior Poster in Training
90 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

Thanks everyone...

HelloMe
Junior Poster in Training
90 posts since Sep 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 8 Months Ago by JamesCherrill and nmaillet

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0749 seconds using 2.78MB