Solved

Vertical scrolling using Graphics2D help needed.

Posted on 2002-07-29
11
358 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
Technology Partners: 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 153
Java Restore security prompts not working 10 135
add projects t working set in maven 2 44
Java syntax, or is it Selenium 6 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

733 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