gpt4 book ai didi

java - 用Java绘制散点图

转载 作者:行者123 更新时间:2023-12-02 05:49:24 40 4
gpt4 key购买 nike

我遇到了这段代码,并且一直在使用它,但是我尝试绘制的点似乎位于不正确的位置。我希望能够像图表一样绘制,因此原点 (0,0) 应该位于左下角,并且点应该相对于它绘制,有人可以帮助我吗?

package graphtest;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.Graphics;
import java.util.*;

public class Scatterplot extends javax.swing.JFrame {
private List points = new ArrayList();

public Scatterplot() {
super("Scatterplot");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

points.add(new Point2D.Float(1, 4));
points.add(new Point2D.Float(2, 10));
points.add(new Point2D.Float(3, 12));
//points.add(new Point2D.Float(3, 10));
// points.add(new Point2D.Float(4, 12));

JPanel panel = new JPanel() {
public void paintComponent(Graphics g) {
//g.translate(0, 0);
for(Iterator i=points.iterator(); i.hasNext(); ) {
Point2D.Float pt = (Point2D.Float)i.next();
g.drawString(".", (int)pt.x, (int)pt.y);
}

}
};

setContentPane(panel);
setBounds(10, 10, 200, 200);

setVisible(true);
}
public static void main(String[] args) {
new Scatterplot();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

// Variables declaration - do not modify
// End of variables declaration
}

最佳答案

您遇到的最大问题(除了不理解代码想要做什么之外)是......

  1. 在进行任何自定义绘制之前,不先调用 super.paintComponent
  2. 使用drawString...

使用 drawString 绘制 . 字符意味着该点将出现在您指定的 y 位置下方,因为该字符必须考虑构成字体的所有字符的整体高度。字体也会在上升线附近渲染,这也会导致 y 位置偏离。

相反,您应该更好地利用 Graphics2D API 并实际绘制点,例如......

这个示例基本上使用 Ellipse2D 来绘制点,我对其进行了设置,以便它将在指定的 x/y 坐标处绘制点的中心。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Scatterplot extends javax.swing.JFrame {

private List points = new ArrayList();

public Scatterplot() {
super("Scatterplot");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

points.add(new Point2D.Float(1, 4));
points.add(new Point2D.Float(2, 10));
points.add(new Point2D.Float(3, 12));
//points.add(new Point2D.Float(3, 10));
// points.add(new Point2D.Float(4, 12));

JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
//g.translate(0, 0);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
// g.setColor(Color.RED);
// g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g.setColor(Color.BLACK);
for (Iterator i = points.iterator(); i.hasNext();) {
Point2D.Float pt = (Point2D.Float) i.next();
Ellipse2D dot = new Ellipse2D.Float(pt.x - 1, pt.y - 1, 2, 2);
g2d.fill(dot);
}
g2d.dispose();
}
};

setContentPane(panel);
setBounds(10, 10, 200, 200);

setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

new Scatterplot();
}
});
}
}

看看2D Graphics了解更多详情

关于java - 用Java绘制散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23709827/

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