Appletスクリーンショットアップロード(3)

3735 ワード

	public void mouseReleased(MouseEvent e) {
					if (isDrag) {
						xEnd = e.getX();
						yEnd = e.getY();
						if (x > xEnd) {
							int temp = x;
							x = xEnd;
							xEnd = temp;
						}
						if (y > yEnd) {
							int temp = y;
							y = yEnd;
							yEnd = temp;
						}
						x += 3;
						y += 3;
						xEnd -= 2;
						yEnd -= 2;
						try {
							BufferedImage image = (BufferedImage) ScreenImage
									.getScreenImage(x, y, xEnd - x, yEnd - y);
							Date date = new Date();
							String time = String.valueOf(date.getTime());
							String targetUrl = "http://127.0.0.1/screencapture/upload?fileName="
									+ time;
							String tmpPath = "c:/" + time + ".jpg";
							File tmpFile = new File(tmpPath);
							if (tmpFile.exists()) {
								tmpFile.delete();
							}
							tmpFile.createNewFile();
							ImageIO.write(image, "jpeg", tmpFile);
							PostMethod filePost = new PostMethod(targetUrl);
							try {
								Part[] parts = { new FilePart(
										tmpFile.getName(), tmpFile) };
								filePost
										.setRequestEntity(new MultipartRequestEntity(
												parts, filePost.getParams()));
								HttpClient client = new HttpClient();
								client.getHttpConnectionManager().getParams()
										.setConnectionTimeout(5000);
								int status = client.executeMethod(filePost);
								String response = filePost
										.getResponseBodyAsString();
								if (status == HttpStatus.SC_OK) {
									JOptionPane
											.showMessageDialog(null, "    !");
								} else {
									JOptionPane
											.showMessageDialog(null, "    !");
								}
								tmpFile.delete();
								JSObject win = JSObject.getWindow(outer);
								win.call("complete", new Object[]{time});
							} catch (Exception ee) {
								JOptionPane.showConfirmDialog(null, ee
										.getMessage(), "    ",
										JOptionPane.DEFAULT_OPTION,
										JOptionPane.ERROR_MESSAGE);
							} finally {
								filePost.releaseConnection();
							}
						} catch (Exception ex) {
							JOptionPane.showConfirmDialog(null,
									ex.getMessage(), "    ",
									JOptionPane.DEFAULT_OPTION,
									JOptionPane.ERROR_MESSAGE);
						}
						dispose();
					}
				}
			});
			this.addMouseMotionListener(new MouseMotionListener() {
				public void mouseDragged(MouseEvent e) {
					if (!isDrag) {
						isDrag = true;
					} else {
						xEnd = e.getX();
						yEnd = e.getY();
						repaint();
					}
				}

				public void mouseMoved(MouseEvent e) {
				}
			});
			this.setUndecorated(true);
			this.setSize(screenDims.width, screenDims.height);
			this.setVisible(true);
			this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		}

		public void paint(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.setStroke(new BasicStroke(5.0f));
			super.paint(g2);
			g2.setColor(new Color(255, 0, 0));
			int height = yEnd - y;
			int width = xEnd - x;
			g2.drawLine(x, y, x, y + height);
			g2.drawLine(x, y, x + width, y);
			g2.drawLine(x, y + height, x + width, y + height);
			g2.drawLine(x + width, y, x + width, y + height);
		}
	}
}