私はJavaアプレットをプログラミングしています。
このアプレットでは、画像上にいくつかのマーカー(赤い円として)といくつかの線を描画する必要があります。
JComponentの拡張として、マークを正常に実装しました。また、これにいくつかのマウスリスナーを追加しました。
ラインオブジェクトに大きな問題があります。JComponentを拡張する別のオブジェクトを作成しましたが、座標系に問題があることを除けば、setDimensionによって問題が発生します。たとえば、すべてのマーカーのクリックをインターセプトします。
垂直線または水平線しか描画できないため、オブジェクトの「寸法」を線に近づける方法ではありません。
みなさん、ありがとうございました。
編集
public class Path extends JComponent {
...
// stroke of the line
private Stroke spessore = new BasicStroke(SPESSORE);
// coordinates
private double x, y, x_2, y_2;
// ZoomManager is an object. In this project I can zoom in and zoom out the
// image, so this object convert coordinates get on the superior JPanel in
// coordinates on the image real-sized.
public Path(double x, double y, ZoomManager zoom) {//, double x_2, double y_2, ZoomManager zoom) {
super();
// this function return the coordinates on the real-sized image
Point a = DrawableObjects.getScaledCoordinates(x, y, zoom);
this.x = a.x;
this.y = a.y;
this.x_2 = a.x;
this.y_2 = a.y;
updateBoundsAndSize(zoom);
// this was only for test...
this.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("CLICK!");
}
...
});
}
// this function is called during the mouse dragging for drow the line.
// it gets the coordinates, convert them, save them and update the bounds and
// size of the object
public void setArrivePoint(Point a, ZoomManager zoom) {
Point p = DrawableObjects.getScaledCoordinates(a.x, a.y, zoom);
this.x_2 = p.x;
this.y_2 = p.y;
updateBoundsAndSize(zoom);
}
// update the bounds of the object, the origin point of the rectangle is the
// top-left coordinate build with the original coordinates. The width and height of the rectangle are obtained by subtraction.
private void updateBoundsAndSize(ZoomManager zoom) {
Point p = DrawableObjects.getPanelCoordinates(x, y, zoom);
Point a = DrawableObjects.getPanelCoordinates(x_2, y_2, zoom);
int min_x = (int)Math.min(p.x, a.x) - SPESSORE;
int min_y = (int)Math.min(p.y, a.y) - SPESSORE;
if (min_x < 0)
min_x =0;
if (min_y < 0)
min_y = 0;
int w = (int) (Math.max(a.x, p.x) - min_x) + SPESSORE;
int h = (int) (Math.max(a.y, p.y) - min_y) + SPESSORE;
setBounds(new Rectangle(min_x, min_y, w, h));
repaint();
}
// drawing function
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D antiAlias = (Graphics2D) g;
antiAlias.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// get ZoomManager from the superior object
ZoomManager zoom = ((JPanelImmagine)this.getParent()).zoom;
antiAlias.setColor(DEFAULT_COLOR);
antiAlias.setStroke(spessore);
Point[] coordinates = updateCoordinates(zoom);
Line2D line = new Line2D.Double(coordinates[0], coordinates[1]);
antiAlias.draw(line);
}
// translate coordinates from superior jpanel to this object
private Point[] updateCoordinates(ZoomManager zoom) {
Point[] output = new Point[2];
Point p = DrawableObjects.getScaledCoordinates(x, y, zoom);
Point a = DrawableObjects.getScaledCoordinates(x_2, y_2, zoom);
double o_x = this.getBounds().getCenterX();
double o_y = this.getBounds().getCenterY();
Point origin = new Point ((int)o_x, (int)o_y);
output[0] = calculateCoordinates(p, origin);
output[1] = calculateCoordinates(a, origin);
return output;
}
private Point calculateCoordinates(Point p, Point origin) {
double new_x = p.x - origin.x;
double new_y = p.y - origin.y;
return new Point((int)new_x, (int)new_y);
}