我遇到过这段代码并且我一直在玩它,但是我试图绘制的点似乎在不正确的地方。我希望能够像一个图形一样绘制,因此原点(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
}
答案 0 :(得分:2)
您遇到的最大问题(除了不了解代码试图做的事情之外)是......
super.paintComponent
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了解详情