Solved

Vertical scrolling using Graphics2D help needed.

Posted on 2002-07-29
11
346 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 200 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 98
rhino JavaScript import, load 25 82
How to convert from xls to xlsx using java 7 39
pagenation logic how it is working in my code 1 30
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 …
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…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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