如何将绘图图上的 x 和 y 坐标转换为 GUI 上的像素

huangapple go评论75阅读模式
英文:

How can I convert x and y coordinates on plot graph to pixels on a GUI

问题

我正在创建一个凸包GUI程序,它显示在一个网格上。以下是GUI的代码。

public class Plot extends JPanel {
    private int width = 1025;
    private int height = 500;
    private int padding = 50;
    private int labelPadding = 50;
    private Color lineColor = new Color(44, 102, 230, 180);
    private Color pointColor = new Color(100, 100, 100, 180);
    private Color gridColor = new Color(200, 200, 200, 200);
    private static final Stroke GRAPH_STROKE = new BasicStroke(2f);
    private int pointWidth = 4;
    private int numberYDivisions = 10;
    private Vector<Point> convexHull;
    private static DecimalFormat df = new DecimalFormat("0.00");
    private Point[] point;

    public Plot(Point[] point, Vector convexHull) {
        this.point = point;
        this.convexHull = convexHull;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        double xScale = (maxX(point) + 10) * 2;
        double yScale = (maxY(point) + 10) * 2;
        List<Point> graphPoints = new ArrayList<>();

        for (Point temp : convexHull) {
            double x1 = temp.x;
            double y1 = temp.y;
            graphPoints.add(new Point(x1, y1));
        }

        double intervalY;
        intervalY = maxY(point) / 5;

        g2.setColor(Color.WHITE);
        g2.fillRect(padding + labelPadding, padding, (int) xScale, (int) yScale);

        // Hatch marks for Y
        // ... 以下省略 ...
        // y axis
        g2.drawLine(500, padding, 500, 1025 - padding);
    }

    private double maxX(Point[] p) {
        // ... 以下省略 ...
    }

    private double minX(Point[] p) {
        // ... 以下省略 ...
    }

    private double maxY(Point[] p) {
        // ... 以下省略 ...
    }

    private double minY(Point[] p) {
        // ... 以下省略 ...
    }
}

在上面的代码中,点从CSV文件中读取,并以具有x和y属性的point[]存储。为了正确显示此图形,基于point[]中的值计算了刻度标记。但我不确定如何绘制每个点,因为创建椭圆需要屏幕上的像素x和y坐标,我不能只是将每个点的x和y坐标用作点的x和y坐标。是否有一种方法将point[]中的x和y坐标转换为像素x和y坐标?任何帮助将不胜感激。

英文:

I'm creating a program for a convex hull gui that's displayed on a grid. The following code is the GUI.

public class Plot extends JPanel{
private int width = 1025;
private int height = 500;
private int padding = 50;
private int labelPadding = 50;
private Color lineColor = new Color(44, 102, 230, 180);
private Color pointColor = new Color(100, 100, 100, 180);
private Color gridColor = new Color(200, 200, 200, 200);
private static final Stroke GRAPH_STROKE = new BasicStroke(2f);
private int pointWidth = 4;
private int numberYDivisions = 10;
private Vector&lt;Point&gt; convexHull;
private static DecimalFormat df = new DecimalFormat(&quot;0.00&quot;);
private Point[] point;
public Plot(Point[] point, Vector convexHull) {
this.point=point;
this.convexHull=convexHull;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
double xScale = (maxX(point)+10)*2;
double yScale = (maxY(point)+10)*2 ;
List&lt;Point&gt; graphPoints =  new ArrayList&lt;&gt;();
for(Point temp : convexHull){
double x1 = temp.x;
double y1 = temp.y;
graphPoints.add(new Point(x1,y1));
}
double intervalY;
intervalY = maxY(point)/5;
g2.setColor(Color.WHITE);
g2.fillRect(padding + labelPadding, padding, (int)xScale, (int)yScale);
//Hatch marks for Y
for(int i = 0;i&lt;numberYDivisions/2;i++){
if(point.length&gt;0){
g2.setColor(gridColor);
g2.drawLine(padding+(100*i), padding, padding+(100*i), 1000);
g2.setColor(Color.BLACK);
String yLabel = df.format(maxY(point) - (intervalY * i)) + &quot;&quot;;
g2.drawLine(495, padding + (100 * i), 505, padding + (100 * i));
g2.drawString(yLabel, 460, padding + (100 * i));
}
}
for(int i = 0;i&lt;=numberYDivisions/2;i++){
if(point.length&gt;0){
g2.setColor(gridColor);
g2.drawLine(500+padding+(100*i), padding, 500+padding+(100*i), 1000);
g2.setColor(Color.BLACK);
if(i!=0) {
String yLabel = df.format(0 - (intervalY * i)) + &quot;&quot;;
g2.drawLine(495, 500 + (100 * (i)) - padding, 505, 500 + (100 * (i)) - padding);
g2.drawString(yLabel, 460, 500 + (100 * (i)) - padding);
}
}
}
Double intervalX;
intervalX = maxX(point)/5;
//Hatch marks and grid lines for x;
for(int i = 0;i&lt;numberYDivisions/2;i++){
if(point.length&gt;0){
g2.setColor(gridColor);
g2.drawLine(padding,padding+(100*i),1000,padding+(100*i));
g2.setColor(Color.BLACK);
String yLabel = df.format(-maxX(point) + (intervalX * i)) + &quot;&quot;;
g2.drawLine(padding + (100 * i), 495, padding + (100 * i), 505);
g2.drawString(yLabel, padding + (100 * i), 480);
}
}
for(int i = 0;i&lt;=numberYDivisions/2;i++){
if(point.length&gt;0){
g2.setColor(gridColor);
g2.drawLine(padding,500+padding+(100*i),1000,500+padding+(100*i));
if(i!=0) {
g2.setColor(Color.BLACK);
String yLabel = df.format(0 + (intervalX * i)) + &quot;&quot;;
g2.drawLine(500 + (100 * i) - padding, 495, 500 + (100 * i) - padding, 505);
g2.drawString(yLabel, 500 + (100 * i) - padding, 480);
}
}
}
//x axis
g2.drawLine(padding, 500, 1025-padding, 500);
//y axis
g2.drawLine(500,padding,500,1025-padding);
}
private double maxX(Point[] p){
Double Max = Double.MIN_VALUE;
for(int i = 0;i&lt;p.length;i++){
if(Max&lt;p[i].x){
Max = p[i].x;
}
}
return Max;
}
private double minX(Point[] p){
Double min = Double.MAX_VALUE;
for(int i = 0;i&lt;p.length;i++){
if(min&gt;p[i].x){
min = p[i].x;
}
}
return min;
}
private double maxY(Point[] p){
Double Max = Double.MIN_VALUE;
for(int i = 0;i&lt;p.length;i++){
if(Max&lt;p[i].y){
Max = p[i].y;
}
}
return Max;
}
private double minY(Point[] p){
Double min = Double.MAX_VALUE;
for(int i = 0;i&lt;p.length;i++){
if(min&gt;p[i].y){
min = p[i].y;
}
}
return min;
}

In the above code the points are read from a CSV file and stored point[] with an x and y property. For this graph to be displayed correctly, the hatch markings are calculated based on the values in point[]. But I am not sure how to plot each point because creating an oval takes x and y coordinates for pixels on the screen, I cannot just use the x and y coordinates of each point as the x and y coordinates for the dots. Is there a way to convert the x and y coordinates in point[] to pixel x and y coordinates? Any help is appreciated.

答案1

得分: 1

根据您的数据的“x和y的最大值”以及“面板的宽度和高度”,您需要将它们进行放大或缩小。

如果您的“数据x”的最大x值为“xmax”,而您的“面板宽度”为“pwidth”,那么您需要将您的“数据x”坐标乘以“pwidth/xmax”。请使用“double”类型进行计算,然后根据需要转换为“int”类型。

英文:

You need to scale them up or down based on the max x and y of your data and the width and height, respectively of your panel.

If your data x maximum x is xmax and your panel width is pwidth then you need to multiply your data x coordinates by pwidth/xmax. Do it using double types then convert to int if necessary.

huangapple
  • 本文由 发表于 2020年4月4日 08:48:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61022470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定