Java programming

Discussion in 'Windows Desktop Systems' started by Polypeptide, Apr 22, 2003.

  1. Polypeptide

    Polypeptide Guest

    Ok, so I'm trying to make this java program. It's very trivial, but I hate java, and It's the last program I'm making for a CS project. I have to make the text scroll down, stop halfway down the page, and then scroll to the right. The text consists of 5 different city names, each having a different color. Right now I can only make the text scroll down. If someone could help me make the text scroll down, stop, and then go to the right and loop, I could implement the 5 different city names and make them different colors. Thanks for your help.






    import java.awt.*;


    public class VerticalScrollText extends java.applet.Applet implements Runnable


    {


    String city1 = "Chicago";
    String city2 = "Prague";
    String city3 = "Fredericton";
    String city4 = "Toronto";
    String city5 = "Bangkok";



    int yPos = 0;


    Thread runner;



    public void start()


    {


    if (runner == null)


    {


    runner = new Thread(this);


    runner.start();


    }


    }



    public void run()


    {


    while(true)


    {


    if (yPos > getSize().height)


    {


    yPos = 0;


    }


    else


    {


    yPos += 2;


    repaint();


    try


    {


    runner.sleep(50);


    }


    catch (InterruptedException e) { }


    }


    }


    }



    public void paint (Graphics g)


    {

    g.drawString(city1, 50,yPos);

    }

    }
     
  2. Blitzkrieg

    Blitzkrieg Guest

    Why not use an array for your city names? Just a suggestion, don't as me about the scrolling to the side though.
     
  3. Swi

    Swi gningnegneur

    Messages:
    302
    Location:
    Montreal
    I'm not a java guru, but if you can call g.drawString(city1, 50,yPos), why can't you do the same for a variable xPos like
    g.drawString(city1, xPos,yPos)

    and just put a few flags to know when stop increasing yPos and start increasing xPos

    definition of drawstring is: drawString(String str, int x, int y)

    so i guess you could do that
     
  4. Polypeptide

    Polypeptide Guest

    Thanks guys, i'm on the way to a solution. Never thought about that before. I'll let you know!
     
  5. Blitzkrieg

    Blitzkrieg Guest

    I'm not sure if this is exactly what you wanted, but here you go:

    save in a file called City.java and compile (forum messes up tabs)

     
  6. Polypeptide

    Polypeptide Guest

    Thanks!

    Wow Blitzkrieg, thanks! I can't believe people here are so supportive. I really like these forums. It's funny, you posted that about 4 minutes after I actually restructured my code and got it all working. I'll post the code tomorrow and you can see that you're solution isn't that far away from mine. Thanks a million!
     
  7. Polypeptide

    Polypeptide Guest

    Here's what i did:





    import java.awt.*;
    import java.applet.Applet;

    public class VerticalScrollText extends java.applet.Applet implements Runnable


    {


    int yPos = 0;
    int xPos = 0;
    int w = 0;
    String [] count = {"Chicago", "Prague", "Bangkok", "Fredericton", "Bratislava"};
    Color [] pretty = {Color.red, Color.black, Color.yellow, Color.green, Color.blue};
    Thread runner;

    public void start()
    {
    if (runner == null)
    {

    runner = new Thread(this);
    runner.start();
    }
    }

    public void run()
    {
    while(true)
    {
    if (xPos > getSize().width)
    {
    w++;
    if (w>4)
    {
    w = 0;
    }
    xPos = 0;
    yPos = 0;

    }
    else if (yPos > getSize().height/2)
    {

    xPos+=2;
    repaint();

    try
    {
    runner.sleep(20);
    }

    catch (InterruptedException e) { }

    }
    else
    {
    yPos += 2;
    repaint();

    }

    try
    {
    runner.sleep(20);
    }

    catch (InterruptedException e) { }

    }

    }


    public void paint (Graphics g)

    {

    g.setColor(pretty[w]);
    g.drawString(count[w], xPos, yPos);

    }

    }
     
  8. Blitzkrieg

    Blitzkrieg Guest

    Ya no problem, I had some extra time after an exam to burn so why not :p