?
Solved

Vertical scrolling using Graphics2D help needed.

Posted on 2002-07-29
11
Medium Priority
?
366 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 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