We are experiencing problems with making our own scrolling class and
putting them into a ScrollPane.The program below does not draw
anything in the paintComponent procedure. It does not create scroll
bars
here is the code
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
class PolygonProgram
{
static Polygon Poly;
static int x=0;
static int y=0;
static int[] x1=new int[100];
static int[] y1=new int[100];
static int[] x2;
static int[] y2;
static int i=0;
static int j=0,k=0;
static String st;
static String[] point=new String[100];
static int height=600;
static int width=600;
static BufferedReader br;
static Dimension Size = new Dimension(50,50);
static class PolygonScrolling extends JPanel implements Scrollable
{
public int heighttomove;
public int widthtomove;
public Dimension getPreferredScrollableViewportSize()
{
int maxx,maxy;
maxx = 0; maxy = 0;
for (int m=0;m<x2.length-1;m++)
{
if(x2[m]>maxx)
{
maxx = x2[m];
}
if(y2[m]>maxy)
{
maxy = y2[m];
}
}
return new Dimension(maxx,maxy);
}
public int getScrollableUnitIncrement(Rectangle visibleRect,int
orientation,int direction)
{
if(direction==SwingConstants.VERTICAL)
{
if(direction>0)
{
for(int m=0;m<y1.length;m++)
{
if(height>y1[m])
{
height=y1[m];
heighttomove=height;
}
}
}
}
if(direction==SwingConstants.VERTICAL)
{
if(direction<0)
{
for(int m1=0;m1<y1.length;m1++)
{
if(height<y1[m1])
{
height=y1[m1];
heighttomove=height;
}
}
}
}
if(direction==SwingConstants.HORIZONTAL)
{
if( direction>0)
{
for(int m2=0;m2<y1.length;m2++)
{
if(width>x1[m2])
{
width=x1[m2];
heighttomove=width;
}
}
}
}
if(direction==SwingConstants.HORIZONTAL)
{
if(direction<0)
{
for(int m3=0;m3<y1.length;m3++)
{
if(width<x1[m3])
{
width=x1[m3];
heighttomove=width;
}
}
}
}
return (heighttomove);
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int
orientation, int direction)
{
if(direction==SwingConstants.VERTICAL)
{
if(direction>0)
{
for(int m=0;m<y1.length;m++)
{
if(height>y1[m])
{
height=y1[m];
widthtomove=4*height;
}
}
}
}
if(direction==SwingConstants.VERTICAL)
{
if(direction<0)
{
for(int m1=0;m1<y1.length;m1++)
{
if(height<y1[m1])
{
height=y1[m1];
widthtomove=4*height;
}
}
}
}
if(direction==SwingConstants.HORIZONTAL)
{
if( direction>0)
{
for(int m2=0;m2<y1.length;m2++)
{
if(width>x1[m2])
{
width=x1[m2];
widthtomove=4*width;
}
}
}
}
if(direction==SwingConstants.HORIZONTAL)
{
if(direction<0)
{
for(int m3=0;m3<y1.length;m3++)
{
if(width<x1[m3])
{
width=x1[m3];
widthtomove=4*width;
}
}
}
}
return (widthtomove);
}
public boolean getScrollableTracksViewportWidth()
{
return false;
}
public boolean getScrollableTracksViewportHeight()
{
return false;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawPolygon(Poly);
}
}
public static void main(String [] args)
{
Poly=new Polygon();
try
{
br = new BufferedReader(new FileReader("points.in"));
}
catch(FileNotFoundException e1)
{
System.out.println("file not found");
}
try
{
String s = br.readLine();
while(s!=null)
{
StringTokenizer P = new StringTokenizer(s);
st=P.nextToken();
x=Integer.parseInt(st);
x1[i]=x;
i++;
st=P.nextToken();
y=Integer.parseInt(st);
y1[j]=y;
j++;
point[k]=x+","+y;
k++;
Poly.addPoint(x,y);
s=br.readLine();
}
x2=new int[i];
y2=new int[j];
for(int m=0;m<i;m++)
{
x2[m]=x1[m];
y2[m]=y1[m];
}
}
catch(IOException e)
{
System.out.println("no points");
}
Arrays.sort(x1);
Arrays.sort(y2);
PolygonScrolling Ps = new PolygonScrolling();
JFrame frame = new JFrame();
frame.setSize(300,300);
JScrollPane sp = new JScrollPane(Ps,ScrollPaneConstants.
VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.
HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setSize(200,200);
sp.setLocation(40,40);
frame.getContentPane().add(sp);
frame.pack();
frame.validate();
frame.setVisible(true);
}
}
pit.grinja@gmx.de - 12 Apr 2006 10:39 GMT
> We are experiencing problems with making our own scrolling class and
> putting them into a ScrollPane.The program below does not draw
> anything in the paintComponent procedure. It does not create scroll
> bars
> static class PolygonScrolling extends JPanel implements Scrollable
> {
[quoted text clipped - 18 lines]
> return new Dimension(maxx,maxy);
> }
The method getPreferredScrollableViewportSize() does not tell you
anything about the size of the JPanel! It just defines the size of the
"peephole" through which you look on your panel. Thus, the viewport is
sized appropriately, but since the preferredSize of your panel is (0,0)
you don´t see anything. When you want to place a custom-made JPanel or
JComponent in a JScrollPane, put the logic for calculating the size in
an overriden "getPreferredSize()" method and simply call
"getPreferredSize()" from "getPreferredScrollableViewportSize()".
Note that a Polygon with coordinats (0,0),(0,1),(1,1) and (1,0) has a
width and height of two pixels, not one. The Dimension returned by
"getPreferredSize()" (with the calculations that you placed in
"getPreferredScrollableViewportSize()") should thus be
"(maxx+1,maxy+1)".
> }
HTH, Piet