Boa tarde,
Estou a tentar implementar algo muito simples: desenhar a mesma forma geométrica no ecrã em vários sitios diferentes, para dar a sensação de movimento.
O problema que está a acontecer é que, fazendo override ao paintComponent() e, lá dentro, invocando o super.paintComponent(), este não está a redesenhar o background do JPanel como deveria. E, assim, sendo, fico sempre com cada forma geométrica desenhada no ecrã e visivel, o que não é pretendido (o que é pretendido é que apenas seja visivel, a cada momento, a última forma geométrica desenhada, dando a sensação de movimento).
Segue o código: (não envio a classe CustomShapeButton por ser desnecessária para esta análise)
public class DBLauncher extends JApplet
{
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setTitle("Database Launcher v1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new DBLauncher();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setLocation(new Point(430, 150));
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(500, 500);
}
@Override
public void init()
{
DBPanel panel = null;
try
{
panel = new DBPanel();
}
catch (IOException ex)
{
Logger.getLogger(DBLauncher.class.getName()).log(Level.SEVERE, null, ex);
}
getContentPane().add(panel);
panel.setBackground(Color.lightGray);
}
}
class DBPanel extends JPanel implements Runnable, MouseListener
{
int musicShapePosX = 85;
int musicShapePosY = 100;
int tvShapePosX = 184;
int tvShapePosY = 164;
int gamesShapePosX = 283;
int gamesShapePosY = 100;
int booksShapePosX = 283;
int booksShapePosY = 228;
int techShapePosX = 184;
int techShapePosY = 293;
int SEGMENT_SHAPE_LENGTH = 50;
int SHAPE_HEIGHT = 10;
int IMAGE_FACTOR_X = 1 ;
int IMAGE_FACTOR_Y = 49;
int musicImagePosX = musicShapePosX + IMAGE_FACTOR_X;
int musicImagePosY = musicShapePosY - IMAGE_FACTOR_Y;
int tvImagePosX = tvShapePosX + IMAGE_FACTOR_X;
int tvImagePosY = tvShapePosY - IMAGE_FACTOR_Y;
int gamesImagePosX = gamesShapePosX + IMAGE_FACTOR_X;
int gamesImagePosY = gamesShapePosY - IMAGE_FACTOR_Y;
int booksImagePosX = booksShapePosX + IMAGE_FACTOR_X;
int booksImagePosY = booksShapePosY - IMAGE_FACTOR_Y;
int techImagePosX = techShapePosX + IMAGE_FACTOR_X;
int techImagePosY = techShapePosY - IMAGE_FACTOR_Y;
float SHAPE_SPEED = 7.5f;
CustomShapeButton musicShapeButton = new CustomShapeButton(musicShapePosX, musicShapePosY, SEGMENT_SHAPE_LENGTH, SHAPE_HEIGHT);
CustomShapeButton tvShapeButton = new CustomShapeButton(tvShapePosX, tvShapePosY, SEGMENT_SHAPE_LENGTH, SHAPE_HEIGHT);
CustomShapeButton gamesShapeButton = new CustomShapeButton(gamesShapePosX, gamesShapePosY, SEGMENT_SHAPE_LENGTH, SHAPE_HEIGHT);
CustomShapeButton booksShapeButton = new CustomShapeButton(booksShapePosX, booksShapePosY, SEGMENT_SHAPE_LENGTH, SHAPE_HEIGHT);
CustomShapeButton techShapeButton = new CustomShapeButton(techShapePosX, techShapePosY, SEGMENT_SHAPE_LENGTH, SHAPE_HEIGHT);
private static BufferedImage musicShapeImage;
private static BufferedImage tvShapeImage;
private static BufferedImage gamesShapeImage;
private static BufferedImage techShapeImage;
private static BufferedImage booksShapeImage;
private ArrayList<Shape> shapes = null;
boolean musicButtonPressed = false;
boolean tvButtonPressed = false;
boolean gamesButtonPressed = false;
boolean booksButtonPressed = false;
boolean techButtonPressed = false;
@SuppressWarnings("CallToThreadStartDuringObjectConstruction")
public DBPanel() throws IOException
{
shapes = new ArrayList();
shapes.add(musicShapeButton);
shapes.add(tvShapeButton);
shapes.add(gamesShapeButton);
shapes.add(booksShapeButton);
shapes.add(techShapeButton);
try
{
musicShapeImage = ImageIO.read(getClass().getResource("/images/imageWoodMusic.png"));
tvShapeImage = ImageIO.read(getClass().getResource("/images/imageWoodTv.png"));
gamesShapeImage = ImageIO.read(getClass().getResource("/images/imageWoodGames.png"));
booksShapeImage = ImageIO.read(getClass().getResource("/images/imageWoodBooks.png"));
techShapeImage = ImageIO.read(getClass().getResource("/images/imageWoodTech.png"));
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
this.setFocusable(true);
addMouseListener(this); //Mouse events listener
//Create and initialize the animation thread
Thread thread = new Thread(this);
thread.start();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(musicShapeButton);
// g2.draw(tvShapeButton);
// g2.draw(gamesShapeButton);
// g2.draw(booksShapeButton);
// g2.draw(techShapeButton);
// g2.drawImage(musicShapeImage, musicImagePosX, musicImagePosY, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, null);
// g2.drawImage(tvShapeImage, tvImagePosX, tvImagePosY, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, null);
// g2.drawImage(gamesShapeImage, gamesImagePosX, gamesImagePosY, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, null);
// g2.drawImage(booksShapeImage, booksImagePosX, booksImagePosY, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, null);
// g2.drawImage(techShapeImage, techImagePosX, techImagePosY, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, (SEGMENT_SHAPE_LENGTH + SHAPE_HEIGHT) * 2, null);
}
@Override
@SuppressWarnings("SleepWhileInLoop")
public void run()
{
while (true)
{
if (musicButtonPressed == true)
{
animateButtonShapeMusic();
}
else if (tvButtonPressed == true)
{
}
else if (gamesButtonPressed == true)
{
}
else if (booksButtonPressed == true)
{
}
else if (techButtonPressed == true)
{
}
repaint();
try
{
Thread.sleep(35);
}
catch (InterruptedException ex)
{
break;
}
}
}
public void delay(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (InterruptedException e)
{
}
}
@Override
public void mouseClicked(MouseEvent me)
{
for (int i = 0; i < shapes.size(); i++)
{
Shape shape = shapes.get(i);
//Mostrar os icones respetivos, consoante o botão seleccionado (if's)
if (shape.contains(me.getPoint()))
{
if (shape.equals(i) == shape.equals(0))
{
musicButtonPressed = true;
System.out.println("Clicked on shape number " + i + " that represents the music button!");
}
else if (shape.equals(i) == shape.equals(1))
{
tvButtonPressed = true;
System.out.println("Clicked on shape number " + i + " that represents the tv button!");
}
else if (shape.equals(i) == shape.equals(2))
{
gamesButtonPressed = true;
System.out.println("Clicked on shape number " + i + " that represents the games button!");
}
else if (shape.equals(i) == shape.equals(3))
{
booksButtonPressed = true;
System.out.println("Clicked on shape number " + i + " that represents the books button!");
}
else if (shape.equals(i) == shape.equals(4))
{
techButtonPressed = true;
System.out.println("Clicked on shape number " + i + " that represents the tech button!");
}
}
}
}
public void animateButtonShapeMusic()
{
if (musicShapePosY < 228)
{
musicShapePosY = (int)(musicShapePosY + SHAPE_SPEED);
musicShapeButton.drawShape(musicShapePosX, musicShapePosY, SEGMENT_SHAPE_LENGTH, SHAPE_HEIGHT);
}
}
public void animateButtonShapeTv()
{
}
public void animateButtonShapeGames()
{
}
public void animateButtonShapeBooks()
{
}
public void animateButtonShapeTech()
{
}
@Override
public void mousePressed(MouseEvent me)
{
}
@Override
public void mouseReleased(MouseEvent me)
{
}
@Override
public void mouseEntered(MouseEvent me)
{
}
@Override
public void mouseExited(MouseEvent me)
{
}
}
Obrigado e cumprimentos,
J Amorim