java多个点求连线_java 求高手帮助 在界面上给出两个或者多个点(坐标) 点击按钮 在每两个点之间出现一个点 求源代码 谢谢...

这篇博客展示了如何使用Java在界面上动态生成多个点,并在每两个点之间绘制连线。通过一个JComboBox选择点的数量,然后使用随机数生成点的坐标。当选择新的点数时,程序会更新点的集合,并在每个点之间画出连线,同时在每条连线的中点处显示一个绿色的小圆点。
摘要由CSDN通过智能技术生成

展开全部

x给你代码,附件是运行截图。

d6a5667cf3cd440d055576c2b242ec62.png

import java.awt.BorderLayout;

public class App extends JFrame implements ActionListener {

private JComboBox combox = null;

private Random random = new Random();

private MyPanel center = new MyPanel();

public App() {

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

JLabel label = new JLabel("point numbers :");

Integer[] items = { 2, 3, 4, 5, 6 };

combox = new JComboBox(items);

JButton button = new JButton("OK");

button.addActionListener(this);

panel.add(label, BorderLayout.WEST);

panel.add(combox, BorderLayout.CENTER);

panel.add(button, BorderLayout.EAST);

add(panel, BorderLayout.NORTH);

add(center, BorderLayout.CENTER);

setSize(600, 400);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String[] args) {

new App();

}

@Override

public void actionPerformed(ActionEvent e) {

int number = (Integer) combox.getSelectedItem();

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

for (int i = 0; i < number; i++) {

Point point = new Point();

point.x = random.nextInt(400) + 80;

point.y = random.nextInt(260) + 50;

points.add(point);

}

center.points = points;

// center.repaint();

repaint();

}

}

class MyPanel extends JPanel {

public List<Point> points = null;

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if (points != null) {

paintPoints(g, points);

}

}

private void paintPoints(Graphics g, List<Point> points) {

if (points.size() == 1) {

return;

}

Point point = points.remove(0);

for (int i = 0; i < points.size(); i++) {

Point tmp = points.get(i);

paintCenterPoint(g, point, tmp);

}

paintPoints(g, points);

}

private void paintCenterPoint(Graphics g, Point src, Point dest) {

// 在两个点上画e68a84e8a2ad62616964757a686964616f31333332613730一个直径20的圆点

g.fillOval(src.x - 10, src.y - 10, 20, 20);

g.fillOval(dest.x - 10, dest.y - 10, 20, 20);

// 画点连接

g.drawLine(src.x, src.y, dest.x, dest.y);

// 计算中间点位置

int x = (int) Math.floor(src.x + (dest.x - src.x) / 2);

int y = (int) Math.floor(src.y + (dest.y - src.y) / 2);

Color color = g.getColor();

g.setColor(Color.GREEN);

// 画两点中间的点

g.fillOval(x - 10, y - 10, 20, 20);

g.setColor(color);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值