回答です・・DBからの取得についてはまたべつの質問でどうぞ
要するに画像をパネルに描画するだけ
ちなみにxyの座標のペアを指定してグラフを生成します
指定はpanelll.setpoint(int,int)メソッドからどうぞ
widthは画像の横幅
heightは画像の縦幅
pxはx一当たりの横幅
pyはy一当たりの縦幅
java
1public class GraphPanel extends JPanel{
2 final Graph gr;
3 GraphPanel(Graph gr){
4 this.gr=gr;
5
6 }
7 @Override
8 public void paintComponent(Graphics g) {
9 Graphics2D g2D = (Graphics2D) g;
10 BufferedImage im=gr.draw();
11 g2D.drawImage(im, 0,0, this);
12 }
13}
14}
せっかく作っても使わなきゃ意味がないので変更
java
1public class Main {
2
3 public static void main(String[] args) throws IOException {
4 final int SIZE=500;
5 Graph g=new Graph(SIZE,SIZE,5,5,0,0);
6 g.setpoint(0,0);
7 g.setpoint(52,60);
8 g.setpoint(32,95);
9 g.setpoint(25, 40);
10 g.setLineColor(Color.RED);
11 g.setOriginLineColor(Color.GREEN);
12 JFrame f=new JFrame();
13 f.setSize(1200, 700);
14 f.setVisible(true);
15 GraphPanel p=new GraphPanel(g);//panelllにあたるPanel
16 f.add(p);
17 g.removepoint(32);
18 }
19
20}
こっちはとりあえずのグラフ生成クラス(いろいろひどいのでライブラリ使ったほうが良いかと)
java
1public class Graph {
2 private boolean change;
3 private BufferedImage image;
4 private Color lineColor,originLineColor;
5 private Integer h,w,onex,oney,originpx,originpy;
6 Map<Integer,Integer> p=new TreeMap<>();
7 public Graph(int width,int height,int onex,int oney,int originpx,int originpy){
8 this.image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
9 this.h=image.getHeight();
10 this.w=image.getWidth();
11 this.originpx=originpx;
12 this.originpy=h-originpy;
13 this.onex=onex;
14 this.oney=oney;
15 }
16 public void setpoint(int x, int y){
17 p.put(x,y);
18 change=true;
19 }
20 public void removepoint(int x){
21 p.remove(x);
22 change=true;
23 }
24 public void setLineColor(Color c){
25 lineColor=c;
26 change=true;
27 }
28 public void setOriginLineColor(Color c){
29 originLineColor=c;
30 change=true;
31 }
32 public BufferedImage draw(){
33
34 if(change){
35 Graphics2D g=(Graphics2D) image.getGraphics();
36 g.clearRect(0, 0, w, h);
37 int x1=0,y1=0;
38 boolean f=false;
39 if(originLineColor!=null){
40 g.setColor(originLineColor);
41 g.drawLine(0,originpy, w, originpy);
42 g.drawLine(originpx, 0, originpx,h);
43 }
44 if(lineColor!=null){
45 g.setColor(lineColor);
46 for(Map.Entry<Integer,Integer> e : p.entrySet()) {
47 if(f){
48 g.drawLine(originpx+x1*onex, originpy-y1*oney, originpx+e.getKey()*onex,originpy-e.getValue()*oney);
49 }else{
50 f=true;
51 }
52 x1=e.getKey();
53 y1=e.getValue();
54 }
55 }
56 change=false;
57 }
58 return image;
59 }
60}