質問編集履歴

2

ソースの追加

2018/11/08 02:10

投稿

yasu_abu7
yasu_abu7

スコア12

test CHANGED
File without changes
test CHANGED
@@ -8,4 +8,578 @@
8
8
 
9
9
  *追記*
10
10
 
11
- パネルにpaintComponentで三角錐の展開図を定義しているものは、常時表示したいと思っています。
11
+ パネルにpaintComponentで三角錐の展開図を定義しているものは、常時表示したいと思っています。ソースは以下になります。
12
+
13
+ 色データ反映ボタンを押したら表示している展開図に画像から抽出した色を反映させたいです。
14
+
15
+
16
+
17
+ ```java
18
+
19
+ public class GamenSeni extends JFrame {
20
+
21
+ static GamenSeni frame;
22
+
23
+ JButton button5;
24
+
25
+ static int S[][] = new int[4][9];
26
+
27
+
28
+
29
+ String[][]V = {
30
+
31
+ {"0","0","0","0","0","0","0","0","0"},
32
+
33
+ {"0","0","0","0","0","0","0","0","0"},
34
+
35
+ {"0","0","0","0","0","0","0","0","0"},
36
+
37
+ {"0","0","0","0","0","0","0","0","0"}
38
+
39
+ };
40
+
41
+ int cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0;
42
+
43
+
44
+
45
+ /* 色を抽出するための点の座標を定義している場所 */
46
+
47
+ static int x[] = {205,260,320,390,440,265,320,380,320};
48
+
49
+ static int y[] = {165,195,170,185,165,270,290,270,360};
50
+
51
+ /*
52
+
53
+ 文字の制限につきx1[]~x36[],y1[]~y36[]は展開図に使用する座標が入った配列
54
+
55
+ */
56
+
57
+ BufferedImage imagehairetu[] = new BufferedImage[4];
58
+
59
+ public static void main(String args[]) {
60
+
61
+ //最初に生成されるフレームの画面の名前
62
+
63
+ frame = new GamenSeni("GUI_Sample");
64
+
65
+ //表示させるためにtrueを定義している
66
+
67
+ frame.setVisible(true);
68
+
69
+ System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
70
+
71
+
72
+
73
+ }
74
+
75
+
76
+
77
+ public GamenSeni(String title){
78
+
79
+ super();
80
+
81
+ setTitle(title);
82
+
83
+ setBounds(550,250,800,600);
84
+
85
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
86
+
87
+
88
+
89
+ //最初の画面
90
+
91
+ JPanel p = new JPanel();
92
+
93
+ JPanel p1 = new JPanel(){
94
+
95
+
96
+
97
+ public void paintComponent(Graphics g){
98
+
99
+
100
+
101
+ super.paintComponent(g);
102
+
103
+ Font fg = new Font("SandSerif",Font.PLAIN,20);
104
+
105
+
106
+
107
+ //drawPolygon
108
+
109
+ //fillPolygon
110
+
111
+ /* 抽出した色を三角の中に反映している場所 */
112
+
113
+
114
+
115
+ /*枠線*/
116
+
117
+ Graphics2D g2 = (Graphics2D)g;
118
+
119
+ BasicStroke stroke = new BasicStroke(2.0f);
120
+
121
+ g2.setStroke(stroke);
122
+
123
+ /*1面*/
124
+
125
+ g.drawPolygon(x1, y1, 4);
126
+
127
+ g.drawPolygon(x2, y2, 4);
128
+
129
+ g.drawPolygon(x3, y3, 4);
130
+
131
+ g.drawPolygon(x4, y4, 4);
132
+
133
+ g.drawPolygon(x5, y5, 4);
134
+
135
+ g.drawPolygon(x6, y6, 4);
136
+
137
+ g.drawPolygon(x7, y7, 4);
138
+
139
+ g.drawPolygon(x8, y8, 4);
140
+
141
+ g.drawPolygon(x9, y9, 4);
142
+
143
+ /*2面*/
144
+
145
+ g.drawPolygon(x10, y10, 4);
146
+
147
+ g.drawPolygon(x11, y11, 4);
148
+
149
+ g.drawPolygon(x12, y12, 4);
150
+
151
+ g.drawPolygon(x13, y13, 4);
152
+
153
+ g.drawPolygon(x14, y14, 4);
154
+
155
+ g.drawPolygon(x15, y15, 4);
156
+
157
+ g.drawPolygon(x16, y16, 4);
158
+
159
+ g.drawPolygon(x17, y17, 4);
160
+
161
+ g.drawPolygon(x18, y18, 4);
162
+
163
+ /*3面*/
164
+
165
+ g.drawPolygon(x19, y19, 4);
166
+
167
+ g.drawPolygon(x20, y20, 4);
168
+
169
+ g.drawPolygon(x21, y21, 4);
170
+
171
+ g.drawPolygon(x22, y22, 4);
172
+
173
+ g.drawPolygon(x23, y23, 4);
174
+
175
+ g.drawPolygon(x24, y24, 4);
176
+
177
+ g.drawPolygon(x25, y25, 4);
178
+
179
+ g.drawPolygon(x26, y26, 4);
180
+
181
+ g.drawPolygon(x27, y27, 4);
182
+
183
+ /*4面*/
184
+
185
+ g.drawPolygon(x28, y28, 4);
186
+
187
+ g.drawPolygon(x29, y29, 4);
188
+
189
+ g.drawPolygon(x30, y30, 4);
190
+
191
+ g.drawPolygon(x31, y31, 4);
192
+
193
+ g.drawPolygon(x32, y32, 4);
194
+
195
+ g.drawPolygon(x33, y33, 4);
196
+
197
+ g.drawPolygon(x34, y34, 4);
198
+
199
+ g.drawPolygon(x35, y35, 4);
200
+
201
+ g.drawPolygon(x36, y36, 4);
202
+
203
+ }
204
+
205
+
206
+
207
+ File file = new File("rgby1.jpg");
208
+
209
+ if(file.exists()){
210
+
211
+ System.out.println("保存した画像があります");
212
+
213
+ if(file.canRead()){
214
+
215
+ System.out.println("画像を読み込めれれるよ!");
216
+
217
+ for(int i=0;i<4;i++){
218
+
219
+ try {
220
+
221
+ imagehairetu[i] = (BufferedImage) ImageIO.read(file);
222
+
223
+ } catch (IOException e) {
224
+
225
+ e.printStackTrace();
226
+
227
+ }
228
+
229
+ System.out.println(i+"番目の要素:"+imagehairetu);
230
+
231
+ }
232
+
233
+ System.out.println("画像を読みこんだよ!");
234
+
235
+ System.out.println("配列に画像を入れたよ!");
236
+
237
+
238
+
239
+ }else{
240
+
241
+ System.out.println("画像を読み込めないよ!");
242
+
243
+ }
244
+
245
+ }else{
246
+
247
+ System.out.println("画像がないよ!");
248
+
249
+ }
250
+
251
+
252
+
253
+ p1.setOpaque(false);
254
+
255
+ p1.setSize(new Dimension(800,600 ));
256
+
257
+ p.setBackground(Color.WHITE);
258
+
259
+ p1.setBackground(Color.white);
260
+
261
+ //自分で場所を決めるためのnull
262
+
263
+ p.setLayout(null);
264
+
265
+ p1.setLayout(null);
266
+
267
+
268
+
269
+ /* テキストボックスを生成している場所 */
270
+
271
+ button5 = new JButton("色データ反映");
272
+
273
+ button5.setBounds(230,10,150,100);
274
+
275
+ button5.addActionListener(
276
+
277
+ new ActionListener(){
278
+
279
+ public void actionPerformed(ActionEvent event){
280
+
281
+ int[][] arr = colorhairetu.getmen(0,4);
282
+
283
+ S = arr;
284
+
285
+
286
+
287
+ for(int o = 0; o < S.length;o++){
288
+
289
+ for(int y = 0; y< S[o].length;y++){
290
+
291
+ V[o][y] = String.valueOf(S[o][y]);
292
+
293
+ //System.out.println(V[o][y]);
294
+
295
+
296
+
297
+ }
298
+
299
+ }
300
+
301
+
302
+
303
+ for(String[] ksa : V){
304
+
305
+ for(String num : ksa){
306
+
307
+ if("1".equals(num)){cnt1++;}
308
+
309
+ if("2".equals(num)){cnt2++;}
310
+
311
+ if("3".equals(num)){cnt3++;}
312
+
313
+ if("4".equals(num)){cnt4++;}
314
+
315
+ }
316
+
317
+ }
318
+
319
+ repaint();
320
+
321
+ //update(paint);
322
+
323
+ }});
324
+
325
+ p.add(button5);//色反映ボタ
326
+
327
+ p.add(p1);
328
+
329
+
330
+
331
+ Container contentPane = getContentPane();
332
+
333
+ getContentPane().add(p, BorderLayout.CENTER);
334
+
335
+
336
+
337
+ }
338
+
339
+ public void paint(Graphics h){
340
+
341
+
342
+
343
+ super.paintComponent(h);
344
+
345
+ /* 色を抽出する場所 */
346
+
347
+ /* 一面 */
348
+
349
+ Color color1 = new Color(imagehairetu[0].getRGB(x[0], y[0]));
350
+
351
+ Color color2 = new Color(imagehairetu[0].getRGB(x[1], y[1] ));
352
+
353
+ Color color3 = new Color(imagehairetu[0].getRGB(x[2], y[2]));
354
+
355
+ Color color4 = new Color(imagehairetu[0].getRGB(x[3], y[3]));
356
+
357
+ Color color5 = new Color(imagehairetu[0].getRGB(x[4], y[4]));
358
+
359
+ Color color6 = new Color(imagehairetu[0].getRGB(x[5], y[5] ));
360
+
361
+ Color color7 = new Color(imagehairetu[0].getRGB(x[6], y[6] ));
362
+
363
+ Color color8 = new Color(imagehairetu[0].getRGB(x[7], y[7] ));
364
+
365
+ Color color9 = new Color(imagehairetu[0].getRGB(x[8], y[8] ));
366
+
367
+ /* 二面 */
368
+
369
+ Color color10 = new Color(imagehairetu[1].getRGB(x[0], y[0]));
370
+
371
+ Color color11 = new Color(imagehairetu[1].getRGB(x[1], y[1]));
372
+
373
+ Color color12 = new Color(imagehairetu[1].getRGB(x[2], y[2]));
374
+
375
+ Color color13 = new Color(imagehairetu[1].getRGB(x[3], y[3]));
376
+
377
+ Color color14 = new Color(imagehairetu[1].getRGB(x[4], y[4]));
378
+
379
+ Color color15 = new Color(imagehairetu[1].getRGB(x[5], y[5]));
380
+
381
+ Color color16 = new Color(imagehairetu[1].getRGB(x[6], y[6]));
382
+
383
+ Color color17 = new Color(imagehairetu[1].getRGB(x[7], y[7]));
384
+
385
+ Color color18 = new Color(imagehairetu[1].getRGB(x[8], y[8]));
386
+
387
+ /* 三面 */
388
+
389
+ Color color19 = new Color(imagehairetu[2].getRGB(x[0], y[0]));
390
+
391
+ Color color20 = new Color(imagehairetu[2].getRGB(x[1], y[1]));
392
+
393
+ Color color21 = new Color(imagehairetu[2].getRGB(x[2], y[2]));
394
+
395
+ Color color22 = new Color(imagehairetu[2].getRGB(x[3], y[3]));
396
+
397
+ Color color23 = new Color(imagehairetu[2].getRGB(x[4], y[4]));
398
+
399
+ Color color24 = new Color(imagehairetu[2].getRGB(x[5], y[5]));
400
+
401
+ Color color25 = new Color(imagehairetu[2].getRGB(x[6], y[6]));
402
+
403
+ Color color26 = new Color(imagehairetu[2].getRGB(x[7], y[7]));
404
+
405
+ Color color27 = new Color(imagehairetu[2].getRGB(x[8], y[8]));
406
+
407
+ /* 四面 */
408
+
409
+ Color color28 = new Color(imagehairetu[3].getRGB(x[0], y[0]));
410
+
411
+ Color color29 = new Color(imagehairetu[3].getRGB(x[1], y[1]));
412
+
413
+ Color color30 = new Color(imagehairetu[3].getRGB(x[2], y[2]));
414
+
415
+ Color color31 = new Color(imagehairetu[3].getRGB(x[3], y[3]));
416
+
417
+ Color color32 = new Color(imagehairetu[3].getRGB(x[4], y[4]));
418
+
419
+ Color color33 = new Color(imagehairetu[3].getRGB(x[5], y[5]));
420
+
421
+ Color color34 = new Color(imagehairetu[3].getRGB(x[6], y[6]));
422
+
423
+ Color color35 = new Color(imagehairetu[3].getRGB(x[7], y[7]));
424
+
425
+ Color color36 = new Color(imagehairetu[3].getRGB(x[8], y[8]));
426
+
427
+
428
+
429
+ h.setColor(color9);
430
+
431
+ h.fillPolygon(x1, y1, 4);
432
+
433
+ h.setColor(color6);
434
+
435
+ h.fillPolygon(x2, y2, 4);
436
+
437
+ h.setColor(color7);
438
+
439
+ h.fillPolygon(x3, y3, 4);
440
+
441
+ h.setColor(color8);
442
+
443
+ h.fillPolygon(x4, y4, 4);
444
+
445
+ h.setColor(color1);
446
+
447
+ h.fillPolygon(x5, y5, 4);
448
+
449
+ h.setColor(color2);
450
+
451
+ h.fillPolygon(x6, y6, 4);
452
+
453
+ h.setColor(color3);
454
+
455
+ h.fillPolygon(x7, y7, 4);
456
+
457
+ h.setColor(color4);
458
+
459
+ h.fillPolygon(x8, y8, 4);
460
+
461
+ h.setColor(color5);
462
+
463
+ h.fillPolygon(x9, y9, 4);
464
+
465
+ /*2面*/
466
+
467
+ h.setColor(color14);
468
+
469
+ h.fillPolygon(x10, y10, 4);
470
+
471
+ h.setColor(color13);
472
+
473
+ h.fillPolygon(x11, y11, 4);
474
+
475
+ h.setColor(color12);
476
+
477
+ h.fillPolygon(x12, y12, 4);
478
+
479
+ h.setColor(color11);
480
+
481
+ h.fillPolygon(x13, y13, 4);
482
+
483
+ h.setColor(color10);
484
+
485
+ h.fillPolygon(x14, y14, 4);
486
+
487
+ h.setColor(color17);
488
+
489
+ h.fillPolygon(x15, y15, 4);
490
+
491
+ h.setColor(color16);
492
+
493
+ h.fillPolygon(x16, y16, 4);
494
+
495
+ h.setColor(color15);
496
+
497
+ h.fillPolygon(x17, y17, 4);
498
+
499
+ h.setColor(color18);
500
+
501
+ h.fillPolygon(x18, y18, 4);
502
+
503
+ /*3面*/
504
+
505
+ h.setColor(color23);
506
+
507
+ h.fillPolygon(x19, y19, 4);
508
+
509
+ h.setColor(color22);
510
+
511
+ h.fillPolygon(x20, y20, 4);
512
+
513
+ h.setColor(color21);
514
+
515
+ h.fillPolygon(x21, y21, 4);
516
+
517
+ h.setColor(color20);
518
+
519
+ h.fillPolygon(x22, y22, 4);
520
+
521
+ h.setColor(color19);
522
+
523
+ h.fillPolygon(x23, y23, 4);
524
+
525
+ h.setColor(color26);
526
+
527
+ h.fillPolygon(x24, y24, 4);
528
+
529
+ h.setColor(color25);
530
+
531
+ h.fillPolygon(x25, y25, 4);
532
+
533
+ h.setColor(color24);
534
+
535
+ h.fillPolygon(x26, y26, 4);
536
+
537
+ h.setColor(color27);
538
+
539
+ h.fillPolygon(x27, y27, 4);
540
+
541
+ /*4面*/
542
+
543
+ h.setColor(color32);
544
+
545
+ h.fillPolygon(x28, y28, 4);
546
+
547
+ h.setColor(color31);
548
+
549
+ h.fillPolygon(x29, y29, 4);
550
+
551
+ h.setColor(color30);
552
+
553
+ h.fillPolygon(x30, y30, 4);
554
+
555
+ h.setColor(color29);
556
+
557
+ h.fillPolygon(x31, y31, 4);
558
+
559
+ h.setColor(color28);
560
+
561
+ h.fillPolygon(x32, y32, 4);
562
+
563
+ h.setColor(color33);
564
+
565
+ h.fillPolygon(x33, y33, 4);
566
+
567
+ h.setColor(color34);
568
+
569
+ h.fillPolygon(x34, y34, 4);
570
+
571
+ h.setColor(color35);
572
+
573
+ h.fillPolygon(x35, y35, 4);
574
+
575
+ h.setColor(color36);
576
+
577
+ h.fillPolygon(x36, y36, 4);
578
+
579
+ }
580
+
581
+ };
582
+
583
+ }
584
+
585
+ ```

1

説明文の追記

2018/11/08 02:10

投稿

yasu_abu7
yasu_abu7

スコア12

test CHANGED
File without changes
test CHANGED
@@ -3,3 +3,9 @@
3
3
  ネットを見ていると、update()というのをいっしょに使用してつかっているのが見られます。
4
4
 
5
5
  自分は、パネルにpaintComponentで三角錐の展開図を定義して、撮られた写真を読み取り、写真から色を抽出して展開図のほうに読み取った色を反映させたいのですが、どうもrepaint()というものの使い方がいまいちわからないので知っている方から詳しくお聞きしたいと思っています。
6
+
7
+
8
+
9
+ *追記*
10
+
11
+ パネルにpaintComponentで三角錐の展開図を定義しているものは、常時表示したいと思っています。