0

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 2
    easiest way to accomplish is to use a JScrollPane on your JLabel set to never show either scrollbar. Then construct a javax.swing.Timer and adjust the JScrollPane's scrollbars in the corresponding action. – ControlAltDel Sep 08 '14 at 14:01
  • So from what I understood. I shall create a JScrollPane with desired contents and add it to the first part of code (before revalidate()). Then I shall add a Timer which will trigger sliding the bar ? – Marcin Wilczyński Sep 08 '14 at 14:10
  • Yes, you've got the gist. Go try it out and update your code here if you have problems – ControlAltDel Sep 08 '14 at 14:14
  • Got another problem :P When I tried adding JScrollPane to JLabel, but only picture loaded to JLabel was shown. When I added JSP to JFrame only picture of JSP was shown. How can I make to get JSP contents with background made from JLabel picture ? – Marcin Wilczyński Sep 08 '14 at 14:33
  • Ok, I think I'm stupid ... why I didn't think about just adding string to picture with parameters that would be changed via timer and revalidated ? Sometimes Im retarded ._.' – Marcin Wilczyński Sep 08 '14 at 14:44

2 Answers2

1

Use javax.swing.Timer to schedule periodic events, and do animation changes from the thread, fired by this timer. As @ControlAltDel noted, JScrollPane may be a good class to look up for implementing moving texts.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • The tutorial is a good starting point; a related example is shown [here](http://stackoverflow.com/a/3621417/230513). – trashgod Sep 08 '14 at 15:14
0

Try this, create a new Java class and paste this code:

Image link

import java.util.Timer;
import java.util.TimerTask;

public class TextoDesplazable extends javax.swing.JFrame {

    Timer timer = new Timer();
    int x, y, limite;

    public TextoDesplazable() {
        initComponents();
        this.setLocationRelativeTo(null);
        x = jLabel1.getX();
        y = jLabel1.getY();
        limite = -jLabel1.getWidth();
        System.out.println("X: " + x);

        timer.schedule(task, 0, 5);
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            if (x < limite) {
                x = TextoDesplazable.this.getWidth();
            }
            jLabel1.setLocation(x--, y);
        }
    };
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Hello");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(308, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TextoDesplazable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextoDesplazable().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}
ricardo130
  • 155
  • 2
  • 4