如何在定义坐标系时绘制点?

时间:2019-01-07 23:58:19

标签: java swing user-interface awt java-2d

我有一个定义为坐标系的矩形,如下图所示。我想绘制点(-7.224,1002),但是我无法创建公式来绘制对应于该值的公式。参考代码:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.stream.Collectors;

import javax.swing.JComponent;

class PlotComponent extends JComponent {

private ArrayList<Point> points = new ArrayList<Point>();



    int sum=0,count=0,count1=0;
     int val=0,val1=0;
     private Color gridColor=new Color(200,200,200,200);



 protected void paintComponent(Graphics g)
   {
       super.paintComponent(g);
       Graphics2D g2 = (Graphics2D) g;
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

   int x_start_point=(int)(getWidth()*0.1);
   int x_end_point=(int)(getWidth()*0.8);
   int y_start_point=(int)(getHeight()*0.1);
   int y_end_point=(int)(getHeight()*0.8);
   int y_line=y_end_point+y_start_point;
   int x_line=x_end_point+x_start_point;
   int div_x=(x_end_point-x_start_point)/13;
   int div_y=(y_end_point-y_start_point)/13;
   Rectangle shape=new Rectangle(x_start_point,y_start_point,x_end_point,y_end_point);
   g2.setColor(Color.WHITE);
   g2.fill(shape);
   g2.setColor(Color.BLUE);
   g2.drawRect(x_start_point,y_start_point,x_end_point,y_end_point);
   int count=-2, count1=0;;
   for(int i=0;i<15;i++)
   {    

       g2.drawLine(x_start_point+(div_x*i),y_line,x_start_point+(div_x*i),(int)(y_line*1.02));
       g2.drawString(Integer.toString(count), (int)(x_start_point+(div_x*i)-3.5),(int)(y_line*1.07));
       count+=2;

       g2.drawLine(x_start_point,y_line-(div_y*i),(int)(x_start_point*0.85),y_line-(div_y*i));
       g2.drawString(Integer.toString(count1),7,(int)(y_line-(div_y*i)));
       count1+=500;
   }



 }

 public void addPoint(Point p) {
       points.add(p);
   }

}

现在,我已经基于一些偏移量值定义了一个矩形,因此在这种情况下,将根据偏移量和取自除法的值来绘制点,但我无法针对相同的点给出解决方案。请帮忙

1 个答案:

答案 0 :(得分:0)

查看代码中的更改以及解释它们的注释:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SwingTest extends JFrame {

    public SwingTest()  {

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(new PlotComponent());
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(()-> new SwingTest());
    }
}

class PlotComponent extends JComponent {

    private final ArrayList<Point2D> points = new ArrayList<>();
    //use constants for better readability
    private static final int X_MARKS = 13, X_START = -8, X_INCREMENT = 2,Y_MARKS = 13,
            Y_START = 0, Y_INCREMENT = 500, POINT_SIZE = 1;

    public PlotComponent() {
        setPreferredSize(new Dimension( 600, 500));
        addPoint(new Point2D.Double(-7.224, 1002)); //add test point
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //you need to use doubles to avoid rounding error and have use non integer coordinates
        double x_start_point = xStart();
        double y_start_point = yStart();
        double y_end_point = yEnd();
        double width = xEnd() - xStart();
        double hight = yEnd() - yStart();

        double div_x= width/X_MARKS; //this is also the scale: number of pixles per
        double div_y= hight/Y_MARKS; //mark. used to scale points when painting

        double xOrigin = x_start_point - div_x *(X_START/X_INCREMENT); //location of
        double yOrigin = y_end_point  + div_y *(Y_START/Y_INCREMENT);  // 0,0

        Rectangle2D shape=new Rectangle2D.Double(x_start_point,y_start_point,width,hight);
        g2.setColor(Color.WHITE);
        g2.fill(shape);
        g2.setColor(Color.BLUE);
        g2.draw(shape);

        int xMarkValue= X_START;
        for(int i=0; i <= X_MARKS;i++){
            Shape xMark = new Line2D.Double(x_start_point+div_x*i, y_end_point,
                                                        x_start_point+ div_x*i,y_end_point*1.02);
            g2.draw(xMark);
            g2.drawString(Integer.toString(xMarkValue), (float) (x_start_point+div_x*i-3.5),
                                                                        (float)(y_end_point*1.07));
            xMarkValue+=X_INCREMENT;
        }

        int yMarkValue=Y_START;
        for(int i=0; i<= Y_MARKS;i++){

            Shape yMark = new Line2D.Double(x_start_point,y_end_point-div_y*i,
                                                        x_start_point*0.85,y_end_point-div_y*i);
            g2.draw(yMark);
            g2.drawString(Integer.toString(yMarkValue),7,(float) (y_end_point-div_y*i));
            yMarkValue+=Y_INCREMENT;
        }

        g2.translate(xOrigin, yOrigin); // move the canvas origin to 0,0
        g2.setColor(Color.RED);

        for( Point2D p : points){
            //scale point coordinates. also change y direction
            Shape point =  new Ellipse2D.Double(p.getX()/div_x, - p.getY()/div_y, POINT_SIZE, POINT_SIZE);
            g2.draw(point);
        }
    }

    private double xStart() {
        return (int)(getWidth()*0.1);
    }
    private double xEnd() {
        return (int)(getWidth()*0.8);
    }

    private double yStart() {
        return (int)(getHeight()*0.1);
    }
    private double yEnd() {
        return (int)(getHeight()*0.8);
    }

    public void addPoint(Point2D p) {
        points.add(p);
    }
}