用Java应用程序和Bresenham算法实现绘制三条直线每条线的起点和终点坐标:第1条: (50,150), (100,50);青色第2条:(150,150) , (100,50)红色;第3条:(75,100) , (125,100)灰色
时间: 2023-05-14 09:05:10 浏览: 99
以下是用Java应用程序和Bresenham算法实现绘制三条直线的代码:
```java
import java.awt.*;
import javax.swing.*;
public class DrawLines extends JPanel {
private void drawLine(Graphics g, int x1, int y1, int x2, int y2, Color color) {
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx - dy;
while (true) {
g.setColor(color);
g.fillRect(x1, y1, 1, 1);
if (x1 == x2 && y1 == y2) {
break;
}
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x1 += sx;
}
if (e2 < dx) {
err += dx;
y1 += sy;
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawLine(g, 50, 150, 100, 50, Color.CYAN);
drawLine(g, 150, 150, 100, 50, Color.RED);
drawLine(g, 75, 100, 125, 100, Color.GRAY);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Lines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.add(new DrawLines());
frame.setVisible(true);
}
}
```
关于中文加密的问题,我不确定你具体指的是哪种加密方式,可以提供更多的信息吗?
阅读全文