Solved

Beginner: Creating a scrolling text applet

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 77
mapShare challenge 13 69
Java Message handling in Service Layer 3 38
Error with Java/Cache JDBC Classpath 2 20
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

759 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

18 Experts available now in Live!

Get 1:1 Help Now