日期:2014-05-20  浏览次数:20698 次

直线的相关问题!!!
比如在数组int   []   a   =   new   int   []{4,8,79,6,55,7};中,如果我想实现:
用直线连接数组a中的每一个点!!!!
要求能画出这个图形!!!!

希望得到大家的回答!!!!

------解决方案--------------------
OK了请结贴吧。


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class DrawTest
{
public static void main(String[] args)
{
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
A frame that contains a panel with drawings
*/
class DrawFrame extends JFrame
{
public DrawFrame()
{
setTitle( "DrawTest ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// add panel to frame

DrawPanel panel = new DrawPanel();
add(panel);
}

public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
}

/**
A panel that displays rectangles and ellipses.
*/
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

// draw a rectangle
int [] a = new int []{4,8,79,6,55,7};
Point2D[]t=new Point2D[a.length];
for(int i =0;i <a.length;i++)
{
t[i] =new Point2D.Double(a[i],i*20);
}
for(int i = 0;i <t.length - 2;i++)
{
Line2D b = new Line2D.Double(t[i],t[i+1]);
g2.draw(b);
}
}
}