java中绘图为什么画不出矩形,java绘制矩形的方法不是两种方式

请记住,Rectangle(和Graphics#fillRect和Graphics#drawRect)不会呈现负宽度/高度的矩形

你需要两件事……

>当前鼠标点(或您的案例中的拖动点)

>首次按下鼠标的位置(锚点或原点)

你应该从mousePressed事件获得锚点…

public void mousePressed(MouseEvent e) {

clickPoint = new Point(e.getPoint());

}

然后,您需要确定哪个点最小,并将其用作开始,哪个最大,并将其用于维度.

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();

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值