0

Pleae help to solve this problem... I has a page designed in JFrames Now i need to make a text scrolling at the top of the page.... Please provide me the code...

1
  • 3
    "Please provide me the code..." Please have a seat while we whip up some code for you. Oh, and help yourself to coffee and donuts while you're waiting... Commented Feb 6, 2011 at 15:10

3 Answers 3

1

Check this out, is is commented and will most likely help you.

http://www.abbeyworkshop.com/howto/java/ta_scroll/index.html

Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps this thread may be of interest to you: Creating A Scrolling Marquee

Comments

1
   ScrollText s=   new ScrollText("ello Everyone.");
   jLabel3.add(s);//add it to jLabel


     public class ScrollText extends JComponent {
      private BufferedImage image;
      private Dimension imageSize;
      private volatile int currOffset;
      private Thread internalThread;
      private volatile boolean noStopRequested;
      public ScrollText(String text) {
       currOffset = 0;
       buildImage(text);
       setMinimumSize(imageSize);
       setPreferredSize(imageSize);
       setMaximumSize(imageSize);
       setSize(imageSize);
       noStopRequested = true;
       Runnable r = new Runnable() {
       public void run() {
        try {
        runWork();
       } catch (Exception x) {
        x.printStackTrace();
      } 
     }
   };

   internalThread = new Thread(r, "ScrollText");
   internalThread.start();
   }

     private void buildImage(String text) {
     RenderingHints renderHints = new RenderingHints(
     RenderingHints.KEY_ANTIALIASING,
     RenderingHints.VALUE_ANTIALIAS_ON);

     renderHints.put(RenderingHints.KEY_RENDERING,
     RenderingHints.VALUE_RENDER_QUALITY);

     BufferedImage scratchImage = new BufferedImage(1, 1,
     BufferedImage.TYPE_INT_RGB);

     Graphics2D scratchG2 = scratchImage.createGraphics();
     scratchG2.setRenderingHints(renderHints);

     Font font = new Font("Serif", Font.BOLD | Font.ITALIC, 24);

     FontRenderContext frc = scratchG2.getFontRenderContext();
     TextLayout tl = new TextLayout(text, font, frc);
     Rectangle2D textBounds = tl.getBounds();
     int textWidth = (int) Math.ceil(textBounds.getWidth());
     int textHeight = (int) Math.ceil(textBounds.getHeight());

      int horizontalPad = 600;
      int verticalPad = 10;

       imageSize = new Dimension(textWidth + horizontalPad, textHeight
       + verticalPad);

       image = new BufferedImage(imageSize.width, imageSize.height,
    BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = image.createGraphics();
        g2.setRenderingHints(renderHints);

       int baselineOffset = (verticalPad / 2) - ((int) textBounds.getY());

      g2.setColor(Color.BLACK);
       g2.fillRect(0, 0, imageSize.width, imageSize.height);

      g2.setColor(Color.GREEN);
       tl.draw(g2, 0, baselineOffset);

            // Free-up resources right away, but keep "image" for
            // animation.
       scratchG2.dispose();
       scratchImage.flush();
       g2.dispose();
   }
         public void paint(Graphics g) {
         // Make sure to clip the edges, regardless of curr size
            g.setClip(0, 0, imageSize.width, imageSize.height);

            int localOffset = currOffset; // in case it changes
             g.drawImage(image, -localOffset, 0, this);
              g.drawImage(image, imageSize.width - localOffset, 0, this);

                       g.setColor(Color.black);
             g.drawRect(0, 0, imageSize.width - 1, imageSize.height - 1);
         }
                 private void runWork() {
                while (noStopRequested) {
               try {
                  Thread.sleep(10); // 10 frames per second

                       // adjust the scroll position
                          currOffset = (currOffset + 1) % imageSize.width;

                             // signal the event thread to call paint()
                            repaint();
                          } catch (InterruptedException x) {
                               Thread.currentThread().interrupt();
                      }          
                  }
                  }

             public void stopRequest() {
               noStopRequested = false;
               internalThread.interrupt();
                    }

                public boolean isAlive() {
                   return internalThread.isAlive();
                          }
                  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.