好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现的简单画图板示例

本文实例讲述了java实现的简单 画图板 。分享给大家供大家参考,具体如下:

这个画图板是我好久之前做的,之后浙大的同学需要做课设然后就花了一点时间将它改了一下,变得简单些能够方便扩充功能,同时学习java基础

先截图一下吧,就可以知道有哪些功能了~

三个分区,上面选择图形,下面选择颜色,立体圆就是一个分形,也先放着不需要的同学可以注释了它

代码很简单,就是jpanel进行分区,得到画笔,同时使用画图的函数就可以做到了

贴代码应该很快就会了~

主类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

package awtdemo;

import java.awt.borderlayout;

import java.awt.color;

import java.awt.dimension;

import java.awt.flowlayout;

import javax.swing.jbutton;

import javax.swing.jframe;

import javax.swing.jpanel;

@suppresswarnings ( "serial" )

public class drawmain extends jpanel {

     public static void main(string[] args) {

         // todo auto-generated method stub

         drawmain draw = new drawmain();

         draw.initui();

     }

     public void initui() {

         jframe jf = new jframe();

         jf.setsize( 1000 , 780 );

         jf.settitle( "简单画板" );

         jf.setdefaultcloseoperation( 3 );

         jf.setlocationrelativeto( null );

         jf.setlayout( new borderlayout());

         // 实例化事件监听类

         drawlistener dl = new drawlistener( this );

         // 实现中间面板

         this .setbackground(color.white);

         jf.add( this , borderlayout.center);

         // 实现性状面板

         jpanel shapepanel = new jpanel();

         shapepanel.setbackground(color.black);

         shapepanel.setlayout( new flowlayout(flowlayout.center));

         shapepanel.setbackground(color.gray);

         ;

         string[] shape = { "直线" , "曲线" , "圆" , "喷枪" , "橡皮擦" , "矩形" , "椭圆" , "圆角矩形" ,

                 "弧线" , "多边形" , "图形" , "三角形" , "立体圆" , };

         for ( int i = 0 ; i < shape.length; i++) {

             jbutton button = new jbutton(shape[i]);

             button.setbackground(color.white);

             button.addactionlistener(dl); // 添加事件监听机制

             shapepanel.add(button);

         }

         jf.add(shapepanel, borderlayout.north);

         // 实现颜色面板

         jpanel colorpanel = new jpanel();

         colorpanel.setbackground(color.black);

         colorpanel.setlayout( new flowlayout(flowlayout.center));

         colorpanel.setbackground(color.gray);

         ;

         color[] color = { color.black, color.blue, color.white, color.gray,

                 color.red, color.cyan, color.green, color.darkgray, color.pink };

         for ( int i = 0 ; i < color.length; i++) {

             jbutton button = new jbutton();

             button.addactionlistener(dl); // 添加事件监听机制

             button.setpreferredsize( new dimension( 30 , 30 ));

             button.setbackground(color[i]);

             colorpanel.add(button);

         }

         jf.add(colorpanel, borderlayout.south);

         jf.setvisible( true );

         this .addmouselistener(dl);

         this .addmousemotionlistener(dl);

     }

}

监听辅助类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

package awtdemo;

import java.awt.basicstroke;

import java.awt.color;

import java.awt.graphics2d;

import java.awt.renderinghints;

import java.awt.event.actionevent;

import java.awt.event.actionlistener;

import java.awt.event.mouseadapter;

import java.awt.event.mouseevent;

import java.util.random;

import javax.swing.jbutton;

public class drawlistener extends mouseadapter implements actionlistener {

     private int x1, y1, x2, y2;

     private int newx1, newy1, newx2, newy2;

     private graphics2d g;

     private drawmain df;

     private boolean flag = false ;

     string shape = "直线" ;

     color color;

     private int [] arrx = new int [ 4 ];

     private int [] arry = new int [ 4 ];

     private int temp = 0 ;

     drawlistener(drawmain d) {

         df = d;

     }

     // 获取形状和颜色

     public void actionperformed(actionevent e) {

         if (e.getactioncommand().equals( "" )) {

             jbutton button = (jbutton) e.getsource();

             color = button.getbackground();

             system.out.println( "color = " + color);

         } else {

             jbutton button = (jbutton) e.getsource();

             shape = button.getactioncommand();

             system.out.println( "string = " + shape);

         }

     }

     // 实现画笔

     public void mousepressed(mouseevent e) {

         g = (graphics2d) df.getgraphics();

         g.setcolor(color);

         x1 = e.getx();

         y1 = e.gety();

     }

     public void mousereleased(mouseevent e) {

         x2 = e.getx();

         y2 = e.gety();

         if (shape.equals( "直线" )) {

             g.drawline(x1, y1, x2, y2);

         } else if (shape.equals( "弧线" )) {

             g.drawarc(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1), 0 , 180 );

         } else if (shape.equals( "多边形" ) && !flag) {

             g.drawline(x1, y1, x2, y2);

             newx1 = x1;

             newy1 = y1;

             newx2 = x2;

             newy2 = y2;

             flag = true ;

         } else if (shape.equals( "圆" )) {

             g.drawoval(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1));

         } else if (shape.equals( "矩形" )) {

             g.drawrect(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1));

         } else if (shape.equals( "圆角矩形" )) {

             g.drawroundrect(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1), 2 , 10 );

         } else if (shape.equals( "椭圆" )) {

             g.drawoval(x1, y1, math.abs(x2 - x1), math.abs(y2 - y1));

         }

     }

     public void mouseclicked(mouseevent e) {

         if (shape.equals( "多边形" ) && flag) {

             x2 = e.getx();

             y2 = e.gety();

             if (e.getclickcount() == 2 ) {

                 g.drawline(newx1, newy1, newx2, newy2);

                 flag = false ;

             }

             g.drawline(newx2, newy2, x2, y2);

             newx2 = x2;

             newy2 = y2;

         } else if (shape.equals( "图形" )) {

             arrx[temp] = e.getx();

             arry[temp] = e.gety();

             temp++;

             if (temp == 4 ) {

                 int x = arrx[ 3 ];

                 int y = arry[ 3 ];

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

                     random ran = new random();

                     int k = ran.nextint( 3 );

                     x = (x + arrx[k]) / 2 ;

                     y = (y + arry[k]) / 2 ;

                     g.drawline(x, y, x, y);

                 }

                 temp = 0 ;

             }

         } else if (shape.equals( "立体圆" )) {

             // double a=-2,b=-2,c=-1.2,d=2;

             double a = 1.40 , b = 1.56 , c = 1.40 , d = - 6.56 ;

             double x = 0 , xo = 0 ;

             double y = 0 , yo = 0 ;

             color[] col = { color.blue, color.cyan, color.green, color.magenta,

                     color.red, color.yellow };

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

                 random r = new random(); // 增加颜色

                 int r = r.nextint(col.length);

                 g.setcolor(col[r]);

                 // x=math.sin(a*yo)-math.cos(b*xo);

                 // y=math.sin(c*xo)-math.cos(d*yo);

                 x = d * math.sin(a * xo) - math.sin(b * yo);

                 y = c * math.cos(a * xo) + math.cos(b * yo);

                 int temp_x = ( int ) (x * 50 );

                 int temp_y = ( int ) (y * 50 );

                 g.drawline(temp_x + 500 , temp_y + 300 , temp_x + 500 ,

                         temp_y + 300 );

                 xo = x;

                 yo = y;

             }

         } else if (shape.equals( "三角形" )) {

             double a = - 2 , b = - 2 , c = - 1.2 , d = 2 ;

             double x = 0 , xo = 0 ;

             double y = 0 , yo = 0 ;

             color[] col = { color.blue, color.cyan, color.green, color.magenta,

                     color.red, color.yellow };

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

                 random r = new random(); // 增加颜色

                 int r = r.nextint(col.length);

                 g.setcolor(col[r]);

                 x = math.sin(a * yo) - math.cos(b * xo);

                 y = math.sin(c * xo) - math.cos(d * yo);

                 int temp_x = ( int ) (x * 50 );

                 int temp_y = ( int ) (y * 50 );

                 g.drawline(temp_x + 500 , temp_y + 300 , temp_x + 500 ,

                         temp_y + 300 );

                 xo = x;

                 yo = y;

             }

         }

     }

     public void mousedragged(mouseevent e) {

         x2 = e.getx();

         y2 = e.gety();

         if (shape.equals( "曲线" )) {

             // g.setstroke(new basicstroke(10));

             // g.setrenderinghint(renderinghints.key_antialiasing,

             // renderinghints.value_antialias_on);

             g.drawline(x1, y1, x2, y2);

             x1 = x2;

             y1 = y2;

         } else if (shape.equals( "橡皮擦" )) {

             g.setstroke( new basicstroke( 80 ));

             g.setrenderinghint(renderinghints.key_antialiasing,

                     renderinghints.value_antialias_on);

             g.setcolor(color.white);

             g.drawline(x1, y1, x2, y2);

             x1 = x2;

             y1 = y2;

         } else if (shape.equals( "喷枪" )) {

             // g.setstroke(new basicstroke(2)); //不用加粗

             // g.setrenderinghint(renderinghints.key_antialiasing,

             // renderinghints.value_antialias_on);

             for ( int k = 0 ; k < 20 ; k++) {

                 random i = new random();

                 int a = i.nextint( 8 );

                 int b = i.nextint( 10 );

                 g.drawline(x2 + a, y2 + b, x2 + a, y2 + b);

             }

         }

     }

}

代码量也还是挺小的,因为是简单画板嘛~~

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/SX_csu2016sw/article/details/76570307

查看更多关于Java实现的简单画图板示例的详细内容...

  阅读:58次