-1

enter image description hereI am drawing some signs in my JPanel(hh:mm -> time), but when I update it and call repaint it covers old letters(they don't dissapear). How to fix this?

tenorsax
  • 21,123
  • 9
  • 60
  • 107
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • Erase them? Put them in a component of their own? – Dave Newton Aug 18 '12 at 02:07
  • did you tried to call revalidate? – TeaCupApp Aug 18 '12 at 02:07
  • possible duplicate of [Graphics.drawString() Draws Over My Old String](http://stackoverflow.com/questions/5842360/graphics-drawstring-draws-over-my-old-string) – Yoda Aug 18 '12 at 02:36
  • 2
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Aug 18 '12 at 02:37
  • @RobertKilar was the image left out in purpose? If so please roll back my edit. – tenorsax Aug 18 '12 at 02:52
  • 2
    This will come down to "how" you are painting them. For instance, if you're using any sort of buffering or layerd painting approaches (paint sections in layers and getting out of sync). You could check out http://stackoverflow.com/questions/11691496/java-transparency-rendering-error/11696916#11696916 and see if it helps – MadProgrammer Aug 18 '12 at 03:05
  • 2
    Thanks for trying to make us guess at what your code is doing. This is usually due to not calling the super paint or paintComponent method, but how the h3ll should we know if you don't show the relevant code?? – Hovercraft Full Of Eels Aug 18 '12 at 03:32

1 Answers1

2

After removing previous lines Call,

revalidate();

then

repaint();

Very Quick code

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestComponent extends JPanel {

    private String drawThis;

    public TestComponent() {
        this.drawThis = "Hello";
        JButton button = new JButton("Change");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                setDrawThis("World");
                repaint();
            }
        }); 
        this.add(button);
    }
    private void drawString(Graphics g, String text, int x, int y) {
        for (String line : text.split("\n"))
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawString(g, drawThis, 20, 20);
    }

    public void setDrawThis(String s) {
        this.drawThis = s;
    }

    public static void main(String s[]) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TestComponent tc = new TestComponent();

        f.add(tc);
        f.setSize(220, 220);
        f.setVisible(true);
    }
}
TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • public void aktualizuj(int nrPrzystanku){ this.nrPrzystanku = nrPrzystanku; revalidate(); repaint(); doesn not work. That's what I want to do, remove them and put new ones. } – Yoda Aug 18 '12 at 02:12
  • remove old lines first then add new lines and then revalidate and repaint. – TeaCupApp Aug 18 '12 at 02:13
  • That's what I am asking how to do? I don't want new Strings cover old ones. – Yoda Aug 18 '12 at 02:13
  • Just try doing what you have sent me in comment, and it will be helpful if you will add your paint code inside your question – TeaCupApp Aug 18 '12 at 02:14
  • Man. That's what I am asking about. How to remove old ones and put new. – Yoda Aug 18 '12 at 02:16
  • 1
    Ok it seems like the question is duplicated : http://stackoverflow.com/questions/5842360/graphics-drawstring-draws-over-my-old-string – TeaCupApp Aug 18 '12 at 02:19
  • I got super.paintComponents(g); instead of super.paintComponent(g); – Yoda Aug 18 '12 at 02:21
  • @RobertKilar, check the quick code written above as it works perfectly. I asked you to call revalidate in order clear the dirty regions of your Panel. Else you can use clearRect method. – TeaCupApp Aug 18 '12 at 02:49
  • 2
    `super.paintComponent(g)` as the first line inside the overridden **paintComponent(...)** method, seems to me will be enough to remove all the previous drawings and adding a new one. – nIcE cOw Aug 18 '12 at 03:00