swing - rotating coordinate plane for data and text in Java -


i need to:
1.) move origin , rotate coordinate plane x-values progress rightward , y-values progress upward new origin(which needs bottom left corner of inner, blue rectangle in code below). enable me plot points @ x,y coordinate pairs in code below.
2.) plot rotated labels tic marks on y-axis of data plot.

the code below sets problem. works, except 2 problems:
1.) data points being plotted upper left hand corner origin , y-values descending downward
2.) labels tic marks on y-axis not being drawn on screen

can show me how fix code below fixes these 2 problems , first paragraph above describes?

the code in following 2 java files:

datagui.java

import java.awt.*; import java.util.arraylist; import javax.swing.*;  class datagui extends jframe{ datagui() {     super("x,y plot");     this.setdefaultcloseoperation(jframe.exit_on_close);     this.setpreferredsize(new dimension(800, 400));     this.pack();     this.setsize(new dimension(800, 600));     this.setlocationrelativeto(null);       setlayout(new gridlayout());     arraylist<double> mydiffs = new arraylist<double>();             mydiffs.add(25.0);             mydiffs.add(9.0);             mydiffs.add(7.0);             mydiffs.add(16.0);             mydiffs.add(15.0);             mydiffs.add(6.0);             mydiffs.add(2.0);             mydiffs.add(8.0);             mydiffs.add(2.0);             mydiffs.add(27.0);             mydiffs.add(14.0);             mydiffs.add(12.0);             mydiffs.add(19.0);             mydiffs.add(10.0);             mydiffs.add(11.0);             mydiffs.add(8.0);             mydiffs.add(19.0);             mydiffs.add(2.0);             mydiffs.add(16.0);             mydiffs.add(5.0);             mydiffs.add(18.0);             mydiffs.add(23.0);             mydiffs.add(9.0);             mydiffs.add(4.0);             mydiffs.add(8.0);             mydiffs.add(9.0);             mydiffs.add(3.0);             mydiffs.add(3.0);             mydiffs.add(9.0);             mydiffs.add(13.0);             mydiffs.add(17.0);             mydiffs.add(7.0);             mydiffs.add(0.0);             mydiffs.add(2.0);             mydiffs.add(3.0);             mydiffs.add(33.0);             mydiffs.add(23.0);             mydiffs.add(26.0);             mydiffs.add(12.0);             mydiffs.add(12.0);             mydiffs.add(19.0);             mydiffs.add(14.0);             mydiffs.add(9.0);             mydiffs.add(26.0);             mydiffs.add(24.0);             mydiffs.add(13.0);             mydiffs.add(19.0);             mydiffs.add(2.0);             mydiffs.add(7.0);             mydiffs.add(28.0);             mydiffs.add(15.0);             mydiffs.add(2.0);             mydiffs.add(5.0);             mydiffs.add(17.0);             mydiffs.add(2.0);             mydiffs.add(16.0);             mydiffs.add(19.0);             mydiffs.add(2.0);             mydiffs.add(31.0);     datapanel mypp = new datapanel(mydiffs,this.getheight(),this.getwidth());     this.add(mypp);     this.setvisible(true);// display panel. } public static void main(string[] args){     datagui mydatagui = new datagui();     mydatagui.setvisible(true); } } 

datapanel.java (note: edited code below include trashgod's suggestions, still not work.)

import java.awt.*; import java.awt.geom.affinetransform; import javax.swing.*; import java.text.decimalformat; import java.text.numberformat; import java.util.*;  class datapanel extends jpanel { insets ins; // holds panel's insets arraylist<double> mydiffs; double maxdiff = double.negative_infinity; double mindiff = double.positive_infinity; double maxplot;  datapanel(arraylist<double> diffs, int h, int w){     setopaque(true);// ensure panel opaque.     setpreferredsize(new dimension(w, h));     setminimumsize(new dimension(w, h));     setmaximumsize(new dimension(w, h));     mydiffs = diffs;     repaint();     this.setvisible(true); }  protected void paintcomponent(graphics g){// override paintcomponent() method.     super.paintcomponent(g);     //get data plotting environment , text     int height = getheight();     int width = getwidth();     ins = getinsets();     graphics2d g2d = (graphics2d)g;     fontmetrics fontmetrics = g2d.getfontmetrics();     string xstring = ("x-axis label");     int xstrwidth = fontmetrics.stringwidth(xstring);     int xstrheight = fontmetrics.getheight();     string ystring = "y-axis label";     int ystrwidth = fontmetrics.stringwidth(ystring);     int ystrheight = fontmetrics.getheight();     string titlestring ="title of graphic";     int titlestrwidth = fontmetrics.stringwidth(titlestring);     int titlestrheight = fontmetrics.getheight();     int leftmargin = ins.left;     //set parameters inner rectangle     int hpad=10;     int vpad = 6;     int testleftstartplotwindow = ins.left+5+(3*ystrheight);     int testinnerwidth = width-testleftstartplotwindow-ins.right-hpad;     getmaxmindiffs();     getmaxplotval();     double increment = 5.0;     int numticks = (int)(maxplot/increment);//will use numticks for: remainder, leftstartplotwindow, innerrectangle+labels+tickmarks     int remainder = testinnerwidth%numticks;     int leftstartplotwindow = testleftstartplotwindow-remainder;     system.out.println("remainder is: "+remainder);     int bottompad = (3*xstrheight)-vpad;     int bluetop = ins.bottom+(vpad/2)+titlestrheight;     int blueheight = height-bottompad-bluetop;     int bluewidth = blueheight;     int bluebottom = blueheight+bluetop;      //plot outer rectangle     g.setcolor(color.red);     int redwidth = width-leftmargin-1;     g.drawrect(leftmargin, ins.bottom, redwidth, height-ins.bottom-1);     //write top label     g.setcolor(color.black);     g.drawstring(titlestring, leftstartplotwindow+((bluewidth/2)-(titlestrwidth/2)), titlestrheight);     // fill, plot, inner rectangle     g.setcolor(color.white);     g.fillrect(leftstartplotwindow, bluetop, bluewidth, blueheight);     g.setcolor(color.blue);     g.drawrect(leftstartplotwindow, bluetop, bluewidth, blueheight);     //scale diffs fit window     double scalar = bluewidth/maxplot;     arraylist<double> scaleddiffs = new arraylist<double>();     for(int e = 0;e<mydiffs.size();e++){scaleddiffs.add(mydiffs.get(e)*scalar);}     //plot scaled diffs     affinetransform @ = g2d.gettransform();//save graphics context's transform     g2d.translate(leftstartplotwindow, bluetop);//translate origin bottom-left corner of blue rectangle     g2d.scale(1, -1);//invert y-axis     for(int w = 0;w<scaleddiffs.size();w++){         if(w>0){             double prior = scaleddiffs.get(w-1);             int priorint = (int)prior;             double current = scaleddiffs.get(w);             int currentint = (int)current;             g2d.drawoval(priorint, currentint, 4, 4);         }     }     g2d.settransform(at);//restore transform conventional rendering     //write x-axis label     g.setcolor(color.red);     g.drawstring(xstring, leftstartplotwindow+((bluewidth/2)-(xstrwidth/2)), height-ins.bottom-vpad);     //write y-axis label     g2d.rotate(math.toradians(-90), 0, 0);//rotate text 90 degrees counter-clockwise     g.drawstring(ystring, -(height/2)-(ystrwidth/2), ystrheight);     g2d.rotate(math.toradians(+90), 0, 0);//rotate text 90 degrees clockwise     // draw tick marks on x-axis     numberformat formatter = new decimalformat("#0.0");     double k = (double)bluewidth/(double)numticks;     double iteration = 0;     for(int h=0;h<=numticks;h++){         int xval = (int)(h*k);         g.setcolor(color.red);         g.drawline(leftstartplotwindow+xval, bluebottom+2, leftstartplotwindow+xval, bluebottom+(xstrheight/2));//draw tick marks         g.drawstring(formatter.format(iteration),leftstartplotwindow+xval-(fontmetrics.stringwidth(double.tostring(iteration))/2),bluebottom+(xstrheight/2)+13);         iteration+=increment;     }     // draw tick marks on y-axis     iteration = 0;     for(int h=0;h<=numticks;h++){         int yval = (int)(h*k);         g.setcolor(color.red);         g.drawline(leftstartplotwindow-2, bluebottom-yval, leftstartplotwindow-(ystrheight/2), bluebottom-yval);//draw tick marks         g2d.rotate(math.toradians(-90), 0, 0);//rotate text 90 degrees counter-clockwise         g.drawstring(formatter.format(iteration),leftstartplotwindow-2,bluebottom-(fontmetrics.stringwidth(double.tostring(iteration))/2));         g2d.rotate(math.toradians(+90), 0, 0);//rotate text 90 degrees clockwise         iteration+=increment;     } } void getmaxmindiffs(){// max , min of diffs     for(int u = 0;u<mydiffs.size();u++){         if(mydiffs.get(u)>maxdiff){maxdiff = mydiffs.get(u);}         if(mydiffs.get(u)<mindiff){mindiff = mydiffs.get(u);}     } } void getmaxplotval(){     maxplot = maxdiff;     maxplot += 1;//make sure maxplot bigger max data value     while(maxplot%5!=0){maxplot+=1;}//make sure maxplot multiple of 5 } } 

also, always, links articles or tutorials on topic appreciated.

one approach shown in sinetest. in outline,

  1. save graphics context's transform.

    graphics2d g2d = (graphics2d) g; affinetransform @ = g2d.gettransform(); 
  2. translate origin center.

    g2d.translate(w / 2, h / 2); 
  3. invert y-axis.

    g2d.scale(1, -1); 
  4. render using cartesian coordinates.

  5. restore transform conventional rendering.

    g2d.settransform(at); 

enter image description here


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -