Solved

Beginner: Creating a scrolling text applet

Posted on 2000-02-14
5
233 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
javap not working 8 58
hibernate example using maven 12 56
hashmap order 17 36
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

856 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