I'm willing to add a moving text and pictures (from one side to another and after it goes out of "windows" to repeat the cycle).
Currently I'm using such construction to display menu in my application :
try
{
repaint = ImageIO.read(new File(ReturnPageName(0)));
}catch (IOException e) {
}
image = new ImageIcon(repaint.getScaledInstance(sizeX,sizeY, Image.SCALE_SMOOTH));
imageLabel.setIcon(image);
revalidate();
repaint();
Where JLabel
is set in JFrame
with such parameters :
imageLabel = new JLabel();
imageLabel.setIcon(image);
imageLabel.setVisible(true);
add(imageLabel);
setUndecorated(true);
setVisible(true);
setSize(sizeX, sizeY);
I want to add to the displayed menu picture moving text/pictures as stated above. How can I easily do it ? Tried with new class extending runnable but it wasn't successful.
EDIT :
I've done something like this,
Drawing menu :
public void drawPageZero()
{
repaint.setData(repaint0.getData());
Graphics g = repaint.createGraphics();
g.setFont(font);
g.setColor(black);
FontMetrics fm = g.getFontMetrics();
if (xN < - fm.stringWidth(text))
{
xN = 2 * fm.stringWidth(text);
}
if (xN < 0 && xN >= -fm.stringWidth(text))
{
xN2 = xN + fm.stringWidth(text);
xN3 = xN + 2 *fm.stringWidth(text);
}
else if (xN <= 2*fm.stringWidth(text) && xN >= fm.stringWidth(text))
{
xN2 = xN - 2*fm.stringWidth(text);
xN3 = xN - fm.stringWidth(text);
}
else if (xN >= 0 && xN < fm.stringWidth(text))
{
xN2 = xN + fm.stringWidth(text);
xN3 = xN - fm.stringWidth(text);
}
g.drawString(text,xN,66);
g.drawString(text,xN2,66);
g.drawString(text,xN3,66);
image = new ImageIcon(repaint.getScaledInstance(sizeX,sizeY, Image.SCALE_SMOOTH));
imageLabel.setIcon(image);
revalidate();
repaint();
timer = new Timer();
timer.schedule(new infoBar(), 50);
}
Where timer looks like that :
class infoBar extends TimerTask
{
@Override
public void run() {
if (page ==0) {
xN -= 1;
timer.cancel();
timer.purge();
drawPageZero();
}
else
{
timer.cancel();
timer.purge();
}
}
}
The problem now is that it pretty hurts when looking at the moving text. It's probably due to too slow refreshing, which might be caused by repaint.setData(repaint0.getData());
operation. Is there any easy and fast way to paste image from one BufferedImage
to another? Before that I was loading picture each cycle, but it was also slow.