[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Vertical scrolling using Graphics2D help needed.

Posted on 2002-07-29
11
Medium Priority
?
372 Views
Last Modified: 2010-03-31
Has anyone had any experience in doing vertical scrolling using Graphics2D.  Basically I need to create a vertical scrolling "widget" that I can use in Swing applets.  I'd like just to be able to pass it a String and the widget will display the text, scrolling the previous text up if necessary.

I have created a component that will do this with a JList inside a JScrollPane.  However, there is a large amount of data that currently is being received (via JMS) by my applet and I noticed that by scrolling using my current method (i.e. calling the method ensureIndexIsVisible() on the scrollpane) is processor intensive. Also, I have noticed that it is even more processor intensive if I turn on BACKINGSTORE_SCROLL_MODE in the viewport of the JScrollPane.  Hence, I thought that Graphics2D may be a better approach.

I'd appreciate it if someone could either point me to a link that will give me an example of how to do this or respond with an example of their own.

Thanks in advance for your help.
0
Comment
Question by:mwalker
  • 6
  • 4
11 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 7185756
if nobody else responds to your problem, I will post some code tomorrow (have to go now)

have a nice eveneing :)
0
 

Author Comment

by:mwalker
ID: 7186334
Thanks heyhey.  I will appreciate it.
0
 
LVL 92

Expert Comment

by:objects
ID: 7186891
Just create an offscreen image of what you want to display and 'move' it up the screen.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:heyhey_
ID: 7187260
do you want all the "old" info to be visible after scrolling ?
0
 

Author Comment

by:mwalker
ID: 7187904
objects: I'm not experienced with drawing in Java.  So, I'm not exactly sure what you mean by an offscreen image.  I also don't know how to "move it up", which is why I posted this question.

heyhey: I do not need the either the "old" info to be visible or accessible after scrolling.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7188079
so you need custom component that displays several rows of text and when new text is appened the old one scroll up (and possible disappear) ?
0
 

Author Comment

by:mwalker
ID: 7188698
You are exactly correct.  If you can show me a simple subclass of JPanel that uses Graphics2D for displaying the text and scrolls up (and possibly disappears), I can add more stuff as I need it (e.g. rendering lines in different colors based on the data sent to it).

Thanks.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7196614
sorry, I was very busy these last few days, I will try to post some sample code during the weekend.
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 800 total points
ID: 7198798
mwalker, here is the example code

copy&paste the code in a single file test.java, then compile it and run it.


import java.awt.*;
import java.util.*;
import javax.swing.*;

public class test
{
  public static void main(String args[])
  {
    try
    {
      JFrame f = new JFrame();
      MessagePanel panel = new MessagePanel();
      MessagePupmer pumper = new MessagePupmer(panel);

      f.getContentPane().setLayout(new GridLayout(1, 1));
      f.getContentPane().add(panel);
      f.setBounds(100, 100, 600, 400);
      f.setVisible(true);

      pumper.start();
    }
    catch (Throwable x)
    {
      x.printStackTrace();
    }
  }
}

class MessagePupmer implements Runnable
{
  private MessagePanel panel;
 
  public MessagePupmer(MessagePanel p)
  {
    panel = p;
  }

  public void start()
  {
    Thread thisThread = new Thread(this);
    thisThread.start();
  }
 
  public void run()
  {
    while(true)
    {
      SingleMessage mes = new SingleMessage();
      panel.addMessage(mes);
     
      int sleepTime = 200 + (int)(Math.random() * 600);
      try{Thread.sleep(sleepTime);}catch (Throwable x){}
    }
  }
}

class MessagePanel extends JPanel
{
  private Vector messages = new Vector();
 
  public void paintComponent(Graphics g)
  {
    Dimension d = getSize();
    Font f = getFont();
    FontMetrics fm = g.getFontMetrics(f);
    int fontHeight = fm. getHeight();
   
    g.setColor(Color.lightGray);
    g.fillRect(0, 0, d.width, d.height);
    g.setFont(f);
   
    int yPos = d.height - fontHeight;
   
    synchronized (messages)
    {
      for (int i = messages.size() - 1; i >=0; i--)
      {
        if (yPos < -fontHeight) break;
        SingleMessage mes = (SingleMessage)messages.elementAt(i);
        g.setColor(mes.messageColor);
        g.drawString(mes.messageText, 5, yPos);
        yPos -= fontHeight;
      }
    }
  }
 
  public void addMessage(SingleMessage mes)
  {
    synchronized (messages)
    {
      messages.addElement(mes);
      if (messages.size() > 100)
      {
        messages.removeElementAt(0);
      }
    }
    repaint();
  }
}

class SingleMessage
{
  public static String[] demoMessages = {"Message 1", "Another message", "Error !!", "Nothing new", "Check your e-mail"};
  public static Color[] demoColors = {Color.red, Color.white, Color.black, Color.darkGray, Color.magenta};
 
  public String messageText;
  public Color messageColor;
 
  public SingleMessage()
  {
    int n1 = (int)(Math.random() * demoMessages.length);
    int n2 = (int)(Math.random() * demoColors.length);
   
    messageText = demoMessages[n1];
    messageColor = demoColors[n2];
  }
}
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7200515
any news ?
0
 

Author Comment

by:mwalker
ID: 7200897
heyhey: This is exactly what I needed. Thanks so much.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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 …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 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