swing - Line Graph in Java(not a jfreechart) -
i have 2 sets of array x , y read .txt file , need put in line graph. far code:
import java.awt.basicstroke; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.fontmetrics; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.renderinghints; import java.awt.stroke; import java.util.arraylist; import java.util.list; import java.util.random; import java.awt.font; import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.logging.level; import java.util.logging.logger; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.swingutilities; public class drawgraph extends jpanel { private int width = 800; private int heigth = 400; private int padding = 25; private int labelpadding = 25; private color linecolor = new color(44, 102, 230, 180); private color pointcolor = new color(100, 100, 100, 180); private color gridcolor = new color(200, 200, 200, 200); private static final stroke graph_stroke = new basicstroke(2f); private int pointwidth = 4; private int numberydivisions = 10; private list<double> scores; private int xp; private int yp; public drawgraph(list<double> scores){ this.scores=scores; } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); double xscale = ((double) getwidth() - (2 * padding) - labelpadding) / (scores.size() - 1); double yscale = ((double) getheight() - 2 * padding - labelpadding) / (getmaxscore() - getminscore()); list<point> graphpoints = new arraylist<>(); (int = 0; < scores.size(); i++) { int x1 = (int) (i * xscale + padding + labelpadding); int y1 = (int) ((getmaxscore() - scores.get(i)) * yscale + padding); graphpoints.add(new point(x1, y1)); } // draw white background g2.setcolor(color.white); g2.fillrect(padding + labelpadding, padding, getwidth() - (2 * padding) - labelpadding, getheight() - 2 * padding - labelpadding); g2.setcolor(color.black); // create hatch marks , grid lines y axis. (int = 0; < numberydivisions + 1; i++) { int x0 = padding + labelpadding; int x1 = pointwidth + padding + labelpadding; int y0 = getheight() - ((i * (getheight() - padding * 2 - labelpadding)) / numberydivisions + padding + labelpadding); int y1 = y0; if (scores.size() > 0) { g2.setcolor(gridcolor); g2.drawline(padding + labelpadding + 1 + pointwidth, y0, getwidth() - padding, y1); g2.setcolor(color.black); string ylabel = ((int) ((getminscore() + (getmaxscore() - getminscore()) * ((i * 1.0) / numberydivisions)) * 100)) / 100.0 + ""; fontmetrics metrics = g2.getfontmetrics(); int labelwidth = metrics.stringwidth(ylabel); g2.drawstring(ylabel, x0 - labelwidth - 5, y0 + (metrics.getheight() / 2) - 3); } g2.drawline(x0, y0, x1, y1); } // , x axis (int = 0; < scores.size(); i++) { if (scores.size() > 1) { int x0 = * (getwidth() - padding * 2 - labelpadding) / (scores.size() - 1) + padding + labelpadding; int x1 = x0; int y0 = getheight() - padding - labelpadding; int y1 = y0 - pointwidth; if ((i % ((int) ((scores.size() / 20.0)) + 1)) == 0) { g2.setcolor(gridcolor); g2.drawline(x0, getheight() - padding - labelpadding - 1 - pointwidth, x1, padding); g2.setcolor(color.black); string xlabel = + ""; fontmetrics metrics = g2.getfontmetrics(); int labelwidth = metrics.stringwidth(xlabel); g2.drawstring(xlabel, x0 - labelwidth / 2, y0 + metrics.getheight() + 3); } g2.drawline(x0, y0, x1, y1); } } // create x , y axes g2.drawline(padding + labelpadding, getheight() - padding - labelpadding, padding + labelpadding, padding); g2.drawline(padding + labelpadding, getheight() - padding - labelpadding, getwidth() - padding, getheight() - padding - labelpadding); stroke oldstroke = g2.getstroke(); g2.setcolor(linecolor); g2.setstroke(graph_stroke); (int = 0; < graphpoints.size() - 1; i++) { int x1 = graphpoints.get(i).x; int y1 = graphpoints.get(i).y; int x2 = graphpoints.get(i + 1).x; int y2 = graphpoints.get(i + 1).y; g2.drawline(x1, y1, x2, y2); } g2.setstroke(oldstroke); g2.setcolor(pointcolor); (int = 0; < graphpoints.size(); i++) { int x = graphpoints.get(i).x - pointwidth / 2; int y = graphpoints.get(i).y - pointwidth / 2; int ovalw = pointwidth; int ovalh = pointwidth; g2.filloval(x, y, ovalw, ovalh); } } // @override // public dimension getpreferredsize() { // return new dimension(width, heigth); // } private double getminscore() { double minscore = double.max_value; (double score : scores) { minscore = math.min(minscore, score); } return minscore; } private double getmaxscore() { double maxscore = double.min_value; (double score : scores) { maxscore = math.max(maxscore, xp); } return maxscore; } public void setscores(list<double> scores) { this.scores = scores; invalidate(); this.repaint(); } public list<double> getscores() { return scores; } private static void createandshowgui() throws filenotfoundexception, ioexception { list<double> scores = new arraylist<>(); bufferedreader br = new bufferedreader(new bufferedreader(new filereader("c:\\users\\johanna\\desktop\\values.txt"))); string line = ""; while((line = br.readline())!=null){ string[] t = line.split(","); double x = double.parsedouble(t[0].trim()); double y = double.parsedouble(t[1].trim()); } mainpanel mainpanel = new mainpanel(scores); mainpanel.setpreferredsize(new dimension(800, 600)); jframe frame = new jframe("drawgraph"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { try { createandshowgui(); } catch (ioexception ex) { logger.getlogger(drawgraph.class.getname()).log(level.severe, null, ex); } } }); } //main changes underneath static class mainpanel extends jpanel { public mainpanel(list<double> scores) { setlayout(new borderlayout()); jlabel title = new jlabel(""); title.setfont(new font("arial", font.bold, 25)); title.sethorizontalalignment(jlabel.center); jpanel drawgraph = new drawgraph(scores); verticalpanel vertpanel = new verticalpanel(); horizontalpanel horipanel = new horizontalpanel(); add(title, borderlayout.north); add(horipanel, borderlayout.south); add(vertpanel, borderlayout.west); add(drawgraph, borderlayout.center); } class verticalpanel extends jpanel { public verticalpanel() { setpreferredsize(new dimension(25, 0)); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d gg = (graphics2d) g; gg.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); font font = new font("arial", font.plain, 15); string string = "transmittance"; fontmetrics metrics = g.getfontmetrics(font); int width = metrics.stringwidth(string); int height = metrics.getheight(); gg.setfont(font); drawrotate(gg, getwidth(), (getheight() + width) / 2, 270, string); } public void drawrotate(graphics2d gg, double x, double y, int angle, string text) { gg.translate((float) x, (float) y); gg.rotate(math.toradians(angle)); gg.drawstring(text, 0, 0); gg.rotate(-math.toradians(angle)); gg.translate(-(float) x, -(float) y); } } class horizontalpanel extends jpanel { public horizontalpanel() { setpreferredsize(new dimension(0, 25)); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d gg = (graphics2d) g; gg.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); font font = new font("arial", font.plain, 15); string string = "wavenumber (cm)"; fontmetrics metrics = g.getfontmetrics(font); int width = metrics.stringwidth(string); int height = metrics.getheight(); gg.setfont(font); gg.drawstring(string, (getwidth() - width) / 2, 11); } } } }
this tried. doesn't work :( why doesn't work? ..............................................................
Comments
Post a Comment