🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.java.gui
Article #74

JScrollPane: No ScrollBar with my JComponent

#74
From: Andreas Raschle
Date: Fri, 30 Jan 1998 00:00
141 lines
4796 bytes
Dear reader

The example JScrollPane works well, it loads its image ( a gif file) and
shows the ScrollBars on both sites if needed:

public class TigerPanel extends JPanel{
  public TigerPanel() {
    setLayout(new BorderLayout());
    Icon BigTiger = new ImageIcon ("BigTiger.gif");
    JLabel TigerLabel = new JLabel (BigTiger);
    JScrollPane TigerPane = new JSCrollPane();
    TigerPane.getViewport().add(TigerLabel);
    add(TigerPane,BorderLayout.CENTER);
  }
}

Now I'd like (I'm told to do that ;-) to paint my pixels to a new image.

The problems/questions are:
- When I use a JComponent instead, which has its own Image 
  with createImage() outside of JComponent.paint(), createImage()
  doesn't work.
- Even the Image is larger than the JComponent, no ScrollBars will
  appear.
- My Graphic can change its ZoomFactor, would that be a problem for
  JLabel (JLabel is thought for Images, which do not change their size)
- Where can I get an example of the JScrollPanel with a self painted
  image? (I scanned many FAQ's and the Online-Tutorial from JDC)

Following there is my sample application, which represents my problem.

Thank you for every hint in advance
Andy

PS: I'm developping with jdk 1.1.5 with swing 0.7 on WinNT4.0 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Andreas Raschle          + phone:  ++41 (0)52 267 77 29 (b)
Wiesenstr. 55            +         ++41 (0)52 765 15 60 (p)
8552 Felben-Wellhausen   + fax  :  ++41 (0)52 268 71 09
Switzerland              + www  :  http://www.twi.ch/~ras
                         + UIN  :  5423307 (www.mirabilis.com)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

ArrayTest.java
###############################################################
import com.sun.java.swing.*;

public class ArrayTest extends JFrame implements ActionListener
{
  JMenuItem item,FileExitMenuItem;
  JScrollPane Pane;
  JViewport MyViewport;
  
  public ArrayTest()
  {
    super("My Swing Viewer");
    setSize(800, 600);
    showWindowElements();
  }
  
  public void showWindowElements()
  {
    JMenuBar jmb = new JMenuBar();
    JMenu file = new JMenu ("File");
    file.add (item = new JMenuItem ("New"));
    item.addActionListener (this);
    file.add (item = new JMenuItem ("Open"));
    item.addActionListener (this);
    file.addSeparator();
    file.add (FileExitMenuItem = new JMenuItem ("Close"));
    item.addActionListener (this);
    jmb.add (file);
    setJMenuBar (jmb);
    Pane = new JScrollPane();
    Pane.setPreferredSize(new Dimension(700, 300));
    MyViewport = Pane.getViewport();
    JComponent bigTiger = new CMyImageIcon();
    MyViewport.add(bigTiger);
    getContentPane().add(Pane);
  } // showWindowelements()
    
  public void actionPerformed (ActionEvent evt) {
    Object Source = evt.getSource();
    if (Source == FileExitMenuItem) {
      dispose();
      System.exit(0);
    } //FileExitMenuItem
    
  }
    
  public static void main(String argv[]){
    ArrayTest MyArray = new ArrayTest();
    MyArray.setLocation(200,100);
    MyArray.show();
  } // main
}
 
CMyImageIcon.java:
##############################################################

import com.sun.java.swing.*;

public class CMyImageIcon extends JComponent
{
  Image OffImage;
  Graphics OffGraphics;
  int Width = 850;
  int HeightInit = 650;
  
  public CMyImageIcon ()
  {
    super ();
  }
  
  private void drawOffImage(Graphics g)
  {
    int X = 0; int Y = 0;
    int W = 80; int H = 160;
    int Start = 0; int Arc = 270;
    g.drawArc (X,Y,W,H,Start,Arc);
    W = 120; H = 120; 
    X = Width - W;
    Y = 0;
    Start = 90; Arc = -270;
    g.drawArc (X,Y,W,H,Start,Arc);
  }

  protected void paintComponent(Graphics g)
  {
    if (OffImage == null)
    {  
      OffImage = createImage (Width, HeightInit);
      OffGraphics = OffImage.getGraphics();
      drawOffImage (OffGraphics);
    }
    g.drawImage(OffImage,0,0,Width,HeightInit,
      0,0,Width,HeightInit,this);
  }
}
Technical Details
Message-ID: <34D1B252.52EF06D7@twi.ch>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10c1fa,4dea4ca28f503351,start
X-Google-Attributes: gid10c1fa,public
X-Google-Thread: 10fd6d,4dea4ca28f503351,start
X-Google-Attributes: gid10fd6d,public
From: Andreas Raschle <ras@twi.ch>
Subject: JScrollPane: No ScrollBar with my JComponent
Date: 1998/01/30
Message-ID: <34D1B252.52EF06D7@twi.ch>
X-Deja-AN: 320520359
Content-Transfer-Encoding: 7bit
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: news@ubnnews.unisource.ch
X-Trace: ubnnews.unisource.ch 886154190 207 (None) 160.85.130.20
Organization: Unisource Business Networks
Newsgroups: comp.lang.java.gui,ch.comp