Jump to content
chuckyegg2

super.paintComponent() não funciona como esperado

Recommended Posts

chuckyegg2

Boa tarde a todos,

Estou a tentar implementar algo muito simples: uma forma geométrica a mover-se.

No entanto, não estou a conseguir que o método super.paintComponent() funcione como deveria. 

O que se pretende é que a forma geométrica seja desenhada e re-desenhada para dar a sensação de movimento, isto sem que cada forma desenhada fique visível no ecrã (apenas a última forma desenhada deverá ser visível em cada milisegundo).

No entanto, o que estou a obter é um rasto de formas desenhadas no ecrã. Invocar o super.paintComponent() deveria eliminar isto, pois é ele o responsável por redesenhar (apagar) o background do JPanel correto?

Segue o código que tenho implementado (o código foi bastante simplificado, para ajudar a análise):

public class Animated_Shape_Test extends JFrame
{
    public static void main(String args[]) throws IOException 
    {
        new Animated_Shape_Test();
    }
    
    public Animated_Shape_Test() throws IOException
    {
        this.setSize(500, 500);
        this.setPreferredSize(new Dimension(500, 500));
        this.setLocation(new Point(430, 150));
        this.setTitle("Database Launcher v1.0");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);
        
        DBPanel panel = new DBPanel();
        
        getContentPane().add(panel, BorderLayout.CENTER);
    }
}

final class DBPanel extends JPanel implements Runnable
{   
    int musicShapePosX = 85;
    int musicShapePosY = 100;
    int SEGMENT_SHAPE_LENGTH = 50;
    int SHAPE_HEIGHT = 10;
    float SHAPE_SPEED = 7.5f; 
    
    CustomShapeButton musicShapeButton = new CustomShapeButton(musicShapePosX, musicShapePosY, SEGMENT_SHAPE_LENGTH, SHAPE_HEIGHT);
    
    private ArrayList<Shape> shapes = null;
    
    protected DBPanel() throws IOException
    {   
        shapes = new ArrayList();
        
        shapes.add(musicShapeButton);
        
        this.setOpaque(true);
        this.setFocusable(true);
        
        startThread();
    }
    
    public void startThread()
    {
        Thread t = new Thread(this);
        
        t.start();
    }
    
    @Override
    public void paintComponent(Graphics g) 
    {
        super.paintComponent(g);
        
        Graphics2D g2 = (Graphics2D) g;
        
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  
        g2.setColor(getBackground().darker());
        g2.fillRect(0, 0, getWidth(), getHeight());
        
        g2.setColor(Color.BLACK);
        g2.draw(musicShapeButton);
    }
    
    public void delay(int milliseconds)
    {
        try
        {
            Thread.sleep(milliseconds);
        } 
        catch (InterruptedException e)
        {
        }
    }
     
    @Override
    public void run() 
    {
        while (true)
        {
            delay(35);
            animateButtonShapeMusic();
            repaint();
        }
    }
    
    public void animateButtonShapeMusic()
    {
        if (musicShapePosY < 228)
        {
            musicShapePosY = (int)(musicShapePosY + SHAPE_SPEED);    
            
            musicShapeButton.drawShape(musicShapePosX, musicShapePosY, SEGMENT_SHAPE_LENGTH, SHAPE_HEIGHT);
        }
    }
}

Obrigado desde já. Cumps.

Edited by chuckyegg2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.