我有下面的代码,当用户拖动鼠标时绘制一个椭圆,但是我只想绘制一个圆而不是椭圆。
我已经尝试了一些东西,但还没有完全发挥作用,据我所知,它将圆圈放在一个矩形内,所以如果我能把它锁定所以它把它放在一个正方形内然后我应该得到什么我想要?代码可能不是最有效的,我只是想为我正在工作的项目写这个,要清楚这不是学校的功课或任何东西。如果我至少可以获得代码的这个圆圈方面,它将在我的项目中节省大量时间
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class DrawingBoardWithMatrix extends JFrame {
public static void main(String[] args) {
DrawingBoardWithMatrix drawingBoardWithMatrix = new DrawingBoardWithMatrix();
}
public DrawingBoardWithMatrix() {
this.setSize(600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new PaintSurface(), BorderLayout.CENTER);
this.setVisible(true);
}
private class PaintSurface extends JComponent {
ArrayList<Shape> shapes = new ArrayList<>();
Point startDrag, endDrag;
public PaintSurface() {
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
endDrag = startDrag;
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
Shape r = makeCircle(startDrag.x, startDrag.y, e.getX(), e.getY());
shapes.add(r);
startDrag = null;
endDrag = null;
repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
repaint();
}
});
}
private void paintBackground(Graphics2D g2){
g2.setPaint(Color.LIGHT_GRAY);
for (int i = 0; i < getSize().width; i += 10) {
Shape line = new Line2D.Float(i, 0, i, getSize().height);
g2.draw(line);
}
for (int i = 0; i < getSize().height; i += 10) {
Shape line = new Line2D.Float(0, i, getSize().width, i);
g2.draw(line);
}
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
paintBackground(g2);
g2.setStroke(new BasicStroke(2));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
for (Shape s : shapes) {
g2.setPaint(Color.BLACK);
g2.draw(s);
// g2.setPaint(colors[(colorIndex++) % 6]);
// g2.fill(s);
}
if (startDrag != null && endDrag != null) {
g2.setPaint(Color.LIGHT_GRAY);
Shape r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
g2.draw(r);
}
}
private Ellipse2D.Float makeCircle(int x1, int y1, int x2, int y2) {
return new Ellipse2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
}
答案 0 :(得分:1)
您的makeCircle()
绘制宽度为Math.abs(x1 - x2)
且高度为Math.abs(y1 - y2)
的椭圆。如果要绘制圆,则宽度和高度必须相等。有几种方法可以实现这一目标:
Math.abs(x1 - x2)
宽度和高度Math.abs(y1 - y2)
宽度和高度但是,我非常关注你的陈述“我现在没有时间正确学习Java” - 这个会回来咬你。如果您打算将编程作为一种严肃的爱好或潜在的工作,那么必须花费大量精力来正确理解您正在做的事情。