drawrect java_java-了解Graphics 2D以及旋转,drawRect方法

出于好奇,我做了这样的事情.

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionAdapter;

import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class RectanglePanel extends JPanel{

private Point anchorPoint = null;

private Point intermediatePoint = null;

private Point finalPoint = null;

public RectanglePanel(){

addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent me){

if(anchorPoint == null){

// first click, set anchor point

anchorPoint = me.getPoint();

}else if(finalPoint == null){

// second click, set final point

finalPoint = me.getPoint();

}else{

// third click, reset clicks, anchor point, intermediate point and final point

anchorPoint = null;

finalPoint = null;

intermediatePoint = null;

}

repaint();

}

});

addMouseMotionListener(new MouseMotionAdapter() {

@Override

public void mouseMoved(MouseEvent me){

if(anchorPoint != null && finalPoint == null){

// mouse moved

// set intermediate point if anchor point is set and final point is not set yet

intermediatePoint = me.getPoint();

repaint();

}

}

});

}

@Override

protected void paintComponent(Graphics g){

super.paintComponent(g);

if(anchorPoint != null){

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.red);

Point p = finalPoint != null ? finalPoint : intermediatePoint;

if(p != null && !p.equals(anchorPoint)){

// final point or intermediate point is set, and is not same as anchor point

// draw square

// calculate angle to rotate canvas

double angle = -Math.toRadians(45) + Math.atan2(p.y - anchorPoint.y, p.x - anchorPoint.x);

// width of square, calculated using distance formaula and pythagorus theorem

// distance formula: distance = sqrt((x1-x2)^2 + (y1-y2)^2)

// pythagorus for right angled triangle: c^2 = a^2 + b^2

double width = Math.sqrt(((p.x - anchorPoint.x) * (p.x - anchorPoint.x) + (p.y - anchorPoint.y) * (p.y - anchorPoint.y)) / 2.0);

// set origin to anchorpoint

g2d.translate(anchorPoint.x, anchorPoint.y);

// rotate canvas

g2d.rotate(angle);

Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, width);

// draw square

g2d.draw(rectangle2D);

// rotate back canvas

g2d.rotate(-angle);

// reset back origin

g2d.translate(-anchorPoint.x, -anchorPoint.y);

}else{

g2d.drawRect(anchorPoint.x, anchorPoint.y, 1, 1);

}

}

}

public static void main(String [] args){

final JFrame frame = new JFrame("Rectangle Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 400);

frame.getContentPane().add(new RectanglePanel());

SwingUtilities.invokeLater(new Runnable() {

public void run() {

frame.setVisible(true);

}

});

}

}

您可以像这样实现,以解决您的问题.让我知道这是您要寻找的吗?

脚步:

1)计算正方形的宽度.您有一些点,它们代表正方形的相对角.这两个点之间的距离是对角线的长度.因此,考虑两个点(x1,y1)和(x2,y2),使用距离公式,对角线的长度由下式给出:

diagonal_length * diagonal_length = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)

正方形和对角线的两侧将形成直角三角形.正方形的边长相等,让正方形的边成为边,然后使用毕达哥拉斯定理:

side * side + side * side = diagonal_length * diagonal_length

解决上面的两个方程,

side = Math.sqrt(((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) / 2.0);

2)计算旋转画布的角度,因此第二个点以第一个点为原点与x轴成45度角.

3)确定第一个点的原点.

4)旋转画布,第二点与x轴成45度角,第一点为原点.这将使正方形的两个侧面落在轴上,而其他两个侧面平行于轴平行,因此可以使用图形的draw方法绘制矩形/正方形.

5)从原点开始绘制正方形,其边长如上所述计算.

6)将画布反向旋转,以使其与旋转前相同.

7)将原点重设为设置原点之前的原点.

做完了!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值