problem z rysowaniem

0

Witam "programuje" w jawie dopiero od 2 dni i mam taki problem

robie aplet 600x400 w ktorym jest obrazek o polozeniu x=300, y=200
i po kliknieciu lewym y++; prawym y--;

i wszystko dziala ok tylko stara pozycja obrazka pozostaje i sie nakladaja na siebie

kod


public class operacje extends JPanel implements MouseListener{
    Image ludzik;
    int x=300,y=200;
    public operacje(){
        setBackground(new Color(255,255,255));
        setPreferredSize(new Dimension(600,400));

        try{
            ludzik =new ImageIcon(this.getClass().getResource("gam/lud.png")).getImage();
        }catch(Exception e){ System.out.println("Nie można wczytać obrazu");}

        addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2=(Graphics2D)g;

        g2.drawImage(ludzik,x,y,null);
    }
    
    public void mousePressed(MouseEvent me){
        int xme=me.getX();
        int yme=me.getY();
        int butt=me.getButton();
       // /*
        if(butt == 1)y++;
        else if(butt == 2){x--; y--;}
        else if(butt == 3)y--;
       /**/

        repaint();
    }
   /*
    public void mouseReleased(MouseEvent me){      
        int butt=me.getButton();
        if(butt == 1){y+=10;}
        else if(butt == 3){y-=10;}
       repaint();
    }
    /**/
    public void mouseEntered(MouseEvent me){}
    public void mouseExited(MouseEvent me){}
    //public void mousePressed(MouseEvent me){}
    public void mouseReleased(MouseEvent me){}
    public void mouseClicked(MouseEvent me) {}

}

0

Dodaj "super.paintComponent(g);" jako pierwszą linię metody paintComponent(Graphics g).

To powinno pomóc, jeżeli nie to użyj metody clearRect(int x, int y, int width, int height) klasy Graphics2D.

0

dzieki __krzysiek85 super.paintComponent(g); pomoglo :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1