chuckyegg2 0 Posted July 8, 2019 Report Share Posted July 8, 2019 (edited) 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 July 9, 2019 by chuckyegg2 Link to post Share on other sites
chuckyegg2 0 Posted July 10, 2019 Author Report Share Posted July 10, 2019 Está resolvido! Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now