質問編集履歴

2

必要なソースコードを追加しました.Thumbnailクラスの I00 = new ImageIcon(filepath).getImage();でエラーが発生しています。

2015/07/17 00:26

投稿

junn
junn

スコア65

test CHANGED
File without changes
test CHANGED
@@ -492,107 +492,71 @@
492
492
 
493
493
 
494
494
 
495
- ItemBox
495
+ Thumbnail
496
496
 
497
497
  ```lang-java
498
498
 
499
- public class ItemBox extends JPanel implements ActionListener{ //ItemBoxはJPanelというオブジェクトクラスを継承
499
+ public class Thumbnail extends JFrame { //JFrameを継承
500
-
501
- //ActionListnerというインタフェースを実装
500
+
502
-
503
- private JButton upButton;
501
+
504
-
505
- private JButton downButton;
502
+
506
-
503
+
504
+
505
+
506
+
507
+
508
+
507
- private JTextField itemField;
509
+ public Thumbnail(File filepath) {
508
-
509
- private Container contentPane;
510
+
510
-
511
- private JPanel itemPanel;
511
+ add(new DrawPanel(filepath));
512
-
513
-
514
-
512
+
515
- GridBagLayout gbl = new GridBagLayout();
513
+ setTitle("イメージを拡大コピー");
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
- void addButton(Object itemobject, int x, int y, int w, int h) {
514
+
524
-
525
- GridBagConstraints gbc = new GridBagConstraints();
515
+ //setDefaultCloseOperation(EXIT_ON_CLOSE);
526
-
527
- gbc.fill = GridBagConstraints.BOTH;
516
+
528
-
529
- gbc.gridx = x;
530
-
531
- gbc.gridy = y;
532
-
533
- gbc.gridwidth = w;
517
+ setSize(350, 350);
534
-
518
+
535
- gbc.gridheight = h;
519
+ setVisible(true);
536
-
537
- gbl.setConstraints((Component) itemobject, gbc);
538
-
539
- add((Component) itemobject);
540
520
 
541
521
  }
542
522
 
523
+
524
+
525
+
526
+
543
-
527
+ }
528
+
529
+
530
+
544
-
531
+ class DrawPanel extends JPanel {
532
+
533
+
534
+
535
+ static final int C01 = 30; // 画像のサイズ変更
536
+
537
+ Image I00;
538
+
539
+ int I01;
540
+
541
+ int I02;
542
+
543
+
544
+
545
+
546
+
547
+
548
+
545
- public ItemBox(){
549
+ public DrawPanel(File filepath) {
546
-
547
-
548
-
549
- //setLayout(null);
550
+
550
-
551
- //setBackground(Color.blue);
551
+ setBackground(Color.white);
552
-
553
-
554
-
552
+
553
+
554
+
555
- itemField = new JTextField("0");
555
+ I00 = new ImageIcon(filepath).getImage();
556
-
557
- itemField.setPreferredSize(new Dimension(20, 10)); //アイテムフィールドの大きさ指定
556
+
558
-
559
- itemField.setFocusable(false); //入力制限
560
-
561
-
562
-
563
- upButton = new JButton("↑"); //upButtonオブジェクトを作成し、↑を代入する。
564
-
565
- downButton = new JButton("↓"); //downButtonオブジェクトを作成し、↓を代入する。
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
- upButton.setActionCommand("upButton");
574
-
575
- upButton.addActionListener(this); //アクションイベントを受け取れるようになる。
576
-
577
- downButton.setActionCommand("downButton");
578
-
579
- downButton.addActionListener(this);
557
+ I01 = I00.getWidth(this);
580
-
581
-
582
-
558
+
583
- setLayout(gbl);
559
+ I02 = I00.getHeight(this);
584
-
585
- addButton(itemField, 0, 0, 1, 2); // (0, 0) 幅=1, 高さ=3
586
-
587
- addButton(upButton, 1, 0, 1, 1); // (1, 0) 幅=1, 高さ=1
588
-
589
- addButton(downButton, 1, 1, 1, 1); // (1, 1) 幅=1, 高さ=1
590
-
591
-
592
-
593
-
594
-
595
-
596
560
 
597
561
  }
598
562
 
@@ -602,13 +566,21 @@
602
566
 
603
567
 
604
568
 
569
+
570
+
571
+ public void paintComponent(Graphics A00) {
572
+
605
- public String getText(){
573
+ super.paintComponent(A00);
606
-
607
-
608
-
609
-
610
-
574
+
575
+
576
+
577
+
578
+
579
+
580
+
611
- return itemField.getText();
581
+ A00.drawString("縮小(" + C01 + "%)", 0, 10);
582
+
583
+ A00.drawImage(I00, 0, 20, I01 * C01 / 100, I02 * C01 / 100, this); //縮小した画像の配置座標
612
584
 
613
585
 
614
586
 
@@ -618,92 +590,34 @@
618
590
 
619
591
 
620
592
 
621
-
593
+ ```
594
+
595
+
596
+
622
-
597
+ ScreenShot
598
+
599
+ ```lang-java
600
+
623
- //操作に応じた処理の実装
601
+ public class ScreenShot {
624
-
602
+
603
+
604
+
625
- public void actionPerformed(ActionEvent event){ //イベントが発生するとここが処理される
605
+ public void screenCapture(File filepath) throws AWTException, IOException {
606
+
626
-
607
+ Robot robot = new Robot();
608
+
627
- //ユーザーの操作e対象の判断 //このクラスでのアクションリスナー
609
+ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
628
-
610
+
629
- String cmd = event.getActionCommand();
611
+ BufferedImage image = robot.createScreenCapture(
630
-
631
-
632
-
633
- if(cmd.equals("upButton")) {
612
+
634
-
635
- int i = Integer.parseInt(itemField.getText());
613
+ new Rectangle(0, 0, screenSize.width, screenSize.height));
636
-
637
- i++;
614
+
638
-
639
- if(i>99)i=0;
640
-
641
- //itemField.setText(String.format("%1$02d",i)); //0埋め
615
+ ImageIO.write(image, "PNG", filepath); //PNGファイルの保存
642
-
643
- itemField.setText(String.valueOf(i));
616
+
644
-
645
-
646
-
647
- }
617
+ }
648
-
649
- else if(cmd.equals("downButton")){
650
-
651
- int i = Integer.parseInt(itemField.getText());
652
-
653
- i--;
654
-
655
- if(i<0)i=99;
656
-
657
- //itemField.setText(String.format("%1$02d",i)); //0埋め
658
-
659
- itemField.setText(String.valueOf(i));
660
-
661
-
662
-
663
- }
664
-
665
- }
666
-
667
-
668
-
669
-
670
-
671
-
672
618
 
673
619
  }
674
620
 
675
621
 
676
622
 
677
-
678
-
679
623
  ```
680
-
681
-
682
-
683
- ScreenShot
684
-
685
- ```lang-java
686
-
687
- public class ScreenShot {
688
-
689
-
690
-
691
- public void screenCapture(File filepath) throws AWTException, IOException {
692
-
693
- Robot robot = new Robot();
694
-
695
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
696
-
697
- BufferedImage image = robot.createScreenCapture(
698
-
699
- new Rectangle(0, 0, screenSize.width, screenSize.height));
700
-
701
- ImageIO.write(image, "PNG", filepath); //PNGファイルの保存
702
-
703
- }
704
-
705
- }
706
-
707
-
708
-
709
- ```

1

ソースコードの修正

2015/07/17 00:26

投稿

junn
junn

スコア65

test CHANGED
@@ -1 +1 @@
1
- [Java Swing]キャプチャ時サムネイルの表示
1
+ [Java Swing]キャプチャツーキャプチャ画像表示
test CHANGED
File without changes