java绘制矩形的方法不是两种方式 [英] java draws rectangle one way not both
问题描述
嗨我在java中有一些绘制矩形的代码,但是它只会向右拖动,即使我向左拖动也向右拖动,这里有代码我有什么帮助吗?
hi i have some code in java that draws a rectangle, hwever itll only drag right, even if i drag left itll drag to the right, here is the code i have any help please?
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
int width = Math.max(selection.x - e.getX(), e.getX() - selection.x);
int height = Math.max(selection.y - e.getY(), e.getY() - selection.y);
selection.setSize(width, height);
repaint();
推荐答案
请记住,矩形
(和图形#fillRect
和图形#drawRect
)将不会呈现负宽度的矩形/高度
Remember, Rectangle
(and Graphics#fillRect
and Graphics#drawRect
) won't render rectangles with negative width/heights
你需要两件事......
You need two things...
- 当前鼠标点(或者在你的情况下拖动点)
- 首次按下鼠标的点(锚点或原点)
您应该从 mousePressed
事件获得锚点...
You should get the anchor point from the mousePressed
event...
public void mousePressed(MouseEvent e) {
clickPoint = new Point(e.getPoint());
}
然后您需要确定哪个点最小并将其用作开始,哪个是最大的,并使用维度。
You then need to make determinations about which point is the smallest and use that as the start and which is the largest and use those for dimensions.
public void mouseDragged(MouseEvent e) {
int minX = Math.min(e.getX(), clickPoint.x);
int minY = Math.min(e.getY(), clickPoint.y);
int maxX = Math.max(e.getX(), clickPoint.x);
int maxY = Math.max(e.getY(), clickPoint.y);
selection.x = minX;
selection.y = minY;
selection.width = maxX - minX;
selection.height = maxY - minY;
repaint();
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SelectionExample {
public static void main(String[] args) {
new SelectionExample();
}
public SelectionExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Rectangle selection = new Rectangle();
private Point clickPoint;
public TestPane() {
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int minX = Math.min(e.getX(), clickPoint.x);
int minY = Math.min(e.getY(), clickPoint.y);
int maxX = Math.max(e.getX(), clickPoint.x);
int maxY = Math.max(e.getY(), clickPoint.y);
selection.x = minX;
selection.y = minY;
selection.width = maxX - minX;
selection.height = maxY - minY;
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
clickPoint = new Point(e.getPoint());
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (selection.width > 0 && selection.height > 0) {
g2d.setColor(new Color(0, 0, 255, 64));
g2d.fill(selection);
g2d.setColor(Color.BLUE);
g2d.draw(selection);
}
g2d.dispose();
}
}
}
这篇关于java绘制矩形的方法不是两种方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!