I have a JFrame in my Java application that contains a JPanel where I have some drawing objects created at run-time. The problem is while scrolling the JFrame for large figures the scrolling slows up and scroll bar does not move smoothly. Please note I am using Graphics 2D object and doing repaint on scroll action.
Is there any way of smoothing the scrolling action of JFrame.
Here is some part of the code
public class DiagramPanel implements MouseListener{
int click=0;
Point p1;
Point p2;
private Dimension panelDimension;
.... // variables
public void go() {
p1 = new Point();
p2 = new Point();
JFrame f = new JFrame();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(1200,500);
panelx = new DiaPanel();
panelx.setOpaque(true);
panelx.setBackground(Color.white);
panelx.setAutoscrolls(true);
panelx.addMouseListener(this);
JScrollPane scrollPane = new JScrollPane();
// scrollPane.add(panelx);
ClassRectangle tempRect = null;
for (ClassRectangle rect : this.classRectangles) {
tempRect = rect;
}
Rectangle rect = new Rectangle();
rect.setBounds(tempRect.getW() - 100, 0, 1000,
tempLife.getEndpointY() * 500);
panelDimension = new Dimension(0,0);
for (ClassRectangle rectx : classRectangles){
panelDimension.width=rectx.getW()+300;
}
for (LifeLine life : lifeLines) {
panelDimension.height=life.getEndpointY()+300;
}
scrollPane.setViewportView(panelx);
panelx.computeVisibleRect(rect);
JScrollPane scrollPane1 = new JScrollPane(panelx);
panelx.setPreferredSize(panelDimension);
panelx.repaint();
panelx.revalidate();
p1.x=0;
p1.y=0;
p2.y=panelDimension.height;
p2.x=panelDimension.width;
f.add( scrollPane1);
scrollPane.revalidate();
f.setBackground(Color.white);
}
public DiagramPanel(ArrayList<Rectangle> classRectangles,
ArrayList<Pair> pairs, ArrayList<Line> lines,
ArrayList<Life> meth) {
// constructing obj of DrawingPanel Here
}
public class SeqDiaPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d2 = (Graphics2D) g;
g2d2.setColor(Color.orange);
//grid
for (int i = 0; i < panelDimension.height; i++) {
g2d2.drawLine(0, 0 + i * 5, panelDimension.width+1000, 0 + i * 5);
}
for (int i = 0; i < panelDimension.width; i++) {
g2d2.drawLine(0 + i * 5, 0, 0 + i *5,panelDimension.height+300);
}
g2d2.setColor(Color.black);
// objects
.......... some objects here
}
}
// draw Lines
Stroke drawingStroke = new BasicStroke(2, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[] { 5 }, 0);
// Stroke drawingStroke = new BasicStroke();
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(drawingStroke);
for (Line life : lines) {
g2d.drawLine(life.getStartpointX(), life.getStartpointY(),
life.getEndpointX(), life.getEndpointY());
panelDimension.height=life.getEndpointY()+300;
}
// draw methodLfe
for (Object2 ml1 : Obj2) {
g2d2.fill3DRect(ml1.StartX(), ml1.getMethodStartY(),
ml1.getBreadth(), ml1.getEndX(),true);
}
}
}
// tobeused
public int calculateWidth(String name){
Font font = new Font("Serif", Font.BOLD, 12);
FontMetrics metrics = new FontMetrics(font){
/**
*
*/
private static final long serialVersionUID = 1L;};
int tempInt2=SwingUtilities.computeStringWidth( metrics, name);
tempInt2=tempInt2+10;
return tempInt2;
}
/*public class MouseClick implements MouseListener{
Point p = new Point(0,0);
@Override
public void mouseClicked(MouseEvent evnt) {
p.x=evnt.getX();
p.y=evnt.getY();
System.out.println("MouseClicked @"+p.x+":"+p.y);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}*/
@Override
public void mouseClicked(MouseEvent evnt) {
click++;
if(click==1){
//Point p= new Point();
p1.x=evnt.getX();
p1.y=evnt.getY();
// System.out.println("MouseClicked1 @"+p1.x+":"+p1.y);
}
if(click==2){
p2.x=evnt.getX();
p2.y=evnt.getY();
//System.out.println("MouseClicked2 @"+p2.x+":"+p2.y);
click=0;
if(p1.x<p2.x&&p1.y<p2.y){
panelx.repaint();
}
else{
}
}/*else{
p1.x=0;
p1.y=0;
p2.x=panelDimension.width+500;
p2.y=panelDimension.height+700;
}*/
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}