• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Beginner: Creating a scrolling text applet

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
boomerang061797
Asked:
boomerang061797
  • 3
1 Solution
 
expertmbCommented:
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
 
heyhey_Commented:
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
 
xtvcaCommented:
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
 
xtvcaCommented:
/*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
 
xtvcaCommented:
Thanks for points (My first expert points)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now