need small help:
I have small GUI program: in which component are assembled in following order:
The main JFrame----contains JScrollPane-----Contains JPanel(1)-------Contains JPanel(2):
I have attached the mousemotionListner to JPanel(2): I have implemented paint method for JPanle 2 as:
@Override
public void paint(Graphics g) {
/*draw the image on this JPanel*/
g.drawImage(img, 0, 0, display_imageSizeX, display_imageSizeY, this);
/*set the x,y axis legend*/
g.setFont(new Font("Serif", Font.BOLD, 20));
g.setColor(Color.yellow);
g.drawString("Y", 10, 20);
g.drawString("X", display_imageSizeX - 20, display_imageSizeX - 10);
/*paint the border for Jpanel*/
this.paintBorder(g);
}
Now i want to provide the functionality in which i can dragged the JPanel2 in JscrollPane:::i could able to implement the method but the problem is when i started dragging the JPanel2 1st time it flashes and it appears that JPanel2 is painted two times:although the final result is what i expected (proper dragging is happening, but my problem is with flashing of components)
here is my implementation of dragging
public void mousePressed(MouseEvent me) {
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
this.jvpLocationPre = this.scrVP.getLocation();
this.mouseX = me.getX();
this.mouseY = me.getY();
}
public void mouseDragged(MouseEvent me) {
/*calculate the new position for viewPort*/
int newPositionX = this.jvpLocationPre.x + (this.mouseX - me.getX());
int newPositionY = this.jvpLocationPre.y + (this.mouseY - me.getY());
//prevent going over the borders. maximum=picture width minus scroll pane view width
int maxX = this.getWidth() - scrVP.getWidth();
int maxY = this.getHeight()- scrVP.getHeight();
if (newPositionX > maxX) {
newPositionX = maxX;
}
if (newPositionY > maxY) {
newPositionY = maxY;
}
if (newPositionX < 0) {
newPositionX = 0;
}
if (newPositionY < 0) {
newPositionY = 0;
}
/*set the new Position for viewPort*/
scrVP.setViewPosition(new Point(newPositionX, newPositionY));
/*store the new values as old*/
this.jvpLocationPre = scrVP.getViewPosition();
}
any suggestions?

New Topic/Question
Reply



MultiQuote







|