Q

  • タグ:
  • タグはありません
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Search extends PicSave {
	

	
	private int x1=0;
	private int y1=0;
	private int w1=0;
	private int h1=0;
	
	Search(){
		x1 = 0;
	}
	public void SetVal(int p1){
		x1 = p1;
	}
	
	JFrame frame;
	JPanel container;
	CanvasPane canvas;

	Point pt_from, pt_to;
	static final Point nullpoint = new Point(-1, -1);

	Search (String title) {

		/*
		 * ウィンドウ周辺のGUI初期設定
		 */
		frame = new JFrame(title);
		frame.setSize(300, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		container = (JPanel) frame.getContentPane();

		/*
		 * 描画の条件判定に使うオブジェクト
		 */
		pt_from = new Point(nullpoint);
		pt_to = new Point(nullpoint);

		canvas = new CanvasPane();
		MouseAdapter madp = new SelectiongMouseListener();
		canvas.addMouseListener(madp);
		canvas.addMouseMotionListener(madp);
		container.add(canvas);

		frame.setVisible(true);

		/*
		 * 50ミリ秒間隔でアニメーションを描かせるスレッド
		 */
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new FrameDrawer(), 0, 50);
	}

	class CanvasPane extends JPanel {

		BufferedImage image;
		TexturePaint tp;

		Color pale = new Color(0, 0, 255, 110);
		Color transparent = new Color(0, 0, 0, 0);

		CanvasPane () {
			super();
			this.setBackground(Color.white);
			this.setOpaque(true);
			
			/*
			 * 描画キャンバスとなるパネルの生成段階(コンストラクタ)で、
			 *  以後繰返し利用するテクスチャ・オブジェクトを用意する
			 */
			int len = 50;
			
			image = new BufferedImage(len, len, BufferedImage.TYPE_INT_ARGB);
			Graphics2D gbi = image.createGraphics();
			gbi.setColor(transparent);
			gbi.fillRect(0, 0, len, len );

			gbi.setColor(pale);
			int unit = len / 2;
			for (int i = 0; i < 2; i++) {
				gbi.fillRect(unit * i, unit * i, unit, unit);
			}

			Rectangle rect = new Rectangle(0, 0, len, len );
			tp = new TexturePaint(image, rect);

		}

		public void paintComponent (Graphics g) {
			super.paintComponent(g);	//バックグラウンド色による初期化なども含まれる
			Graphics2D g2 = (Graphics2D)g;
			//画像の取得
			 BufferedImage readImage = null;
			    try {
			      readImage = ImageIO.read(new File("10.jpg"));
			    } catch (Exception e) {
			      e.printStackTrace();
			      readImage = null;
			    }

			    if (readImage != null){
			      g2.drawImage(readImage, 0, 0, this);
			    }

			    
			if (!pt_from.equals(nullpoint) && !pt_to.equals(nullpoint) ) {
				/*
				 * 始点・終点双方の条件をチェックするのは、mousePressedだけされて
				 *  mouseMovedが生じていない場合に描画を見送るため
				 */
			    
				int x = Math.min(pt_from.x, pt_to.x);
				int y = Math.min(pt_from.y, pt_to.y);
				int w = Math.abs(pt_to.x – pt_from.x);
				int h = Math.abs(pt_to.y – pt_from.y);

				//g2.setPaint(tp);
				//g2.fillRect(x, y, w, h);	//テクスチャを先に描画

				g2.setColor(Color.red);
				for (int i = 0; i < 2; i++) {	//青色で線幅3ピクセルの枠を描く
					g2.drawRect(x + i, y + i, w – 2 * i, h – 2 * i);
					//xの始点とyの始点、画像の大きさの取得
					x1=x+i;
					y1=y+i;
					w1=w-2*i;
					h1=h-2*i;
					System.out.println("1は"+x1);
					return x1;
				}
			}
		}
	}
	public int GetVal(){
		System.out.println("GetValは"+x1);
		return x1;
	}




	class SelectiongMouseListener extends MouseAdapter {

		@Override
		public void mousePressed (MouseEvent ev) {
			pt_from.setLocation(ev.getPoint() );
		}

		@Override
		public void mouseReleased (MouseEvent ev) {
			 System.out.println("xの始点は="+x1+"yの始点は="+y1+"画像の横は="+ w1+"画像の縦は="+h1);
			//pt_from.setLocation(nullpoint);
			//pt_to.setLocation(nullpoint);
			PicSave save;
			save = new PicSave();
            save.doCapture();
		}

		@Override
		public void mouseDragged (MouseEvent ev) {
			pt_to.setLocation(ev.getPoint() );
		}

	}

	class FrameDrawer extends TimerTask {	//50ミリ秒おきに描画

		@Override
		public void run() {
			canvas.repaint();
		}

	}
	

	public static void main(String[] args) {
		new Search("Search");
	}

}