Solved

Beginner: Creating a scrolling text applet

Posted on 2000-02-14
5
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

729 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