求救! 菜鸟一枚不知道在java里怎么用drawline划线
/*图里只有预设的点,点之间没有连线,求帮助*/import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Prim extends Applet{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("顶点数和边数:");
while (sc.hasNext()) {
int n = sc.nextInt();//图的顶点数
int m = sc.nextInt(); //图的边数
if (n == 0 && m == 0)
break;
int[][] map = new int[n][n];//存放边的权值
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
map[i][j] = 100; //起点i,终点为j的边的权植。
}
}
System.out.println("顶点记为从0到" + (n-1) + ",请输入相应顶点以及之间的权值");
for (int i = 0; i < m; ++i) {
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
map[u][v] = w;//起点u,终点为v的边的权植。
map[v][u] = w;
}
Edge[] e = new Edge[n]; //存放最小生成树的n-1条边
prime(map, e, n);
JFrame f = new JFrame();
f.setSize(800,600);
f.setLayout(null);
f.setVisible(true);
f.setTitle("最小生成树:");
JLabel L1 = new JLabel();
JLabel L2 = new JLabel();
JLabel L3 = new JLabel();
L1.setText("("+0+")");
L1.setBounds(300,100,100,100);
L2.setText("("+1+")");
L2.setBounds(400,100,100,100);
L3.setText("("+2+")");
L3.setBounds(500,100,100,100);
f.add(L1);
f.add(L2);
f.add(L3);
int k = 0;
for(int j = 3;j<n;j++)
{
JLabel L = new JLabel();
L.setText("("+j+")");
L.setBounds(300+k,300,100,100);
f.add((L));
k += 100;
}
Prim p = new Prim();
f.add(p);
}
}
static void prime(int[][] map, Edge[] e, int n) {
for (int i = 0; i < n - 1; ++i) {
e[i] = new Edge();
e[i].from = 0;
e[i].end = i + 1;
e[i].weight = map[0][i + 1];
}
for (int k = 1; k < n; ++k) {
int m = -1;
int min = Integer.MAX_VALUE;
for (int j = k - 1; j < n - 1; ++j) {
if (e[j].weight < min) {
min = e[j].weight;
m = j;
}
}
Edge tmp = e[m];
e[m] = e[k - 1];
e[k - 1] = tmp;
int c = k - 1;
for (int j = k; j < n - 1; ++j) {
int t = e[j].end;
int w = e[j].weight;
if (map[e[c].end][t] < w) {
e[j].from = e[c].end;
e[j].weight = map[e[c].end][t];
}
}
}
}
public static class Edge {
int from;
int end;
int weight;
}
public void paint(Graphics g,int n,Edge[] e)
{
g.setColor(Color.black);
for(int i=0;i<n-1;++i)
{
if(e[i].from<3)
{
if(e[i].end<3)
g.drawLine(e[i].from*100+300, 100, e[i].end*100+100, 100);
else
g.drawLine(e[i].from*100+300, 100, e[i].end*100+100, 300);
}
else
{
if(e[i].end<3)
g.drawLine(e[i].from*100+300, 300, e[i].end*100+100, 100);
else
g.drawLine(e[i].from*100+300, 300, e[i].end*100+100, 300);
}
}
}
}
--------------------编程问答-------------------- 新手最大问题是 不会'baidu'和调试。 --------------------编程问答-------------------- 最好是add(p)以后再f.setVisible(true);
否则,窗体已经显示,要让窗体能反映内部处理结果,就要调用repaint方法重绘一下
可以试试看
Prim p = new Prim();
f.add(p);
p.repaint(); //重绘一下
--------------------编程问答-------------------- 不认真看书才是最大问题。
为什么要 Applet?????? --------------------编程问答--------------------
是啊,LZ没好好看api
public void paint(Graphics g,int n,Edge[] e)这个方法applet本身不会自己调用
Prim p = new Prim();
f.add(p);
p.paint(p.getGraphic(), n, e);//自己调用方法试试看
--------------------编程问答-------------------- 先得到控件的Graphics对象,JPanel.getGraphics(),然后调用graphics.drawline --------------------编程问答--------------------
重绘之后还是无图 --------------------编程问答--------------------
调用之后仍然无图。。。。 --------------------编程问答--------------------
+1
lz要学会查资料,网上很多资料将得很好,还有很多例子 --------------------编程问答-------------------- 观摩学习学习..
补充:Java , Java SE