回答編集履歴

1

コード追加

2021/07/29 11:51

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -1 +1,125 @@
1
1
  drawImage のパラメータである各座標値をカード毎に表示してみて、カード画像内の各位置とあっているかを確認されては如何でしょうか。
2
+
3
+
4
+
5
+ ----
6
+
7
+ 106等の数値を直接指定するのではなく、画像が正しく作られている前提で、各イメージの大きさを求める風にしては如何でしょう。
8
+
9
+
10
+
11
+ ```java
12
+
13
+ import java.awt.Graphics;
14
+
15
+ import java.awt.Image;
16
+
17
+ import java.awt.image.ImageObserver;
18
+
19
+ import java.io.File;
20
+
21
+ import java.io.IOException;
22
+
23
+
24
+
25
+ import javax.imageio.ImageIO;
26
+
27
+ import javax.swing.JFrame;
28
+
29
+
30
+
31
+ public class ConcentrationFrame extends JFrame {
32
+
33
+
34
+
35
+ public static void main(String[] args) throws IOException {
36
+
37
+ new ConcentrationFrame().setVisible(true);
38
+
39
+ }
40
+
41
+
42
+
43
+ private CardsImage cardsImage;
44
+
45
+
46
+
47
+ public ConcentrationFrame() throws IOException {
48
+
49
+ super("神経衰弱");
50
+
51
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52
+
53
+ setBounds(10, 10, 1550, 950);
54
+
55
+
56
+
57
+ cardsImage = new CardsImage("card52.png");
58
+
59
+ }
60
+
61
+
62
+
63
+ public void paint(Graphics g) {
64
+
65
+ super.paint(g);
66
+
67
+ for(int i=0; i<13*4+1; i++) {
68
+
69
+ int x = (i%13) * cardsImage.getCardWidth() + 20;
70
+
71
+ int y = (i/13) * cardsImage.getCardHeight() + 46;
72
+
73
+ cardsImage.drawCard(g, i, x, y, this);
74
+
75
+ }
76
+
77
+ }
78
+
79
+
80
+
81
+ private static class CardsImage {
82
+
83
+ private Image img;
84
+
85
+ private int height, width;
86
+
87
+
88
+
89
+ public CardsImage(String filename) throws IOException {
90
+
91
+ img = ImageIO.read(new File(filename)); //画像のロード
92
+
93
+ width = img.getWidth(null) / 13;
94
+
95
+ height = img.getHeight(null) / 5;
96
+
97
+ }
98
+
99
+
100
+
101
+ public void drawCard(Graphics g, int n, int dx, int dy, ImageObserver observer) {
102
+
103
+ if(img == null) throw new IllegalStateException("image nothing.");
104
+
105
+ if(n >= 13*4+1) throw new IllegalArgumentException("Number Error");
106
+
107
+
108
+
109
+ int sx = (n % 13) * width;
110
+
111
+ int sy = (n / 13) * height;
112
+
113
+ g.drawImage(img, dx, dy, dx+width, dy+height, sx, sy, sx+width, sy+height, observer);
114
+
115
+ }
116
+
117
+ public int getCardWidth() { return width; }
118
+
119
+ public int getCardHeight() { return height; }
120
+
121
+ }
122
+
123
+ }
124
+
125
+ ```