Solved

Beginner: Creating a scrolling text applet

Posted on 2000-02-14
5
231 Views
Last Modified: 2012-06-27
I'm trying to get into Java, but I am a reasonably experienced C++ programmer. However not in the GUI field so a lot of the graphic programming is new to me.

Can someone tell me which classes I need to use to create a "scrolling text" applet? A simple step by step procedure would be nice just to get me started.
0
Comment
Question by:boomerang061797
  • 3
5 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 2518795
hi,
 try the following code
  ScrollPane scrollPane = new ScrollPane();
  TextArea textArea = new TextArea();

in init()
    this.getContentPane().add(scrollPane, BorderLayout.CENTER);
    scrollPane.add(textArea, null);
    scrollPane.add(textArea, null);

mohan
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2519106
override paint() method to render the text.
start another Thread that will change the text origin coordinates and call repaint()

there are a lot of examples on the web - check
www.javashareware.com
www.jars.com
www.gamelan.com
0
 

Accepted Solution

by:
xtvca earned 100 total points
ID: 2520388
Hnag on

I'm begginer to but I'll try to help
This is the code for horisontal scrolling text applet
I'll try to explain the best I can
(sorry for english i'ts not my native language)

import java.awt.Graphics;
import java.applet.*;
import java.awt.Image;
import java.awt.Color;

//header files needed for applet and graphics manipulation


public class scroller extends Applet implements Runnable { //begginig of class
      /*Global variables*/
      int coordx,q;
      Thread thread;
      Image slikaIzvanEkrana;
      Graphics grafickiMedjuspremnik;//sorry for names (my native language)
      //--------------------
      
      
                                                /*----  applet initialisation
                                                each applet have init,start,paint,repaint methods
                                                we need to overide them for our purpose
                                                */
                                                      public void init(){
                                                      slikaIzvanEkrana = createImage(size().width,size().height);
                                                      /*create new image for double buffering
                                                      we will first draw on this pic and then we willl copy the content of this pic
                                                      on the screen that prevents flickering
                                                      so called double buffering
                                                      */
                                                      
                                                      grafickiMedjuspremnik = slikaIzvanEkrana.getGraphics();
                                                      coordx=400;//first coordinate on which will we draw text      

                                                }
                                                
      
                                                //-- starting applet
                                                public void start(){
                                                      thread = new Thread(this);//making new thread of this one
                                                      thread.start();//starting this thread
                                                }
      


                              
      
      
      //--- The thread body (we started this method upper with thread.start()
            public void run() {

                  q=20;

            while(q<40) {//condition will always be trouth so the method will not stop (our thread will not end)
                  
                        repaint();//caling repaint (which we modyfied will clear the double buffer image)
                              
                        coordx=coordx-2;                                    //------ move the text for 2 pixels left
                                    
                                    try {
                                          Thread.sleep(30);//stop our thread for 30 milisecounds(speed od movement)
                                    } catch (InterruptedException e) {
                                    }
                                    //------ end try/catch InterruptedException e
            
              if(coordx<-400) coordx=400;//if our text is out of the screen (left side)

                              //      ---- !!! drawing text in double buffer
                              grafickiMedjuspremnik.setColor(Color.orange);//set color of text
                              grafickiMedjuspremnik.drawString("This is the scrolling text ...... jeeeeaaahhh ...... :-]",coordx,10);                              
                              //draw text "This is.." ,xpos,ypos

                  
            }//end while loop
            
      
      }//end run method (thread body)








      //-- overiding paint method to use our double buffer graphics
      public void paint(Graphics g) {
            g.drawImage(slikaIzvanEkrana,0,0,this);//draw our double buffer image on the applet
            
      }
      
      
      //--- ovveriding upddate method for repainting to prevent flickering during clearing of screen
      public void update(Graphics g) {
            paint(g);
            
            //clear our double bufferd graphics otherwise text will have traces of prevois drawing
            grafickiMedjuspremnik.setColor(Color.white);
            grafickiMedjuspremnik.fillRect(0,0,size().width,size().height);
      }
      


}//end of our class

now you need a htm document

somthing like this

<body bgcolor="white">

  <APPLET CODE="scroller" width=400 height=200>
  </APPLET>

of course this is the simple applet and it takes no param
but you can add it easy
also i used rough estimation of text width like x=400

you have methods which gives the exact amount of string width and height

Hope this can help (I know how tought is at the beggining)
:-)

Vedran

0
 

Expert Comment

by:xtvca
ID: 2520399
/*create new image for double buffering
we will first draw on this pic and then we willl copy the content of this pic
on the screen that prevents flickering
so called double buffering

I meant that method of using secound pic for drawing and later copying it to screen to prevent flickering is called souble buffering

sorry for typos
0
 

Expert Comment

by:xtvca
ID: 2526195
Thanks for points (My first expert points)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now