gpt4 book ai didi

java - 如何在JPanel上绘图的其他类中创建方法

转载 作者:行者123 更新时间:2023-12-01 10:49:07 25 4
gpt4 key购买 nike

我的目标:创建一个Point类的方法(代码在底部),它允许我绘制多个点(以及将来的线),所以它看起来像这样:

Point pointA = new Point(0,0);
Point pointB = new Point(10,20);
pointA.draw(); // or probably: pointA.draw(someSurface);
pointB.draw();

我从tutorial获取代码并对其进行了修改。

我在寻找答案时发现了很多类似的问题,但要么没有一个能找到适合我的案例的答案,要么我无法实现它。

正如您在下面看到的,我尝试在 Point 类和 Surface 类内部创建该方法(注释代码),但没有成功。

问题是什么以及我应该在代码中修复什么?

主要:

public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
BasicGraphics ex = new BasicGraphics();
ex.setVisible(true);
Point pointA = new Point(0,0);
Point pointB = new Point(10,10);
pointA.draw(ex.currentSurface);
pointB.draw(ex.currentSurface);
}
});
}
}

基本图形:

public class BasicGraphics extends JFrame {
public Surface currentSurface;
public BasicGraphics() {
initUI();
}
private void initUI() {
currentSurface=new Surface();
add(currentSurface);
setTitle("My points");
setSize(100, 100);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

表面:

class Surface extends JPanel {

/*public void drawPoint(Graphics g, Point givenPoint) {
//I want to represent points as little circles
Ellipse2D circle = new Ellipse2D.Double();
Graphics2D g2d = (Graphics2D) g;
circle.setFrameFromCenter(givenPoint.x, givenPoint.y, 10,10);
g2d.draw(circle);
}*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
}

要点:

public class Point {
public double x;
public double y;
public Point(double newX, double newY){
x=newX;
y=newY;
}

public void draw(Surface givenSurface){
Graphics g = givenSurface.getGraphics();
Ellipse2D circle = new Ellipse2D.Double();
Graphics2D g2d = (Graphics2D) g;
circle.setFrameFromCenter(x, y, 10,10);
g2d.draw(circle);
givenSurface.paint(g2d);
//givenSurface.drawPoint(g2d,this);
//givenSurface.repaint();
}
}

编辑:解决方案,感谢 camickr 。这不是我能想象的最优雅的方式,但它有效:

Point中的绘制方法:

public void draw(Surface givenSurface){
givenSurface.pointsList.add(this);
}

表面等级:

class Surface extends JPanel {

public ArrayList<Point> pointsList = new ArrayList<>();

private void drawPoints(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
double diameter = 10;
for (Point currentPoint : pointsList) {
Ellipse2D circle = new Ellipse2D.Double(currentPoint.x - diameter / 2, currentPoint.y - diameter / 2, diameter, diameter);
g2d.fill(circle);
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawPoints(g);
}

}

最佳答案

create a method of class Point (code at the bottom), that allows me to draw multiple points (and lines in the future).

所有自定义绘制都需要在面板的 paintComponent() 方法中完成。因此,每次绘制面板时,都会重新绘制点/线。

因此,如果您想绘制多个对象,您需要:

  1. 创建要绘制的对象的 ArrayList
  2. 创建一个方法将对象添加到此列表
  3. 在paintComponent()方法中,您迭代列表并绘制每个对象。

查看 Custom Painting Approaches 中的 DrawOnCompont 示例。它展示了如何动态添加要绘制的矩形。

关于java - 如何在JPanel上绘图的其他类中创建方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34020857/

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