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

回答編集履歴

8

継承忘れも修正

2017/12/17 14:28

投稿

tignear
tignear

スコア260

answer CHANGED
@@ -7,7 +7,7 @@
7
7
  pxはx一当たりの横幅
8
8
  pyはy一当たりの縦幅
9
9
  ```java
10
- public class GraphPanel{
10
+ public class GraphPanel extends JPanel{
11
11
  final Graph gr;
12
12
  GraphPanel(Graph gr){
13
13
  this.gr=gr;

7

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

2017/12/17 14:28

投稿

tignear
tignear

スコア260

answer CHANGED
@@ -16,7 +16,7 @@
16
16
  @Override
17
17
  public void paintComponent(Graphics g) {
18
18
  Graphics2D g2D = (Graphics2D) g;
19
- BufferedImage im=gr.drow();
19
+ BufferedImage im=gr.draw();
20
20
  g2D.drawImage(im, 0,0, this);
21
21
  }
22
22
  }
@@ -79,7 +79,7 @@
79
79
  originLineColor=c;
80
80
  change=true;
81
81
  }
82
- public BufferedImage drow(){
82
+ public BufferedImage draw(){
83
83
 
84
84
  if(change){
85
85
  Graphics2D g=(Graphics2D) image.getGraphics();

6

抜けに関する修正

2017/12/17 14:24

投稿

tignear
tignear

スコア260

answer CHANGED
@@ -7,6 +7,7 @@
7
7
  pxはx一当たりの横幅
8
8
  pyはy一当たりの縦幅
9
9
  ```java
10
+ public class GraphPanel{
10
11
  final Graph gr;
11
12
  GraphPanel(Graph gr){
12
13
  this.gr=gr;

5

大規模に変更

2016/07/21 11:40

投稿

tignear
tignear

スコア260

answer CHANGED
@@ -7,66 +7,102 @@
7
7
  pxはx一当たりの横幅
8
8
  pyはy一当たりの縦幅
9
9
  ```java
10
- public class GraphPanel extends JPanel{
11
- private Graph gr;
10
+ final Graph gr;
12
- public GraphPanel(int width,int height,int px,int py){
11
+ GraphPanel(Graph gr){
13
- gr=new Graph(new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR),px,py);
12
+ this.gr=gr;
13
+
14
- }
14
+ }
15
+ @Override
15
- public setpoint(int x,int y){
16
+ public void paintComponent(Graphics g) {
17
+ Graphics2D g2D = (Graphics2D) g;
18
+ BufferedImage im=gr.drow();
16
- gr.setpoint(x,y);
19
+ g2D.drawImage(im, 0,0, this);
17
- }
20
+ }
18
- public removepoint(int x){
19
- gr.removepoint(x);
20
21
  }
21
- @Override
22
- public void paintComponent(Graphics g) {
23
- Graphics2D g2D = (Graphics2D) g;
24
- BufferedImage im=gr.drow(Color.RED);
25
- g2D.drawImage(im, 0,0, this);
26
- }
27
22
  }
28
23
  ```
29
24
  せっかく作っても使わなきゃ意味がないので変更
30
25
  ```java
26
+ public class Main {
27
+
28
+ public static void main(String[] args) throws IOException {
29
+ final int SIZE=500;
31
- panelll = new GraphPanel(1000,1000,10,10);
30
+ Graph g=new Graph(SIZE,SIZE,5,5,0,0);
32
- panelll.setpoint(0,0);
31
+ g.setpoint(0,0);
32
+ g.setpoint(52,60);
33
+ g.setpoint(32,95);
33
- panelll.setpoint(15,40);
34
+ g.setpoint(25, 40);
35
+ g.setLineColor(Color.RED);
36
+ g.setOriginLineColor(Color.GREEN);
37
+ JFrame f=new JFrame();
38
+ f.setSize(1200, 700);
39
+ f.setVisible(true);
40
+ GraphPanel p=new GraphPanel(g);//panelllにあたるPanel
41
+ f.add(p);
42
+ g.removepoint(32);
43
+ }
44
+
45
+ }
34
46
  ```
35
47
 
36
48
  こっちはとりあえずのグラフ生成クラス(いろいろひどいのでライブラリ使ったほうが良いかと)
37
49
  ```java
38
- public class Graph{
50
+ public class Graph {
51
+ private boolean change;
39
52
  private BufferedImage image;
40
- private int w,h;
41
- private Graphics2D g;
53
+ private Color lineColor,originLineColor;
42
- private Integer px,py;
54
+ private Integer h,w,onex,oney,originpx,originpy;
43
55
  Map<Integer,Integer> p=new TreeMap<>();
44
- public Graph(BufferedImage image,int px,int py){
56
+ public Graph(int width,int height,int onex,int oney,int originpx,int originpy){
45
- this.image=image;
57
+ this.image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
46
- w=image.getWidth();
47
- h=image.getHeight();
58
+ this.h=image.getHeight();
59
+ this.w=image.getWidth();
60
+ this.originpx=originpx;
61
+ this.originpy=h-originpy;
48
- this.px=px;
62
+ this.onex=onex;
49
- this.py=py;
63
+ this.oney=oney;
50
- g=(Graphics2D) image.getGraphics();
51
64
  }
52
65
  public void setpoint(int x, int y){
53
66
  p.put(x,y);
67
+ change=true;
54
68
  }
55
69
  public void removepoint(int x){
56
70
  p.remove(x);
71
+ change=true;
57
72
  }
73
+ public void setLineColor(Color c){
74
+ lineColor=c;
75
+ change=true;
76
+ }
77
+ public void setOriginLineColor(Color c){
78
+ originLineColor=c;
79
+ change=true;
80
+ }
58
- public BufferedImage drow(Color linecolor){
81
+ public BufferedImage drow(){
82
+
83
+ if(change){
84
+ Graphics2D g=(Graphics2D) image.getGraphics();
85
+ g.clearRect(0, 0, w, h);
59
- int x1=0,y1=0;
86
+ int x1=0,y1=0;
60
- boolean f=false;
87
+ boolean f=false;
88
+ if(originLineColor!=null){
61
- g.setColor(linecolor);
89
+ g.setColor(originLineColor);
62
- for(Map.Entry<Integer,Integer> e : p.entrySet()) {
63
- if(f){
64
- g.drawLine(x1*px, h-y1*py, e.getKey()*px,h-e.getValue()*py);
90
+ g.drawLine(0,originpy, w, originpy);
65
- }else{
66
- f=true;
91
+ g.drawLine(originpx, 0, originpx,h);
67
92
  }
93
+ if(lineColor!=null){
94
+ g.setColor(lineColor);
95
+ for(Map.Entry<Integer,Integer> e : p.entrySet()) {
96
+ if(f){
97
+ g.drawLine(originpx+x1*onex, originpy-y1*oney, originpx+e.getKey()*onex,originpy-e.getValue()*oney);
98
+ }else{
99
+ f=true;
100
+ }
68
- x1=e.getKey();
101
+ x1=e.getKey();
69
- y1=e.getValue();
102
+ y1=e.getValue();
103
+ }
104
+ }
105
+ change=false;
70
106
  }
71
107
  return image;
72
108
  }

4

詳細にした

2016/07/21 09:51

投稿

tignear
tignear

スコア260

answer CHANGED
@@ -28,7 +28,9 @@
28
28
  ```
29
29
  せっかく作っても使わなきゃ意味がないので変更
30
30
  ```java
31
- panelll = new GraphPanel(1000,1000,10,10);
31
+ panelll = new GraphPanel(1000,1000,10,10);
32
+ panelll.setpoint(0,0);
33
+ panelll.setpoint(15,40);
32
34
  ```
33
35
 
34
36
  こっちはとりあえずのグラフ生成クラス(いろいろひどいのでライブラリ使ったほうが良いかと)

3

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

2016/07/21 09:38

投稿

tignear
tignear

スコア260

answer CHANGED
@@ -22,7 +22,7 @@
22
22
  public void paintComponent(Graphics g) {
23
23
  Graphics2D g2D = (Graphics2D) g;
24
24
  BufferedImage im=gr.drow(Color.RED);
25
- g2D.drawImage(im, im.getWidth(),im.GetHeight(), this);
25
+ g2D.drawImage(im, 0,0, this);
26
26
  }
27
27
  }
28
28
  ```

2

使い方を追記

2016/07/21 09:31

投稿

tignear
tignear

スコア260

answer CHANGED
@@ -1,6 +1,7 @@
1
1
  回答です・・DBからの取得についてはまたべつの質問でどうぞ
2
2
  要するに画像をパネルに描画するだけ
3
3
  ちなみにxyの座標のペアを指定してグラフを生成します
4
+ 指定はpanelll.setpoint(int,int)メソッドからどうぞ
4
5
  widthは画像の横幅
5
6
  heightは画像の縦幅
6
7
  pxはx一当たりの横幅

1

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

2016/07/21 08:49

投稿

tignear
tignear

スコア260

answer CHANGED
@@ -1,5 +1,10 @@
1
1
  回答です・・DBからの取得についてはまたべつの質問でどうぞ
2
2
  要するに画像をパネルに描画するだけ
3
+ ちなみにxyの座標のペアを指定してグラフを生成します
4
+ widthは画像の横幅
5
+ heightは画像の縦幅
6
+ pxはx一当たりの横幅
7
+ pyはy一当たりの縦幅
3
8
  ```java
4
9
  public class GraphPanel extends JPanel{
5
10
  private Graph gr;
@@ -9,6 +14,9 @@
9
14
  public setpoint(int x,int y){
10
15
  gr.setpoint(x,y);
11
16
  }
17
+ public removepoint(int x){
18
+ gr.removepoint(x);
19
+ }
12
20
  @Override
13
21
  public void paintComponent(Graphics g) {
14
22
  Graphics2D g2D = (Graphics2D) g;
@@ -19,7 +27,7 @@
19
27
  ```
20
28
  せっかく作っても使わなきゃ意味がないので変更
21
29
  ```java
22
- panelll = new GraphPanel();
30
+ panelll = new GraphPanel(1000,1000,10,10);
23
31
  ```
24
32
 
25
33
  こっちはとりあえずのグラフ生成クラス(いろいろひどいのでライブラリ使ったほうが良いかと)