gpt4 book ai didi

Java图形使用fillRect绘制一条线

转载 作者:行者123 更新时间:2023-11-30 03:56:55 26 4
gpt4 key购买 nike

我想画一条线,但我不能使用“drawline”,只能使用“fillRect”。当我通过鼠标单击事件从用户那里读取一个点并放置一个矩形时,我有以下代码。

我需要做同样的事情,但是通过从 A 点到 B 点绘制矩形来绘制一条线。

这是我的代码:

package graphics_draw_rec;

import java.awt.*;
import java.awt.Point;
import javax.swing.*;
import java.awt.event.*;

public class DrawRectangleOnMouseClick extends JPanel{

MouseHandler mouseHandler = new MouseHandler();
Point p2 = new Point(0, 0);
boolean drawing;

public DrawRectangleOnMouseClick(){
this.setPreferredSize(new Dimension(500, 400));
this.addMouseListener(mouseHandler);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(p2.x, p2.y, 5, 5);
}

private class MouseHandler extends MouseAdapter {

public void mouseClicked(MouseEvent e) {
drawing = true;
p2 = e.getPoint();
repaint();
}

public void mouseReleased(MouseEvent e) {
drawing = false;
p2 = e.getPoint();
repaint();
}
}

public static void main(String[] args) {


JFrame f = new JFrame("Draw Rectangle On Mouse Click");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new DrawRectangleOnMouseClick());
f.pack();
f.setVisible(true);
}

}

最佳答案

这是比较基础的(我是这么说的,但是我在做这些事情的时候不得不查一下)。基本上,它的作用是为您提供一种方法,通过该方法您可以在 0 到 1 之间的给定系数(或时间)处沿直线找到点。

这个例子允许调整点之间的距离,系数越小,距离越小。例如,第一个图像使用 0.1d 的增量,而第二个图像使用 0.01d

@0.1 @0.01

对于这个例子,尝试大约0.05d来制作一个漂亮的“堵嘴”线;)

所有很酷的“数学东西”都发生在 Between 方法中......

public Point2D between(Point p1, Point p2, double time) {

double deltaX = p2.getX() - p1.getX();
double deltaY = p2.getY() - p1.getY();

double x = p1.getX() + time * deltaX;
double y = p1.getY() + time * deltaY;

return new Point2D.Double(x, y);

}

对于可运行的示例...

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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawLine {

public static void main(String[] args) {
new DrawLine();
}

public DrawLine() {
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 Point p1 = new Point(50, 50);
private Point p2 = new Point(150, 150);

public TestPane() {
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
for (double t = 0; t < 1; t += 0.01) {

Point2D p = between(p1, p2, t);
g2d.fillRect((int)p.getX(), (int)p.getY(), 5, 5);

}
g2d.dispose();
}

public Point2D between(Point p1, Point p2, double time) {

double deltaX = p2.getX() - p1.getX();
double deltaY = p2.getY() - p1.getY();

double x = p1.getX() + time * deltaX;
double y = p1.getY() + time * deltaY;

return new Point2D.Double(x, y);

}
}

}

关于Java图形使用fillRect绘制一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22930952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com