Hi,
ich habe ein kleines performance Problem beim manuellen resizen eines JWindow. Beim resizen des Fensters "flackert" es oder besser gesagt es wird zu langsam neu gezeichnet. Vielleicht fällt jemandem von euch ja eine Lösung ein.
TESTKLASSE
RESIZEABLE_WINDOW
LG
Hemeroc
ich habe ein kleines performance Problem beim manuellen resizen eines JWindow. Beim resizen des Fensters "flackert" es oder besser gesagt es wird zu langsam neu gezeichnet. Vielleicht fällt jemandem von euch ja eine Lösung ein.
TESTKLASSE
Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
public class TestResizeableWindow {
public static ResizeableWindow resizeableWindow;
public static void main(String[] args) {
JPanel jPanel = new JPanel();
JButton exitButton = new JButton("exit");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestResizeableWindow.resizeableWindow.dispose();
}
});
jPanel.add(exitButton);
resizeableWindow = new ResizeableWindow(
BorderFactory.createLineBorder(Color.RED, 5),
jPanel
);
resizeableWindow.setSize(new Dimension(300, 400));
resizeableWindow.setLocationRelativeTo(null);
resizeableWindow.setVisible(true);
}
}
RESIZEABLE_WINDOW
Java:
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Arrays;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class ResizeableWindow extends JWindow {
private static final long serialVersionUID = 1704588697579741996L;
private boolean _resizeable = true;
private Border _resizeBorder;
private JPanel _moveJPanel;
private final ResizeListener _resizeListener = new ResizeListener (this);
public boolean isResizeable() {
return this._resizeable;
}
public void setResizeable(boolean resizeable) {
this._resizeable = resizeable;
if (!this._resizeable) {
this.removeMouseListener(this._resizeListener);
this.removeMouseMotionListener(this._resizeListener);
this.setCursor(Cursor.getDefaultCursor());
} else {
if (!Arrays.asList(this.getMouseListeners()).contains(this._resizeListener)) {
this.addMouseListener(this._resizeListener);
}
if (!Arrays.asList(this.getMouseMotionListeners()).contains(this._resizeListener)) {
this.addMouseMotionListener(this._resizeListener);
}
}
}
public ResizeableWindow (Border resizeBorder, JPanel jPanel) {
this._resizeBorder = resizeBorder;
this._moveJPanel = jPanel;
this.rootPane.setBorder(this._resizeBorder);
this.add(this._moveJPanel);
this.setResizeable(true);
}
private class ResizeListener extends MouseAdapter implements MouseMotionListener {
private static final byte NO_RESIZE = 0;
private static final byte RESIZE_NORTH = 4;
private static final byte RESIZE_EAST = 1;
private static final byte RESIZE_SOUTH = 8;
private static final byte RESIZE_WEST = 2;
private byte _resizeDirection = ResizeListener.NO_RESIZE;
private final JWindow _jWindow;
private Rectangle tempBounds = new Rectangle();
public ResizeListener(JWindow jWindow) {
this._jWindow = jWindow;
}
public void mousePressed(MouseEvent mouseEvent) {
this._resizeDirection = getResizeDirection(mouseEvent);
}
public void mouseReleased(MouseEvent mouseEvent) {
this._resizeDirection = ResizeListener.NO_RESIZE;
}
public void mouseMoved(MouseEvent mouseEvent) {
short direction = getResizeDirection(mouseEvent);
this._jWindow.setCursor(getCursorByDirection(direction));
}
public void mouseDragged(MouseEvent mouseEvent) {
Rectangle bounds = this._jWindow.getBounds(tempBounds);
Point mousePosition = mouseEvent.getPoint();
SwingUtilities.convertPointToScreen(mousePosition, this._jWindow);
if ((this._resizeDirection & ResizeListener.RESIZE_EAST) != 0) {
bounds.width = mouseEvent.getX();
} else if ((this._resizeDirection & ResizeListener.RESIZE_WEST) != 0) {
bounds.width += bounds.x - mousePosition.x;
bounds.x = mousePosition.x;
}
if ((this._resizeDirection & ResizeListener.RESIZE_SOUTH) != 0) {
bounds.height = mouseEvent.getY();
} else if ((this._resizeDirection & ResizeListener.RESIZE_NORTH) != 0) {
bounds.height += bounds.y - mousePosition.y;
bounds.y = mousePosition.y;
}
if (this._jWindow.getMinimumSize().height > bounds.height) {
bounds.y = this._jWindow.getBounds().y;
bounds.height = this._jWindow.getMinimumSize().height;
}
if (this._jWindow.getMinimumSize().width > bounds.width) {
bounds.x = this._jWindow.getBounds().x;
bounds.width = this._jWindow.getMinimumSize().width;
}
this._jWindow.setBounds(bounds);
this._jWindow.validate();
this._jWindow.repaint();
}
private byte getResizeDirection(MouseEvent mouseEvent) {
byte resizeDirection = ResizeListener.NO_RESIZE;
Dimension componentDimension = this._jWindow.getSize();
Point mousePosition = mouseEvent.getPoint();
Insets insets = this._jWindow.getRootPane().getInsets();
if (mousePosition.x <= insets.left) {
resizeDirection |= ResizeListener.RESIZE_WEST;
} else if (mousePosition.x >= componentDimension.width - insets.right) {
resizeDirection |= ResizeListener.RESIZE_EAST;
}
if (mousePosition.y <= insets.top) {
resizeDirection |= ResizeListener.RESIZE_NORTH;
} else if (mousePosition.y >= componentDimension.height - insets.bottom) {
resizeDirection |= ResizeListener.RESIZE_SOUTH;
}
return resizeDirection;
}
private Cursor getCursorByDirection(int direction) {
int cursorType = Cursor.DEFAULT_CURSOR;
switch (direction) {
case ResizeListener.RESIZE_NORTH:
cursorType = Cursor.N_RESIZE_CURSOR;
break;
case (ResizeListener.RESIZE_NORTH | ResizeListener.RESIZE_EAST):
cursorType = Cursor.NE_RESIZE_CURSOR;
break;
case ResizeListener.RESIZE_EAST:
cursorType = Cursor.E_RESIZE_CURSOR;
break;
case (ResizeListener.RESIZE_SOUTH | ResizeListener.RESIZE_EAST):
cursorType = Cursor.SE_RESIZE_CURSOR;
break;
case ResizeListener.RESIZE_SOUTH:
cursorType = Cursor.S_RESIZE_CURSOR;
break;
case (ResizeListener.RESIZE_SOUTH | ResizeListener.RESIZE_WEST):
cursorType = Cursor.SW_RESIZE_CURSOR;
break;
case ResizeListener.RESIZE_WEST:
cursorType = Cursor.W_RESIZE_CURSOR;
break;
case (ResizeListener.RESIZE_NORTH | ResizeListener.RESIZE_WEST):
cursorType = Cursor.NW_RESIZE_CURSOR;
break;
default:
cursorType = Cursor.DEFAULT_CURSOR;
break;
}
return Cursor.getPredefinedCursor(cursorType);
}
}
}
LG
Hemeroc