在搜索到一个截屏软件,发现截屏时没有线条框住所选区域,请求高手帮帮忙,下面是部分源码:
package gys;import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;import javax.swing.JFrame;
import javax.swing.JLabel;public class JTFrame extends JFrame implements MouseListener,MouseMotionListener
{
int startx,starty,endx, endy; //截图的起始坐标 int x,y; //当前鼠标的位置 主要用于提示框的位置 // private int x1,y1,x2,y2;
private BackgroundImage labFullScreenImage = new BackgroundImage(); boolean state=false; //画图状态 Toolkit t=this.getToolkit().getDefaultToolkit(); // 工具包 能够找出整个屏幕的大小
Robot robot; //截取屏幕的类
BufferedImage bufImg; //用于存放全屏的图片
BufferedImage targetImg; //目标图像 用于存储截取后的图片
TestFrame tf;
public JTFrame(TestFrame tf){
super("截图窗口");
init();
this.tf=tf;
this.addMouseListener(this); //给窗口鼠标添加事件
this.addMouseMotionListener(this); //给窗口鼠标添加事件
this.setCursor(Cursor.CROSSHAIR_CURSOR);
this.setSize(t.getScreenSize());
this.setResizable(false);
this.setUndecorated(true); //显示没有标题栏
} public void init()
{
try {
robot=new Robot();
bufImg=robot.createScreenCapture(new Rectangle(t.getScreenSize())); // 把整个屏幕截下来 bufImg用于存放全屏的图片
} catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.drawImage(bufImg, 0, 0,this);
}
public void update(Graphics g)
{
paint(g);
Graphics2D g2d=(Graphics2D)g;
if(state)
{
g2d.setColor(Color.black);
g2d.fillRect(startx, endy+2, 200, 20);
g2d.setColor(Color.white);
g2d.drawString("双击任意处截取图片,按右键重画", startx+10, endy+15);
}
if(endx-startx>0 && endx-startx>0)
{
g2d.setColor(Color.red);
g2d.drawRect(startx,starty,endx-startx, endy-starty); //绘制截取框 就是那条红线
g2d.setColor(Color.black); g2d.fillRect(startx, starty-20, 115, 18);
g2d.setColor(Color.white);
g2d.drawString("宽:"+(endx-startx-1)+"px 宽:"+(endy-starty-1)+"px", startx+5, starty-7);
}
else
{
g2d.setColor(Color.black);
g2d.fillRect(x+2, y+2, 240, 20);
g2d.setColor(Color.white);
g2d.drawString("按住鼠标左键不放选取截图范围,按右键退出", x+7, y+17);
}
} public void success(){
tf.setVisible(true);
tf.targetImg=targetImg;
tf.drawImg();
// tf.update(null);
this.setVisible(false);
} public void mouseClicked(MouseEvent e) //鼠标响应事件
{
if(e.getButton()==3){
if(state){
this.startx=0;
this.starty=0;
this.endx=0;
this.endy=0;
this.setCursor(Cursor.CROSSHAIR_CURSOR);
this.repaint();
this.state=false;
}
else{
tf.setVisible(true);
this.setVisible(false);
}
}
if(e.getClickCount() == 2){
if(endx-startx>0 && endx-startx>0){
this.success();
}
}
}
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
if(!state){
startx=e.getX();
starty=e.getY();
}
}
public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub
if(!state){
if(endx-startx>0 && endx-startx>0){
targetImg=robot.createScreenCapture(new Rectangle(startx+1,starty+1,endx-startx-1, endy-starty-1));
this.setCursor(Cursor.HAND_CURSOR);
state=true;
this.repaint(); }
}
}
public void mouseDragged(MouseEvent e)
{
// TODO Auto-generated method stub
if(!state){
endx=e.getX();
endy=e.getY();
//labFullScreenImage.drawRectangle(startx,starty,endx,endy);
this.repaint();
}
} public void mouseMoved(MouseEvent e)
{
if(!state){
this.x=e.getX();
this.y=e.getY();
this.repaint();
} } public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub } public void mouseExited(MouseEvent e)
{ }
}
class BackgroundImage extends JLabel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(x,y,w,h);
String area = Integer.toString(w)+" * "+ Integer.toString(h);
g.drawString(area,x+(int)w/2-15,y+(int)h/2);
g.drawLine(lineX,0,lineX,getHeight());
g.drawLine(0,lineY,getWidth(),lineY);
} public void drawRectangle(int x,int y,int width,int height)
{
this.x = x;
this.y = y;
h = height;
w = width;
repaint();
} public void drawCross(int x,int y)
{
lineX = x;
lineY = y;
repaint();
} int lineX,lineY;
int x,y,h,w;
}
package gys;import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;import javax.swing.JFrame;
import javax.swing.JLabel;public class JTFrame extends JFrame implements MouseListener,MouseMotionListener
{
int startx,starty,endx, endy; //截图的起始坐标 int x,y; //当前鼠标的位置 主要用于提示框的位置 // private int x1,y1,x2,y2;
private BackgroundImage labFullScreenImage = new BackgroundImage(); boolean state=false; //画图状态 Toolkit t=this.getToolkit().getDefaultToolkit(); // 工具包 能够找出整个屏幕的大小
Robot robot; //截取屏幕的类
BufferedImage bufImg; //用于存放全屏的图片
BufferedImage targetImg; //目标图像 用于存储截取后的图片
TestFrame tf;
public JTFrame(TestFrame tf){
super("截图窗口");
init();
this.tf=tf;
this.addMouseListener(this); //给窗口鼠标添加事件
this.addMouseMotionListener(this); //给窗口鼠标添加事件
this.setCursor(Cursor.CROSSHAIR_CURSOR);
this.setSize(t.getScreenSize());
this.setResizable(false);
this.setUndecorated(true); //显示没有标题栏
} public void init()
{
try {
robot=new Robot();
bufImg=robot.createScreenCapture(new Rectangle(t.getScreenSize())); // 把整个屏幕截下来 bufImg用于存放全屏的图片
} catch (Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.drawImage(bufImg, 0, 0,this);
}
public void update(Graphics g)
{
paint(g);
Graphics2D g2d=(Graphics2D)g;
if(state)
{
g2d.setColor(Color.black);
g2d.fillRect(startx, endy+2, 200, 20);
g2d.setColor(Color.white);
g2d.drawString("双击任意处截取图片,按右键重画", startx+10, endy+15);
}
if(endx-startx>0 && endx-startx>0)
{
g2d.setColor(Color.red);
g2d.drawRect(startx,starty,endx-startx, endy-starty); //绘制截取框 就是那条红线
g2d.setColor(Color.black); g2d.fillRect(startx, starty-20, 115, 18);
g2d.setColor(Color.white);
g2d.drawString("宽:"+(endx-startx-1)+"px 宽:"+(endy-starty-1)+"px", startx+5, starty-7);
}
else
{
g2d.setColor(Color.black);
g2d.fillRect(x+2, y+2, 240, 20);
g2d.setColor(Color.white);
g2d.drawString("按住鼠标左键不放选取截图范围,按右键退出", x+7, y+17);
}
} public void success(){
tf.setVisible(true);
tf.targetImg=targetImg;
tf.drawImg();
// tf.update(null);
this.setVisible(false);
} public void mouseClicked(MouseEvent e) //鼠标响应事件
{
if(e.getButton()==3){
if(state){
this.startx=0;
this.starty=0;
this.endx=0;
this.endy=0;
this.setCursor(Cursor.CROSSHAIR_CURSOR);
this.repaint();
this.state=false;
}
else{
tf.setVisible(true);
this.setVisible(false);
}
}
if(e.getClickCount() == 2){
if(endx-startx>0 && endx-startx>0){
this.success();
}
}
}
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
if(!state){
startx=e.getX();
starty=e.getY();
}
}
public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub
if(!state){
if(endx-startx>0 && endx-startx>0){
targetImg=robot.createScreenCapture(new Rectangle(startx+1,starty+1,endx-startx-1, endy-starty-1));
this.setCursor(Cursor.HAND_CURSOR);
state=true;
this.repaint(); }
}
}
public void mouseDragged(MouseEvent e)
{
// TODO Auto-generated method stub
if(!state){
endx=e.getX();
endy=e.getY();
//labFullScreenImage.drawRectangle(startx,starty,endx,endy);
this.repaint();
}
} public void mouseMoved(MouseEvent e)
{
if(!state){
this.x=e.getX();
this.y=e.getY();
this.repaint();
} } public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub } public void mouseExited(MouseEvent e)
{ }
}
class BackgroundImage extends JLabel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(x,y,w,h);
String area = Integer.toString(w)+" * "+ Integer.toString(h);
g.drawString(area,x+(int)w/2-15,y+(int)h/2);
g.drawLine(lineX,0,lineX,getHeight());
g.drawLine(0,lineY,getWidth(),lineY);
} public void drawRectangle(int x,int y,int width,int height)
{
this.x = x;
this.y = y;
h = height;
w = width;
repaint();
} public void drawCross(int x,int y)
{
lineX = x;
lineY = y;
repaint();
} int lineX,lineY;
int x,y,h,w;
}
绘制指定矩形的边框。
1.package gys;
2.import java.awt.Color;
3.import java.awt.Cursor;
4.import java.awt.Graphics;
5.import java.awt.Graphics2D;
6.import java.awt.Rectangle;
7.import java.awt.Robot;
8.import java.awt.Toolkit;
9.import java.awt.event.MouseAdapter;
10.import java.awt.event.MouseEvent;
11.import java.awt.event.MouseListener;
12.import java.awt.event.MouseMotionListener;
13.import java.awt.image.BufferedImage;
14.import javax.swing.JFrame;
15.import javax.swing.JLabel;
16.public class JTFrame extends JFrame implements MouseListener,MouseMotionListener
17.{
18. int startx,starty,endx, endy; //截图的起始坐标
19. int x,y; //当前鼠标的位置 主要用于提示框的位置
20. private BackgroundImage labFullScreenImage = new BackgroundImage();
21. boolean state=false; //画图状态
22. Toolkit t=this.getToolkit().getDefaultToolkit(); // 工具包 能够找出整个屏幕的大小
23. Robot robot; //截取屏幕的类
24. BufferedImage bufImg; //用于存放全屏的图片
25. BufferedImage targetImg; //目标图像 用于存储截取后的图片
26. TestFrame tf;27. public JTFrame(TestFrame tf)
28. {
29. super("截图窗口");
30. init();
31. this.tf=tf;
32. this.addMouseListener(this); //给窗口鼠标添加事件
33. this.addMouseMotionListener(this); //给窗口鼠标添加事件
34. this.setCursor(Cursor.CROSSHAIR_CURSOR);
35. this.setSize(t.getScreenSize());
36. this.setResizable(false);
37. this.setUndecorated(true); //显示没有标题栏
38. }39. public void init()
40. {
41. try
42. {
43. robot=new Robot();
44. bufImg=robot.createScreenCapture(new Rectangle(t.getScreenSize()));
// 把整个屏幕截下来 bufImg用于存放全屏的图片
45. } catch (Exception e)
46. {
47. e.printStackTrace();
48. }
49. }
50. public void paint(Graphics g)
51. {
52. Graphics2D g2d=(Graphics2D)g;
53. g2d.drawImage(bufImg, 0, 0,this);
54. }
55. public void update(Graphics g)
56. {
57. paint(g);
58. Graphics2D g2d=(Graphics2D)g;
59. if(state)
60. {
61. g2d.setColor(Color.black);
62. g2d.fillRect(startx, endy+2, 200, 20);
63. g2d.setColor(Color.white);
64. g2d.drawString("双击任意处截取图片,按右键重画", startx+10, endy+15);
65. }
66. if(endx-startx>0 && endx-startx>0)
67. {
68. g2d.setColor(Color.red);
69. g2d.drawRect(startx,starty,endx-startx, endy-starty);
//绘制截取框 就是那条红线
70. g2d.setColor(Color.black);
71. g2d.fillRect(startx, starty-20, 115, 18);
73. g2d.setColor(Color.white);
74. g2d.drawString("宽:"+(endx-startx-1)+"px 宽:"+(endy-starty-1)+"px", startx+5, starty-7);
75. }
76. else
77. {
78. g2d.setColor(Color.black);
79. g2d.fillRect(x+2, y+2, 240, 20);
80. g2d.setColor(Color.white);
81. g2d.drawString("按住鼠标左键不放选取截图范围,按右键退出", x+7, y+17);
82. }
83. }84. public void success()
85. {
86. tf.setVisible(true);
87. tf.targetImg=targetImg;
88. tf.drawImg();
89. this.setVisible(false);
90. }91. public void mouseClicked(MouseEvent e) //鼠标响应事件
92. {
93. if(e.getButton()==3)
94. {
95. if(state){
96. this.startx=0;
97. this.starty=0;
98. this.endx=0;
99. this.endy=0;
100. this.setCursor(Cursor.CROSSHAIR_CURSOR);
101. this.repaint();
102. this.state=false;
103. }
104. else{
105. tf.setVisible(true);
106. g2d.drawRect(startx,starty,endx-startx, endy-starty);
//绘制截取框 就是那条红线
107. this.setVisible(false);
108. }
109. }
110. if(e.getClickCount() == 2){
111. if(endx-startx>0 && endx-startx>0){
112. this.success();
113. }
114. }
115. }116. public void mousePressed(MouseEvent e)
117. {
118. if(!state){
119. startx=e.getX();
120. starty=e.getY();
121. }
122. }123. public void mouseReleased(MouseEvent e)
124. {
125. if(!state){
126. if(endx-startx>0 && endx-startx>0)
127. {
128. targetImg=robot.createScreenCapture(new Rectangle(startx+1,starty+1,endx-startx-1, endy-starty-1));
129. this.setCursor(Cursor.HAND_CURSOR);
130. state=true;
131. this.repaint();
132. }
133. }
134. }135. public void mouseDragged(MouseEvent e)
136. {
137. if(!state){
138. endx=e.getX();
139. endy=e.getY();
140. this.repaint();
141. }
142. }143. public void mouseMoved(MouseEvent e)
144. {
145. if(!state){
146. this.x=e.getX();
147. this.y=e.getY();
148. this.repaint();
149. }
150. }
151. public void mouseEntered(MouseEvent e)
152. {
153. }154. public void mouseExited(MouseEvent e)
155. {156. }157.}
158. class BackgroundImage extends JLabel
159. {
160. public void paintComponent(Graphics g)
161. {
162. super.paintComponent(g);
163. g.drawRect(x,y,w,h);
164. String area = Integer.toString(w)+" * "+ Integer.toString(h);
165. g.drawString(area,x+(int)w/2-15,y+(int)h/2);
166. g.drawLine(lineX,0,lineX,getHeight());
167. g.drawLine(0,lineY,getWidth(),lineY);
168. }169. public void drawRectangle(int x,int y,int width,int height)
170. {
171. this.x = x;
172. this.y = y;
173. h = height;
174. w = width;
175. repaint();
176. }177. public void drawCross(int x,int y)
178. {
179. lineX = x;
180. lineY = y;
190. repaint();
191. }192. int lineX,lineY;
193. int x,y,h,w;
194. }这是其中一个 .java 文件,麻烦大家帮帮忙呀!