Java 中的笛卡尔平面

Cartesian Plane in Java

在学习了循环、数组、方法...之后,我开始研究图形,但遇到了一些问题。当我看到这个时,我正在寻找一些例子:http://javaceda.blogspot.ch/2010/06/draw-cartesian-coordinate-system-in.html

它在 Java 中提供了笛卡尔平面的示例。我几乎可以理解该代码的所有内容(除了 invokelater、SwingUtilities 之类的几行代码,我稍后会看一下它们……)。

假设现在我想创建一个 class "Point",其中包含构造函数、getter、将点的笛卡尔坐标转换为 "Pixel Coordinates" 的方法以及打印平面上的点。

因此,这是从 link:

中获取的代码
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Cartesian {
 public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {

   @Override
   public void run() {
    CartesianFrame frame = new CartesianFrame();
    frame.showUI();
   }
  });
 }

}

class CartesianFrame extends JFrame {
 CartesianPanel panel;

 public CartesianFrame() {
  panel = new CartesianPanel();
  add(panel);
 }

 public void showUI() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setTitle("Cartesian");
  setSize(700, 700);
  setVisible(true);
 }
}

class CartesianPanel extends JPanel {
 // x-axis coord constants
 public static final int X_AXIS_FIRST_X_COORD = 50;
 public static final int X_AXIS_SECOND_X_COORD = 600;
 public static final int X_AXIS_Y_COORD = 600;

 // y-axis coord constants
 public static final int Y_AXIS_FIRST_Y_COORD = 50;
 public static final int Y_AXIS_SECOND_Y_COORD = 600;
 public static final int Y_AXIS_X_COORD = 50;

 //arrows of axis are represented with "hipotenuse" of 
 //triangle
 // now we are define length of cathetas of that triangle
 public static final int FIRST_LENGHT = 10;
 public static final int SECOND_LENGHT = 5;

 // size of start coordinate lenght
 public static final int ORIGIN_COORDINATE_LENGHT = 6;

 // distance of coordinate strings from axis
 public static final int AXIS_STRING_DISTANCE = 20;


 public void paintComponent(Graphics g) {

  super.paintComponent(g);

  Graphics2D g2 = (Graphics2D) g;

  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);

  // x-axis
  g2.drawLine(X_AXIS_FIRST_X_COORD, X_AXIS_Y_COORD,
     X_AXIS_SECOND_X_COORD, X_AXIS_Y_COORD);
  // y-axis
  g2.drawLine(Y_AXIS_X_COORD, Y_AXIS_FIRST_Y_COORD,
     Y_AXIS_X_COORD, Y_AXIS_SECOND_Y_COORD);

  // draw origin Point
  g2.fillOval(
    X_AXIS_FIRST_X_COORD - (ORIGIN_COORDINATE_LENGHT / 2), 
    Y_AXIS_SECOND_Y_COORD - (ORIGIN_COORDINATE_LENGHT / 2),
    ORIGIN_COORDINATE_LENGHT, ORIGIN_COORDINATE_LENGHT);

  // numerate axis
  int xCoordNumbers = 10;
  int yCoordNumbers = 10;
  int xLength = (X_AXIS_SECOND_X_COORD - X_AXIS_FIRST_X_COORD)
      / xCoordNumbers;
  int yLength = (Y_AXIS_SECOND_Y_COORD - Y_AXIS_FIRST_Y_COORD)
      / yCoordNumbers;

  // draw x-axis numbers
  for(int i = 1; i < xCoordNumbers; i++) {
   g2.drawLine(X_AXIS_FIRST_X_COORD + (i * xLength),
     X_AXIS_Y_COORD - SECOND_LENGHT,
     X_AXIS_FIRST_X_COORD + (i * xLength),
     X_AXIS_Y_COORD + SECOND_LENGHT);
   g2.drawString(Integer.toString(i), 
     X_AXIS_FIRST_X_COORD + (i * xLength) - 3,
     X_AXIS_Y_COORD + AXIS_STRING_DISTANCE);
  }

  //draw y-axis numbers
  for(int i = 1; i < yCoordNumbers; i++) {
   g2.drawLine(Y_AXIS_X_COORD - SECOND_LENGHT,
     Y_AXIS_SECOND_Y_COORD - (i * yLength), 
     Y_AXIS_X_COORD + SECOND_LENGHT,
     Y_AXIS_SECOND_Y_COORD - (i * yLength));
   g2.drawString(Integer.toString(i), 
     Y_AXIS_X_COORD - AXIS_STRING_DISTANCE, 
     Y_AXIS_SECOND_Y_COORD - (i * yLength));
  }
 }
}

这是我要补充的class点:

class Point{
      public int x,y;

        public Point(int x,int y){
            this.x=x;
            this.y=y;
        }
        public Point FromCartToPix() {
            this.x=X_AXIS_FIRST_X_COORD+(x*xLength);
            this.y=Y_AXIS_SECOND_Y_COORD - (y * yLength);
            return this;

        }
        public int GetX(){
            return this.x;
        }
        public int GetY(){
            return this.y;
        }
        public void DrawPoint(){
            g2.fillOval(
                    this.FromCartToPix().Getx(), 
                    this.FromCartToPix().Gety(),
                    ORIGIN_COORDINATE_LENGHT, ORIGIN_COORDINATE_LENGHT);
        }
}

我的 Java 手册中只有一小章是关于 Java 图形和 Swing 的,因此我无法实现这个 class。我认为它应该插入

PaintComponent(Graphics g)

不然我用不了

g2.filloval

但后来我无法使用

Point a = new Point (2,3);

在主内

我知道这是一个有点模糊和笼统的问题,但我无法让它发挥作用。我搜索了很多关于 Graphics2D g2 =(Graphics2D) g 的用法,但找不到准确的解释,即使在 Javadocs.

如果你知道 link 可以解释它,我将不胜感激

提前致谢

您的 Point class 无权访问 CartesianPanel.

Graphics2D 对象

您应该将 FromCartToPixDrawPoint 方法的功能移至 CartesionPanel。通过这种方式,您实际上可以绘制点并将数据 (Point) 与 UI (CartesionPanel) 分开。

// add in CartesionPanel
private List<Point> points = new ArrayList<>();

public void drawPoint(Point point) {
    points.add(point);
    repaint();
}

private void drawPointOnPanel(Point point, Graphics g) {
    final int pointDiameter = 5;
    final int x = X_AXIS_FIRST_X_COORD + (point.x * xLength) - pointDiameter / 2;
    final int y = Y_AXIS_SECOND_Y_COORD - (point.y * yLength) - pointDiameter / 2;
    g.fillOval(x, y, pointDiameter, pointDiameter);
}

public void paintComponent(Graphics g) {
    // existing code ...

    // draw points
    points.forEach(p -> drawPointOnPanel(p, g))
}

在您的 main 函数中,您可以通过以下方式绘制 Points:

CartesianFrame frame = new CartesianFrame();
frame.showUI();

frame.panel.drawPoint(new Point(3, 4));