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
Solved

Vertical scrolling using Graphics2D help needed.

Posted on 2002-07-29
11
353 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

808 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