-
scrollbar issue
Hello,
I do have a jpanel to which I am adding a scrollpane. Everything is working fine till that point. I do have a set of codes which draws vertical lines and if I do move the scollbar then nothing happens.
Here comes my issue. I am also drawing some horizontal lines. When I drag the knob of the scrollbar the horizontal lines get erased leaving the vertical lines alone.
I tried my best to figure out the issue but couldn't solve it. Any help would be appreciated.
I have also attached a partial code along with this thread.
Thanks in advance
gemi
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* DrawCanvas_UI.java
*
* Created on Mar 13, 2009, 2:27:13 PM
*/
package DrawCanvas_UI;
//import DrawCanvas_UI.Drawlines;
import interaction.*;
import java.awt.Adjustable;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.ScrollPane;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JPanel;
/**
*
* @author arun
*/
public class DrawCanvas_UI extends javax.swing.JFrame {
Driver drive = new Driver();
BAP bap = new BAP();
static int processmax ;
static int eventmax ;
Vector vSrc = new Vector();
Vector vDest = new Vector();
Vector vEvent = new Vector();
ScrollPane scroller;
public static int MAX_Horzspace = 10; // horizontal spacing between events
public static final int Unit_Const1 = 1; // p1 to p0
public static final int Unit_Const2 = 10;
public static final int Unit_Const_pi = 5;
float stroke = 0;
drawArrow arrow = new drawArrow();
MyAdjustmentListener listener = new MyAdjustmentListener();
/** Creates new form DrawCanvas_UI */
public DrawCanvas_UI() {
// Code to initiate the driver class so that it opens the file does all processing
// Driver drive = new Driver();
try{
drive.filesplit();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
initComponents();
scroller = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
scroller.add(new DrawCanvas());
Adjustable vadjust = scroller.getVAdjustable();
Adjustable hadjust = scroller.getHAdjustable();
hadjust.setUnitIncrement(10);
vadjust.setUnitIncrement(10);
scroller.getHAdjustable().addAdjustmentListener(listener);
scroller.getVAdjustable().addAdjustmentListener(listener);
scroller.setSize(925,740); // Here, the drawingPanel is set larger than the size of the JFrame which
//contains it.
add("Center", scroller);
pack(); // pack resizes the Window to the minimum size to satisfy the preferred size of each of the components in the layout
}
private void BrowseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// TODO add your handling code here:
//Code to perform input dialog event
InputDialog id = new InputDialog();
int n = id.incrementHorzSize();
MAX_Horzspace = n;
System.out.println(MAX_Horzspace);
if (MAX_Horzspace != 10)
{
// repaint();
DrawCanvas_UI dr = new DrawCanvas_UI();
dr.drawPanel.repaint();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DrawCanvas_UI().setVisible(true);
}
});
}
//code to draw vertical lines
class DrawCanvas extends JPanel{
scrollwindow scroll = new scrollwindow(); // this defines the size of the panel so that the scrollbar knows how much to increment
int scrollh = scroll.getScrollhorizontal();
Vector vPoint1 = new Vector();
Vector vPoint2 = new Vector();
// Point p = new Point();
private int xa;
private int xb;
private int ya = 50;
private int yb ;
DrawCanvas()
{
}
public Dimension getPreferredSize() {
// return new Dimension(10200, 6000);
return new Dimension(scrollh, 6000);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
processmax = bap.maxProcess(); // to find the maximum no of process
// code to find the source , dest , event to find a string and receive match
vSrc = bap.getMyVectorSrc();
vDest = bap.getMyVectorDest();
vEvent = bap.getMyVectorEvent();
// This code is used to draw the vertical lines
int width = (925/processmax) ;//10=processmax
// System.out.println(processmax);
if(width < 60)
{
width = 70;
}
int height = 1690;
int x1 = 30 ; //left
int y1 = 50; //top --- Constant value
int x2 = 30 ; //right
int y2 = height; //bottom
int y3 = y1-Unit_Const2; //for process no. (Ex:P1)
int x3 = x1-Unit_Const_pi; // x-cords for pi
for (int i = 0;i <= processmax;i++)
{
vPoint1.add(new Point(x1,y1)); // store the x1,y1 in a vector
vPoint2.add(new Point(x2,y2)); // store x2,y2 in a vector
g2.setColor(Color.BLACK);
g2.drawLine(x1, y1, x2, y2); // cordinates to draw individual vertical lines
g2.setColor(Color.BLUE);
g2.drawString("P"+i,x3 , y3);
x1 = (x1 + width);
// x2 = (x2 + width);
x2 = x1;
x3 = (x1 - Unit_Const2);
}
code to draw horizontal lines
for ( int i =0; i<vSrc.size();i++)
{
//these values are got from another class
short vertexSrc = (Short)vSrc.elementAt(i);
int vertexDest = (Short)vDest.elementAt(i);
int vertexEvent = (Integer)vEvent.elementAt(i);
if(vertexSrc < vertexDest)
{
Point p1 = (Point)vPoint1.elementAt(vertexSrc);
Point p2 = (Point)vPoint2.elementAt(vertexDest);
xa = p1.x;
xb = p2.x - Unit_Const1;
ya = ya + MAX_Horzspace;
yb = ya;
}
else
{
Point p1 = (Point)vPoint1.elementAt(vertexSrc);
Point p2 = (Point)vPoint2.elementAt(vertexDest);
xa = p1.x;
xb = p2.x + Unit_Const1;
ya = ya + MAX_Horzspace ;
yb = ya;
}
arrow.drawArrow(g2,xa,ya,xb,yb,stroke);
}
}
}
public class MyAdjustmentListener implements AdjustmentListener{
DrawCanvas drc = new DrawCanvas();
// This method is called whenever the value of a scrollbar is changed,
// either by the user or programmatically.
public void adjustmentValueChanged(AdjustmentEvent evt) {
Adjustable source = evt.getAdjustable();
// getValueIsAdjusting() returns true if the user is currently
// dragging the scrollbar's knob and has not picked a final value
if (evt.getValueIsAdjusting()) {
DrawCanvas_UI dr = new DrawCanvas_UI();
// dr.drawPanel.repaint();
dr.drawPanel.validate();
return;
}
int orient = source.getOrientation();
if (orient == Adjustable.HORIZONTAL) {
// System.out.println("from horizontal scrollbar");
} else {
// System.out.println("from vertical scrollbar");
}
}
}
}
-
This is a multi-posted thread that has already been posted in the New to Java section. I will close this thread and ask the original poster not to multi-post.