回答編集履歴

8

継承忘れも修正

2017/12/17 14:28

投稿

tignear
tignear

スコア260

test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  ```java
18
18
 
19
- public class GraphPanel{
19
+ public class GraphPanel extends JPanel{
20
20
 
21
21
  final Graph gr;
22
22
 

7

いまさらdrawをdrowと書いてたのを修正

2017/12/17 14:28

投稿

tignear
tignear

スコア260

test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  Graphics2D g2D = (Graphics2D) g;
36
36
 
37
- BufferedImage im=gr.drow();
37
+ BufferedImage im=gr.draw();
38
38
 
39
39
  g2D.drawImage(im, 0,0, this);
40
40
 
@@ -160,7 +160,7 @@
160
160
 
161
161
  }
162
162
 
163
- public BufferedImage drow(){
163
+ public BufferedImage draw(){
164
164
 
165
165
 
166
166
 

6

抜けに関する修正

2017/12/17 14:24

投稿

tignear
tignear

スコア260

test CHANGED
@@ -16,6 +16,8 @@
16
16
 
17
17
  ```java
18
18
 
19
+ public class GraphPanel{
20
+
19
21
  final Graph gr;
20
22
 
21
23
  GraphPanel(Graph gr){

5

大規模に変更

2016/07/21 11:40

投稿

tignear
tignear

スコア260

test CHANGED
@@ -16,39 +16,29 @@
16
16
 
17
17
  ```java
18
18
 
19
- public class GraphPanel extends JPanel{
20
-
21
- private Graph gr;
19
+ final Graph gr;
22
-
20
+
23
- public GraphPanel(int width,int height,int px,int py){
21
+ GraphPanel(Graph gr){
24
-
22
+
25
- gr=new Graph(new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR),px,py);
23
+ this.gr=gr;
26
-
24
+
25
+
26
+
27
- }
27
+ }
28
+
28
-
29
+ @Override
30
+
29
- public setpoint(int x,int y){
31
+ public void paintComponent(Graphics g) {
32
+
30
-
33
+ Graphics2D g2D = (Graphics2D) g;
34
+
35
+ BufferedImage im=gr.drow();
36
+
31
- gr.setpoint(x,y);
37
+ g2D.drawImage(im, 0,0, this);
32
-
38
+
33
- }
39
+ }
34
-
35
- public removepoint(int x){
40
+
36
-
37
- gr.removepoint(x);
38
-
39
- }
41
+ }
40
-
41
- @Override
42
-
43
- public void paintComponent(Graphics g) {
44
-
45
- Graphics2D g2D = (Graphics2D) g;
46
-
47
- BufferedImage im=gr.drow(Color.RED);
48
-
49
- g2D.drawImage(im, 0,0, this);
50
-
51
- }
52
42
 
53
43
  }
54
44
 
@@ -58,11 +48,45 @@
58
48
 
59
49
  ```java
60
50
 
51
+ public class Main {
52
+
53
+
54
+
55
+ public static void main(String[] args) throws IOException {
56
+
57
+ final int SIZE=500;
58
+
61
- panelll = new GraphPanel(1000,1000,10,10);
59
+ Graph g=new Graph(SIZE,SIZE,5,5,0,0);
62
-
60
+
63
- panelll.setpoint(0,0);
61
+ g.setpoint(0,0);
62
+
64
-
63
+ g.setpoint(52,60);
64
+
65
+ g.setpoint(32,95);
66
+
65
- panelll.setpoint(15,40);
67
+ g.setpoint(25, 40);
68
+
69
+ g.setLineColor(Color.RED);
70
+
71
+ g.setOriginLineColor(Color.GREEN);
72
+
73
+ JFrame f=new JFrame();
74
+
75
+ f.setSize(1200, 700);
76
+
77
+ f.setVisible(true);
78
+
79
+ GraphPanel p=new GraphPanel(g);//panelllにあたるPanel
80
+
81
+ f.add(p);
82
+
83
+ g.removepoint(32);
84
+
85
+ }
86
+
87
+
88
+
89
+ }
66
90
 
67
91
  ```
68
92
 
@@ -72,31 +96,33 @@
72
96
 
73
97
  ```java
74
98
 
75
- public class Graph{
99
+ public class Graph {
100
+
101
+ private boolean change;
76
102
 
77
103
  private BufferedImage image;
78
104
 
79
- private int w,h;
80
-
81
- private Graphics2D g;
105
+ private Color lineColor,originLineColor;
82
-
106
+
83
- private Integer px,py;
107
+ private Integer h,w,onex,oney,originpx,originpy;
84
108
 
85
109
  Map<Integer,Integer> p=new TreeMap<>();
86
110
 
87
- public Graph(BufferedImage image,int px,int py){
111
+ public Graph(int width,int height,int onex,int oney,int originpx,int originpy){
88
-
112
+
89
- this.image=image;
113
+ this.image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
90
-
91
- w=image.getWidth();
114
+
92
-
93
- h=image.getHeight();
115
+ this.h=image.getHeight();
116
+
94
-
117
+ this.w=image.getWidth();
118
+
119
+ this.originpx=originpx;
120
+
121
+ this.originpy=h-originpy;
122
+
95
- this.px=px;
123
+ this.onex=onex;
96
-
124
+
97
- this.py=py;
125
+ this.oney=oney;
98
-
99
- g=(Graphics2D) image.getGraphics();
100
126
 
101
127
  }
102
128
 
@@ -104,37 +130,83 @@
104
130
 
105
131
  p.put(x,y);
106
132
 
133
+ change=true;
134
+
107
135
  }
108
136
 
109
137
  public void removepoint(int x){
110
138
 
111
139
  p.remove(x);
112
140
 
141
+ change=true;
142
+
113
- }
143
+ }
144
+
114
-
145
+ public void setLineColor(Color c){
146
+
147
+ lineColor=c;
148
+
149
+ change=true;
150
+
151
+ }
152
+
153
+ public void setOriginLineColor(Color c){
154
+
155
+ originLineColor=c;
156
+
157
+ change=true;
158
+
159
+ }
160
+
115
- public BufferedImage drow(Color linecolor){
161
+ public BufferedImage drow(){
162
+
163
+
164
+
116
-
165
+ if(change){
166
+
167
+ Graphics2D g=(Graphics2D) image.getGraphics();
168
+
169
+ g.clearRect(0, 0, w, h);
170
+
117
- int x1=0,y1=0;
171
+ int x1=0,y1=0;
118
-
172
+
119
- boolean f=false;
173
+ boolean f=false;
174
+
120
-
175
+ if(originLineColor!=null){
176
+
121
- g.setColor(linecolor);
177
+ g.setColor(originLineColor);
122
-
123
- for(Map.Entry<Integer,Integer> e : p.entrySet()) {
178
+
124
-
125
- if(f){
126
-
127
- g.drawLine(x1*px, h-y1*py, e.getKey()*px,h-e.getValue()*py);
179
+ g.drawLine(0,originpy, w, originpy);
128
-
129
- }else{
180
+
130
-
131
- f=true;
181
+ g.drawLine(originpx, 0, originpx,h);
132
182
 
133
183
  }
134
184
 
185
+ if(lineColor!=null){
186
+
187
+ g.setColor(lineColor);
188
+
189
+ for(Map.Entry<Integer,Integer> e : p.entrySet()) {
190
+
191
+ if(f){
192
+
193
+ g.drawLine(originpx+x1*onex, originpy-y1*oney, originpx+e.getKey()*onex,originpy-e.getValue()*oney);
194
+
195
+ }else{
196
+
197
+ f=true;
198
+
199
+ }
200
+
135
- x1=e.getKey();
201
+ x1=e.getKey();
136
-
202
+
137
- y1=e.getValue();
203
+ y1=e.getValue();
204
+
205
+ }
206
+
207
+ }
208
+
209
+ change=false;
138
210
 
139
211
  }
140
212
 

4

詳細にした

2016/07/21 09:51

投稿

tignear
tignear

スコア260

test CHANGED
@@ -58,7 +58,11 @@
58
58
 
59
59
  ```java
60
60
 
61
- panelll = new GraphPanel(1000,1000,10,10);
61
+ panelll = new GraphPanel(1000,1000,10,10);
62
+
63
+ panelll.setpoint(0,0);
64
+
65
+ panelll.setpoint(15,40);
62
66
 
63
67
  ```
64
68
 

3

いくつか間違えてました・・

2016/07/21 09:38

投稿

tignear
tignear

スコア260

test CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
  BufferedImage im=gr.drow(Color.RED);
48
48
 
49
- g2D.drawImage(im, im.getWidth(),im.GetHeight(), this);
49
+ g2D.drawImage(im, 0,0, this);
50
50
 
51
51
  }
52
52
 

2

使い方を追記

2016/07/21 09:31

投稿

tignear
tignear

スコア260

test CHANGED
@@ -3,6 +3,8 @@
3
3
  要するに画像をパネルに描画するだけ
4
4
 
5
5
  ちなみにxyの座標のペアを指定してグラフを生成します
6
+
7
+ 指定はpanelll.setpoint(int,int)メソッドからどうぞ
6
8
 
7
9
  widthは画像の横幅
8
10
 

1

コードが間違っているのを変更

2016/07/21 08:49

投稿

tignear
tignear

スコア260

test CHANGED
@@ -1,6 +1,16 @@
1
1
  回答です・・DBからの取得についてはまたべつの質問でどうぞ
2
2
 
3
3
  要するに画像をパネルに描画するだけ
4
+
5
+ ちなみにxyの座標のペアを指定してグラフを生成します
6
+
7
+ widthは画像の横幅
8
+
9
+ heightは画像の縦幅
10
+
11
+ pxはx一当たりの横幅
12
+
13
+ pyはy一当たりの縦幅
4
14
 
5
15
  ```java
6
16
 
@@ -19,6 +29,12 @@
19
29
  gr.setpoint(x,y);
20
30
 
21
31
  }
32
+
33
+ public removepoint(int x){
34
+
35
+ gr.removepoint(x);
36
+
37
+ }
22
38
 
23
39
  @Override
24
40
 
@@ -40,7 +56,7 @@
40
56
 
41
57
  ```java
42
58
 
43
- panelll = new GraphPanel();
59
+ panelll = new GraphPanel(1000,1000,10,10);
44
60
 
45
61
  ```
46
62