根据点画线java

 1 package com.yang;

 2 

 3 import java.awt.Color;

 4 import java.awt.Graphics;

 5 import java.util.ArrayList;

 6 import java.util.List;

 7 

 8 import javax.swing.JFrame;

 9 import javax.swing.JPanel;

10 

11 public class drawlineforspline extends JFrame{

12     

13     private static final long serialVersionUID = 1L;

14     static List <mypoint>plist;

15     public static class mypoint{

16         int x;

17         int y;

18         public mypoint(int x,int y){

19             this.x=x;

20             this.y=y;

21         }

22     }

23     public drawlineforspline(){

24         init();

25     }

26     public drawlineforspline(ArrayList plist){

27         init();

28         this.plist=plist;

29          

30     }

31     private void init(){

32      

33         this.setTitle("drawline");

34         this.setBounds(200, 200, 500, 400);

35         this.setBackground(Color.white);

36         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

37         this.setLocationRelativeTo(null);

38         this.setVisible(true); 

39         

40         plist =new ArrayList();

41         plist.add(new mypoint(50,80));

42         plist.add(new mypoint(50,120));

43         plist.add(new mypoint(80,50));

44         plist.add(new mypoint(150,10));

45         plist.add(new mypoint(180,80));

46         plist.add(new mypoint(230,200));

47         

48     }

49  

50     

51     public class Mypanel extends JPanel{     

52         public void paint(Graphics g){

53             mypoint fromP=new mypoint(50,80);

54             mypoint toP=new mypoint(370,240);

55             for(int i=0;i<plist.size()-1;i++){

56                 g.drawLine(plist.get(i).x, plist.get(i).y, plist.get(i+1).x, plist.get(i+1).y);

57             }

58         }

59     }

60     

61     public static void main(String[] args) {

62         drawlineforspline d=new drawlineforspline();

63         Mypanel myp=d.new Mypanel();

64         d.add(myp);

65     }

66 

67 }

 

你可能感兴趣的:(java)