4 changed files with 207 additions and 369 deletions
-
63src/main/java/com/qyft/ms/app/common/generator/PathGenerator.java
-
47src/main/java/com/qyft/ms/test/PathGenerator/MainFrame.java
-
114src/main/java/com/qyft/ms/test/PathGenerator/PathPanelJPanel.java
@ -1,47 +0,0 @@ |
|||
package com.qyft.ms.test.PathGenerator; |
|||
|
|||
import javax.swing.*; |
|||
import java.awt.*; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 主窗口,包含main方法。创建面板PathPanel,加载进JFrame展示。 |
|||
*/ |
|||
public class MainFrame extends JFrame { |
|||
|
|||
public static void main(String[] args) { |
|||
SwingUtilities.invokeLater(() -> { |
|||
MainFrame frame = new MainFrame(); |
|||
frame.setVisible(true); |
|||
}); |
|||
} |
|||
|
|||
public MainFrame() { |
|||
super("Zigzag Path Demo (No Inner Classes)"); |
|||
|
|||
setDefaultCloseOperation(EXIT_ON_CLOSE); |
|||
|
|||
// 1. 定义边界 |
|||
int left = 0; |
|||
int right = 20; |
|||
int bottom = 0; |
|||
int top = 50; |
|||
int spacing = 2; |
|||
|
|||
// 2. 生成路径(从“左上角”往下的水平之字形) |
|||
List<Point> path = PathGenerator.generatePathPoints( |
|||
left, right, bottom, top, |
|||
spacing, |
|||
PathGenerator.MoveMode.HORIZONTAL_ZIGZAG_TOP_DOWN |
|||
); |
|||
|
|||
// 3. 创建面板并设置大小 |
|||
PathPanelJPanel panel = new PathPanelJPanel(left, right, bottom, top, path); |
|||
panel.setPreferredSize(new Dimension((right - left + 1) * 20, |
|||
(top - bottom + 1) * 20)); |
|||
getContentPane().add(panel); |
|||
|
|||
pack(); |
|||
setLocationRelativeTo(null); // 居中显示 |
|||
} |
|||
} |
@ -1,114 +0,0 @@ |
|||
package com.qyft.ms.test.PathGenerator; |
|||
|
|||
import javax.swing.*; |
|||
import java.awt.*; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 自定义面板:绘制网格和路径。 |
|||
* 坐标系:数学上left..right, bottom..top;GUI需翻转y轴。 |
|||
*/ |
|||
public class PathPanelJPanel extends JPanel { |
|||
|
|||
private final int left; |
|||
private final int right; |
|||
private final int bottom; |
|||
private final int top; |
|||
private final List<Point> pathPoints; |
|||
|
|||
// 每单位1点对应多少像素,可自行调节 |
|||
private static final int CELL_SIZE = 20; |
|||
|
|||
public PathPanelJPanel(int left, int right, int bottom, int top, List<Point> pathPoints) { |
|||
this.left = left; |
|||
this.right = right; |
|||
this.bottom = bottom; |
|||
this.top = top; |
|||
this.pathPoints = pathPoints; |
|||
} |
|||
|
|||
@Override |
|||
protected void paintComponent(Graphics g) { |
|||
super.paintComponent(g); |
|||
|
|||
Graphics2D g2d = (Graphics2D) g.create(); |
|||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|||
|
|||
// 1) 画网格 |
|||
drawGrid(g2d); |
|||
// 2) 画路径 |
|||
drawPath(g2d); |
|||
|
|||
g2d.dispose(); |
|||
} |
|||
|
|||
/** 绘制网格线和坐标标签 */ |
|||
private void drawGrid(Graphics2D g2d) { |
|||
g2d.setColor(Color.LIGHT_GRAY); |
|||
|
|||
// 画垂直线 |
|||
for (int x = left; x <= right; x++) { |
|||
int px = worldToPanelX(x); |
|||
g2d.drawLine(px, worldToPanelY(bottom), px, worldToPanelY(top)); |
|||
} |
|||
// 画水平线 |
|||
for (int y = bottom; y <= top; y++) { |
|||
int py = worldToPanelY(y); |
|||
g2d.drawLine(worldToPanelX(left), py, worldToPanelX(right), py); |
|||
} |
|||
|
|||
// 坐标文本标注(可选) |
|||
// g2d.setColor(Color.BLACK); |
|||
// for (int x = left; x <= right; x++) { |
|||
// for (int y = bottom; y <= top; y++) { |
|||
// int px = worldToPanelX(x) + 2; |
|||
// int py = worldToPanelY(y) - 2; |
|||
// g2d.drawString(x + "," + y, px, py); |
|||
// } |
|||
// } |
|||
} |
|||
|
|||
/** 绘制之字形路径及每个拐点小圆,边绘制边输出坐标 */ |
|||
private void drawPath(Graphics2D g2d) { |
|||
// 先用红色线段连接拐点 |
|||
g2d.setColor(Color.RED); |
|||
g2d.setStroke(new BasicStroke(2f)); |
|||
|
|||
for (int i = 0; i < pathPoints.size() - 1; i++) { |
|||
Point p1 = pathPoints.get(i); |
|||
Point p2 = pathPoints.get(i + 1); |
|||
|
|||
int x1 = worldToPanelX(p1.x); |
|||
int y1 = worldToPanelY(p1.y); |
|||
int x2 = worldToPanelX(p2.x); |
|||
int y2 = worldToPanelY(p2.y); |
|||
|
|||
g2d.drawLine(x1, y1, x2, y2); |
|||
} |
|||
|
|||
// 再用蓝色在拐点上画小圆点 |
|||
g2d.setColor(Color.BLUE); |
|||
for (Point p : pathPoints) { |
|||
int cx = worldToPanelX(p.x); |
|||
int cy = worldToPanelY(p.y); |
|||
|
|||
// 同时打印到控制台,说明“已经画到了这个点” |
|||
System.out.println("画点: (" + p.x + ", " + p.y + ")"); |
|||
|
|||
g2d.fillOval(cx - 4, cy - 4, 8, 8); |
|||
} |
|||
} |
|||
|
|||
/** 坐标转换:世界X -> 面板像素X */ |
|||
private int worldToPanelX(int worldX) { |
|||
return (worldX - left) * CELL_SIZE; |
|||
} |
|||
|
|||
/** 坐标转换:世界Y -> 面板像素Y(翻转y) */ |
|||
private int worldToPanelY(int worldY) { |
|||
// 矩形整体高度(像素) |
|||
int totalHeight = (top - bottom) * CELL_SIZE; |
|||
// 自下而上 -> GUI坐标需自上而下翻转 |
|||
return totalHeight - (worldY - bottom) * CELL_SIZE; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue