Repaint JComponent on res
To: comp.lang.java.gui
Hi,
Hope someone can help.
I'm drawing in a JComponent.
Initially the JComponent is sized at 200 x 200
and an origin point is set at getWidth()/2,
and getHeight()/2. A cross is drawn at the origin.
Now, I use the mouse to move the origin
(hence the cross drawn at the origin) to approx.
3 * getWidth()/4, and 3 * getHeight()/4. Fine.
Now, I wish to resize the JComponent and have the
cross drawn at the same relative location in the
resized component as it was in the old sized component.
i.e.
old location = 75% width, 75% height of old component size.
new location = 75% width, 75% height of new component size.
The origin may be outside the visible area of the component,
for instance it may be located at -10X, 500Y because I will
be drawing more things that just this example cross.
It seems like such a simple thing but I can't seem to get
a before ComponentResize size and an after ComponentResize size
at the same time so that I can compare them.
Thanks for any help.
BV
package scratch;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
public class Scratch
extends JComponent {
private static final long
serialVersionUID = 1L;
boolean unPainted = true;
private int originXOffset;
private int originYOffset;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame f = new JFrame("Scratch");
Scratch scratch = new Scratch();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(scratch);
f.pack();
f.setVisible(true);
}
public Scratch() {
setPreferredSize(new Dimension(200, 200));
addComponentListener(resizer);
addMouseListener(mouser);
addMouseMotionListener(mouser);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (unPainted) {
originXOffset = getWidth()/2;
originYOffset = getHeight()/2;
unPainted = false;
}
Color startingColor = g2.getColor();
g2.setColor(Color.RED);
g2.drawLine(
originXOffset - 10, originYOffset,
originXOffset + 10, originYOffset);
g2.drawLine(
originXOffset, originYOffset - 10,
originXOffset, originYOffset + 10);
g2.setColor(startingColor);
}
private ComponentListener resizer =
new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
// TODO Shift originXOffset and originYOffset
// so that the cross is drawn at the same relative
// location in the newly sized component.
}
};
private MouseInputAdapter mouser =
new MouseInputAdapter() {
int startX;
int startY;
boolean inDrag = false;
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
startX = e.getX();
startY = e.getY();
inDrag = true;
}
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
inDrag = false;
}
@Override
public void mouseDragged(MouseEvent e) {
int currentX = e.getX();
int currentY = e.getY();
if (currentX < startX) {
originXOffset =
originXOffset - (startX - currentX);
startX = currentX;
}
else {
originXOffset =
originXOffset + (currentX - startX);
startX = currentX;
}
if (currentY < startY) {
originYOffset =
originYOffset - (startY - currentY);
startY = currentY;
}
else {
originYOffset =
originYOffset + (currentY - startY);
startY = currentY;
}
if (inDrag) {
repaint();
}
}
};
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24