java swing rectangle,Java,Swing,Awt - 在圖像上繪制一個矩形並使其可見

I'm trying to open an image, to draw a rectangle on it (mouse DraggedEvent) while mouse is dragged and to draw the final rectangle when the mouse is released.

我正在嘗試打開一個圖像,在拖動鼠標時在其上繪制一個矩形(鼠標DraggedEvent),並在釋放鼠標時繪制最終的矩形。

I achieve to draw the rectangle while dragging but I'm not able at the moment to collect the coordinates of the final point to draw the rectangle and to stock his data. Do you have any ideas to improve my code ? (Add an image in the background, that keeps rectangles on it even when resized even if several are drawn on it)

我實現在拖動時繪制矩形,但我現在無法收集最終點的坐標來繪制矩形並存儲他的數據。您有什么想法來改進我的代碼嗎? (在背景中添加圖像,即使調整了大小,也會在其上保留矩形,即使在其上繪制了多個圖像)

I guess mousReleased Event could help me, but I'm not sure how I should use it.

我想mousReleased Event可以幫助我,但我不確定如何使用它。

Edit : After a first rectangle being painted, I want it to remain visible if I draw a second one.

編輯:在繪制第一個矩形后,如果我繪制第二個矩形,我希望它保持可見。

package selectZone;

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

public class Rec extends JFrame {

public Rec() {

super("rectangle");

setPreferredSize(new java.awt.Dimension(400, 400));

setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel pane = new Pan();

add(pane);

pack();

setVisible(true);

}

public static void main(String[] args) {

new Rec();

}

}

class Pan extends JPanel {

private Point origin;

private Point end;

private Point endFinal;

public Pan() {

MouseAdapter adapter = new Lis();

addMouseMotionListener(adapter);

addMouseListener(adapter);

}

class Lis extends MouseAdapter {

public void mouseDragged(MouseEvent e) {

end = e.getPoint();

repaint();

}

public void mousePressed(MouseEvent e) {

origin = e.getPoint();

}

public void mouseReleased(MouseEvent e, Graphics g) {

endFinal = e.getPoint();

g.clearRect(0, 0, getWidth(), getHeight());

g.setColor(Color.RED);

int x1 = (int) (origin.getX());

int y1 = (int) (origin.getY());

int x2 = (int) (endFinal.getX());

int y2 = (int) (endFinal.getY());

g.drawRect(x1, y1, x2 - x1, y2 - y1);

}

}

public void paint(Graphics g) {

g.clearRect(0, 0, getWidth(), getHeight());

g.setColor(Color.RED);

if (origin != null) {

int tmp;

int x1 = (int) (origin.getX());

int y1 = (int) (origin.getY());

int x2 = (int) (end.getX());

int y2 = (int) (end.getY());

if (x1 > x2) {

tmp = x1;

x1 = x2;

x2 = tmp;

}

if (y1 > y2) {

tmp = y1;

y1 = y2;

y2 = tmp;

}

g.drawRect(x1, y1, x2 - x1, y2 - y1);

}

}

}

Thanks for your time guys.

謝謝你的時間。

1 个解决方案

#1

1

I'm trying to open an image, to draw a rectangle on it

我正在嘗試打開圖像,在其上繪制一個矩形

After a first rectangle being painted, I want it to remain visible if I draw a second one.

在繪制第一個矩形后,如果我繪制第二個矩形,我希望它保持可見。

Keep an ArrayList of Rectangles to paint. Then in the paintComponent() method of your component that displays the image you iterate through the list to draw the rectangles.

保持要繪制的矩形的ArrayList。然后在組件的paintComponent()方法中顯示圖像,迭代列表以繪制矩形。

Check out DrawOnComponet example from Custom Painting Approaches for a working example to get you started.

查看自定義繪圖方法中的DrawOnComponet示例,以獲得一個工作示例,以幫助您入門。

The example just draws the rectangle, so you will also need to draw the image. Or you could extend JLabel, so the label paints the image and then you just draw the rectangles.

該示例僅繪制矩形,因此您還需要繪制圖像。或者您可以擴展JLabel,因此標簽繪制圖像然后您只繪制矩形。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值