teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

ソースコードの編集

2015/07/22 08:09

投稿

junn
junn

スコア65

title CHANGED
File without changes
body CHANGED
@@ -5,14 +5,9 @@
5
5
  実際に追加するソースコード等を教えていただけると
6
6
  助かります。
7
7
 
8
-
9
-
10
-
11
8
  MainInit
12
9
  ```lang-Java
13
-
14
-
15
- public class MainInit implements ActionListener , WindowListener { //impriments(組み込む)
10
+ public class MainInit implements ActionListener, MouseListener, MouseMotionListener{ //impriments(組み込む)
16
11
  private JFrame mainFrame;
17
12
  private Container contentPane;
18
13
  private JPanel itemPanel;
@@ -25,81 +20,68 @@
25
20
  private String folderpath;
26
21
  private JLabel folderlabel;
27
22
 
28
- private SettingWindow settingwindow;
29
23
 
30
24
  private ItemBox itemboxtest1;
31
25
  private ItemBox itemboxtest2;
32
26
  private ItemBox itemboxtest3;
33
27
  private ItemBox itemboxtest4;
34
28
 
29
+ private Thumbnail DrawPanel;
35
30
 
31
+ private JFrame mainFrame2;
32
+ private Point startPoint;
33
+ private Point endPoint;
34
+ private int x;
35
+ private int y;
36
36
  public MainInit(){
37
37
  mainFrame = new JFrame("エビデンスキャプチャ");
38
- mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //終了方法
38
+ mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setSize(500, 300); mainFrame.setLocationRelativeTo(null); contentPane = mainFrame.getContentPane();
39
- mainFrame.setSize(500, 300); //ウィンドウのサイズ
40
- mainFrame.setLocationRelativeTo(null); //ウィンドウを画面の中央に表示
41
- contentPane = mainFrame.getContentPane(); //要素、成分 メインフレームのパネルを取得
42
39
 
43
-
44
40
  contentPane.setLayout(null);
45
41
 
46
42
 
47
- dirButton = new JButton("Dir"); //Dirボタンの作成 クラス名( ) = コンストラクタ
43
+ dirButton = new JButton("Dir"); dirButton.setBounds(20, 0, 80, 30);
48
- dirButton.setBounds(20, 0, 80, 30);
49
- contentPane.add(dirButton); //コンパネにボタンを追加
50
-
51
- addButton = new JButton("Capture");
44
+ contentPane.add(dirButton); addButton = new JButton("Capture");
52
45
  addButton.setBounds(400, 0, 90, 60);
53
46
  contentPane.add(addButton);
54
47
 
55
48
 
56
49
 
50
+
51
+
57
52
  JMenuBar menubar = new JMenuBar();
58
- JMenu menu1 = new JMenu("設定"); //メニューの生成
53
+ JMenu menu1 = new JMenu("設定"); JMenu menu2 = new JMenu("ヘルプ");
59
- JMenu menu2 = new JMenu("ヘルプ");
60
54
  JMenuItem menuitem1 = new JMenuItem("設定");
61
55
 
62
56
  menubar.add(menu1);
63
57
  menubar.add(menu2);
64
58
  menu1.add(menuitem1);
65
59
 
66
- mainFrame.getRootPane().setJMenuBar(menubar); //メインフレームに対してメニューバーを追加
60
+ mainFrame.getRootPane().setJMenuBar(menubar);
67
-
68
61
  folderlabel = new JLabel();
69
- textField = new JTextField(); //オブジェクトの作成
62
+ textField = new JTextField();
70
-
71
-
72
63
  //「追加」ボタンとアクション・リスナーの関連付け
73
64
  dirButton.addActionListener(this);
74
- dirButton.setActionCommand("Dir"); // イベントに対するコマンド文字列登録
65
+ dirButton.setActionCommand("Dir"); addButton.addActionListener(this);
75
- addButton.addActionListener(this);
76
- addButton.setActionCommand("Capture"); // イベントに対するコマンド文字列登録
66
+ addButton.setActionCommand("Capture")
77
67
 
78
68
 
79
- menuitem1.addActionListener(this);
80
- menuitem1.setActionCommand("settingwindow"); // イベントに対するコマンド文字列登録
81
69
 
82
-
83
- itemPanel = new JPanel(); //上下
70
+ itemPanel = new JPanel(); itemPanel.setBounds(0, 0, 500, 100);
84
- itemPanel.setBounds(0, 0, 500, 100);
85
71
  contentPane.add(itemPanel);
86
- //itemPanel.setBackground(Color.RED);
87
-
88
- textPanel = new JPanel(); //directryfield dir add
72
+ textPanel = new JPanel(); textPanel.setLayout(null);
89
- textPanel.setLayout(null);
90
73
  contentPane.add(textPanel);
91
74
  textPanel.setBounds(0,80, 500, 100);
92
- //textPanel.setBackground(Color.GREEN);
75
+
93
-
94
76
  buttonPanel = new JPanel();
95
77
 
96
78
  buttonPanel.setLayout(null);
97
79
  contentPane.add(buttonPanel);
98
80
  buttonPanel.setBounds(0,180,500,200);
99
- //buttonPanel.setBackground(Color.ORANGE);
81
+ ;
100
82
 
101
83
 
102
- itemboxtest1 = new ItemBox(); //□↑↓
84
+ itemboxtest1 = new ItemBox();
103
85
  itemPanel.add(itemboxtest1);
104
86
 
105
87
 
@@ -124,55 +106,60 @@
124
106
 
125
107
 
126
108
 
127
- // フォルダパス初期値
109
+
110
+
128
111
  folderpath = "C:\\tmp";
129
- // folderlabel,textFieldを初期値設定
130
- folderlabel.setText(folderpath);
112
+ folderlabel.setText(folderpath);
131
113
 
132
- textField.setText("UT_FIL_%d-%d-%d-%d"); //初期表示フォルダパス
114
+ textField.setText("UT_CNT_%d-%d-%d-%d"); mainFrame.setVisible(true);
133
-
134
-
135
- mainFrame.setVisible(true);
136
115
  screenShot = new ScreenShot();
137
116
 
138
117
  }
139
118
 
140
119
 
141
- // 利用者の操作に応じた処理を実装
142
- public void actionPerformed(ActionEvent event){
143
- // コマンド文字列を取得
144
- String cmd = event.getActionCommand();
145
120
 
121
+
122
+ public void actionPerformed(ActionEvent event){
123
+ String cmd = event.getActionCommand();
124
+
146
125
  if(cmd.equals("Capture")){
126
+ beforeProcess();
147
127
  int x = mainFrame.getX();
148
128
  int y = mainFrame.getY();
149
- // 画面キャプチャー
129
+
130
+
131
+
132
+
150
133
  try{
151
134
  File filepath = FilePathCreate();
152
- // 自分を消す(見えない位置に移動)
135
+
153
136
  mainFrame.setLocation(-1000,-1000);
154
- // 画面キャプチャ
137
+
155
- screenShot.screenCapture(filepath);
138
+ screenShot.screenCapture(filepath, endPoint, endPoint);
156
139
 
157
- // 自分を表示(元の位置に移動)
140
+
158
141
  mainFrame.setLocation(x, y);
159
142
 
160
- //new Thumbnail(filepath); //サムネイルの表示
143
+ new Thumbnail(filepath);
161
144
 
162
-
163
145
  } catch (AWTException e){
164
- // TODO 自動生成された catch ブロック
146
+
165
147
  e.printStackTrace();
166
148
  } catch (IOException e){
167
- // TODO 自動生成された catch ブロック
168
- e.printStackTrace();
149
+ e.printStackTrace();
169
150
  }
151
+
152
+
170
153
  }
154
+
155
+
171
156
  else if(cmd.equals("Dir")) {
172
157
 
173
158
 
174
- JFileChooser filechooser = new JFileChooser(folderpath); //上二行と相互関係
159
+
175
160
 
161
+
162
+ JFileChooser filechooser = new JFileChooser(folderpath);
176
163
  filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
177
164
 
178
165
  int selected = filechooser.showSaveDialog(this.contentPane);
@@ -183,15 +170,9 @@
183
170
 
184
171
 
185
172
  }
173
+ }
186
174
 
187
- else if(cmd.equals("settingwindow")){
188
175
 
189
-
190
- settingwindow = new SettingWindow();
191
- }
192
- }
193
-
194
-
195
176
  }
196
177
 
197
178
 
@@ -208,9 +189,10 @@
208
189
  name = filename + "(" + m + ")" ;
209
190
  } while (m < 100); // 無限ループ防止
210
191
  throw new IllegalStateException("パス生成失敗");
192
+
211
193
  }
212
194
 
213
- // 数値リストからファイル名を生成
195
+
214
196
  public String FileNameCreate() {
215
197
  String filename;
216
198
  String[] itemNumbers = new String[4];
@@ -233,122 +215,132 @@
233
215
  }
234
216
 
235
217
 
236
- public void windowDeactivated(WindowEvent we) { //非アクティブのイベント
218
+ public void windowDeactivated(WindowEvent we) { System.out.println("windowClosing");
237
- System.out.println("windowClosing");
238
219
 
239
220
  }
240
-
241
- // アプリケーションの起動
242
221
  public static void main(String[] args) {
243
222
  new MainInit();
244
223
  }
245
- }
246
- ```
247
224
 
225
+ @Override
226
+ public void mouseClicked(MouseEvent e) {
227
+ }
248
228
 
249
- ItemBox
250
- ```lang-java
251
- public class ItemBox extends JPanel implements ActionListener{ //ItemBoxはJPanelというオブジェクトクラスを継承
252
- //ActionListnerというインタフェースを実装
253
- private JButton upButton;
254
- private JButton downButton;
255
- private JTextField itemField;
256
- private Container contentPane;
257
- private JPanel itemPanel;
258
229
 
259
- GridBagLayout gbl = new GridBagLayout();
260
230
 
261
231
 
232
+ @Override
233
+ public void mousePressed(MouseEvent e) {
234
+ startPoint = e.getPoint();
262
235
 
263
- void addButton(Object itemobject, int x, int y, int w, int h) {
264
- GridBagConstraints gbc = new GridBagConstraints();
265
- gbc.fill = GridBagConstraints.BOTH;
266
- gbc.gridx = x;
267
- gbc.gridy = y;
268
- gbc.gridwidth = w;
269
- gbc.gridheight = h;
270
- gbl.setConstraints((Component) itemobject, gbc);
271
- add((Component) itemobject);
272
- }
236
+ }
273
237
 
274
- public ItemBox(){
275
238
 
276
- //setLayout(null);
277
- //setBackground(Color.blue);
278
239
 
279
- itemField = new JTextField("0");
280
- itemField.setPreferredSize(new Dimension(20, 10)); //アイテムフィールドの大きさ指定
281
- itemField.setFocusable(false); //入力制限
282
240
 
241
+ @Override
283
- upButton = new JButton("↑"); //upButtonオブジェクトを作成し、↑を代入する。
242
+ public void mouseReleased(MouseEvent e) { /
284
- downButton = new JButton("↓"); //downButtonオブジェクトを作成し、↓を代入する。
243
+ endPoint = e.getPoint();
244
+ afterProcess();
285
245
 
286
-
246
+ }
287
247
 
288
- upButton.setActionCommand("upButton");
289
- upButton.addActionListener(this); //アクションイベントを受け取れるようになる。
290
- downButton.setActionCommand("downButton");
291
- downButton.addActionListener(this);
292
248
 
293
- setLayout(gbl);
294
- addButton(itemField, 0, 0, 1, 2); // (0, 0) 幅=1, 高さ=3
295
- addButton(upButton, 1, 0, 1, 1); // (1, 0) 幅=1, 高さ=1
296
- addButton(downButton, 1, 1, 1, 1); // (1, 1) 幅=1, 高さ=1
297
249
 
298
250
 
251
+ @Override
252
+ public void mouseEntered(MouseEvent e) {
299
253
 
300
- }
254
+ }
301
255
 
302
256
 
303
257
 
304
- public String getText(){
305
258
 
259
+ @Override
260
+ public void mouseExited(MouseEvent e) {
261
+
306
262
 
263
+ }
307
- return itemField.getText();
264
+ public void beforeProcess() {
308
265
 
266
+ x = mainFrame.getX();
267
+ y = mainFrame.getY();
268
+ mainFrame.setLocation(-1000,-1000);
309
269
 
310
- }
311
270
 
271
+ JFrame.setDefaultLookAndFeelDecorated(false);
272
+ mainFrame2 = new JFrame();
273
+ mainFrame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
274
+ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
275
+ mainFrame2.setSize(screenSize.width, screenSize.height);
276
+ JPanel p = new JPanel();
277
+ p.setBackground(new Color(.5f,.5f,.5f,.5f));
278
+ mainFrame2.getContentPane().add(p);
312
279
 
313
- //操作に応じた処理の実装
314
- public void actionPerformed(ActionEvent event){ //イベントが発生するとここが処理される
315
- //ユーザーの操作e対象の判断 //このクラスでのアクションリスナー
316
- String cmd = event.getActionCommand();
280
+ mainFrame2.setLocationRelativeTo(null);
281
+ mainFrame2.setUndecorated(true);
317
282
 
318
- if(cmd.equals("upButton")) {
319
- int i = Integer.parseInt(itemField.getText());
320
- i++;
321
- if(i>99)i=0;
322
- //itemField.setText(String.format("%1$02d",i)); //0埋め
283
+ com.sun.awt.AWTUtilities.setWindowOpaque(mainFrame2, false); //半透明化
284
+ mainFrame2.addMouseListener(this);
323
- itemField.setText(String.valueOf(i));
285
+ mainFrame2.addMouseMotionListener(this);
286
+ mainFrame2.setVisible(true);
287
+
288
+ }
324
289
 
290
+ public void afterProcess() {
291
+ mainFrame2.dispose();
292
+ File filepath = FilePathCreate();
293
+ try {
294
+ screenShot.screenCapture(filepath,startPoint,endPoint);
295
+ } catch (AWTException e1) {
296
+ // TODO 自動生成された catch ブロック
297
+ e1.printStackTrace();
298
+ } catch (IOException e1) {
299
+ // TODO 自動生成された catch ブロック
300
+ e1.printStackTrace();
325
- }
301
+ }
326
- else if(cmd.equals("downButton")){
302
+ // 自分を表示(元の位置に移動)
327
- int i = Integer.parseInt(itemField.getText());
328
- i--;
329
- if(i<0)i=99;
330
- //itemField.setText(String.format("%1$02d",i)); //0埋め
331
- itemField.setText(String.valueOf(i));
303
+ mainFrame.setLocation(x, y);
304
+ }
332
305
 
333
- }
334
- }
335
306
 
336
307
 
337
308
 
309
+ @Override
310
+ public void mouseDragged(MouseEvent e) {
311
+
338
312
  }
339
313
 
340
314
 
315
+
316
+
317
+ @Override
318
+ public void mouseMoved(MouseEvent e) {
319
+
320
+ }
321
+
322
+ }
323
+
324
+
325
+
326
+
327
+
341
328
  ```
342
329
 
330
+
343
331
  ScreenShot
344
332
  ```lang-java
333
+
345
334
  public class ScreenShot {
346
335
 
347
- public void screenCapture(File filepath) throws AWTException, IOException {
336
+ public void screenCapture(File filepath, Point startPoint, Point endPoint) throws AWTException, IOException {
348
337
  Robot robot = new Robot();
349
338
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
350
339
  BufferedImage image = robot.createScreenCapture(
351
- new Rectangle(0, 0, screenSize.width, screenSize.height));
340
+ new Rectangle( (int)startPoint.getX(),
341
+ (int)startPoint.getY(),
342
+ (int )(endPoint.getX() - startPoint.getX()),
343
+ (int)(endPoint.getY() - startPoint.getY())));
352
344
  ImageIO.write(image, "PNG", filepath); //PNGファイルの保存
353
345
  }
354
346
  }

1

コードの変更

2015/07/22 08:08

投稿

junn
junn

スコア65

title CHANGED
File without changes
body CHANGED
File without changes