swing - Creating a Simple Bar Chart in Java - Reads Data and Outputs Bar Graph -


im required create simple bar chart 1 of projects. program requires input of 1 9 integers greater zero. program required display simple bar graph integers displayed above bars. professor did not explain program me , first time working graphics.

the program requires:

a class "simplebarchart" (which extend frame) private variables bar[] bars , int [] inputdata among others (like private graphics g, private int windowwid, windowht, etc) , following functions. uses auxiliary class bars shown:

class bar { public int height, width, value, nix, nwy;   public bar() {}   public bar(int height, int width, int value, int nix, int nwy)   { this.height = height; etc } } 

next constructor simplebarchart(), calls readdata(), createbars(), , drawbars().

a function private void readdata(), reads inputdata bar-chart. use code given below, uses joptionpane.

a function private void createbars() create bars array on assigning width, height, value (bars[i].value = inputdata[i]), nix, , nwy of each bar. requires 25 pixels of space on top , bottom of display-window, 10 pixels between bars, , has allow bar-heights scaled form of inputdata items.

finally function private void drawbars() draw bars, 1 @ time suitable sleep-time between bars, 2 different colors. requires use of g.drawstring("" +b.value, nix + b.width/2, nwy - 10) label each bar in black b value @ 10 pixel above top.

ive been trying figure out day , i'm lost. appreciated!

heres code have far:

package simplebarchart;  import java.awt.*; import java.awt.event.*; import java.awt.toolkit.*; import javax.swing.*; import java.io.*; import java.util.*;   public class simplebarchart extends jframe {     private final int outer_margin = 20;     private static final color background_color = color.white;     private static final color bar_color = color.red;      private int space_on_left_right;     private image fimagebuffer;     private insets finsets;     private graphics g;     private bar[] bars;     private static int sleep = 500;     private int[] inputdata;   class bar {     private int height, value;     public int width;     public int nwx;     public int nwy;     public color color;      public bar() {}     public bar(int height, int width, int value, int nwx, int nwy)     {         this.height = height;         this.width = width;          this.value = value;          this.nwx = nwx;         this.nwy = nwy;     }  }   public simplebarchart(final int[] inputdata) {     this.inputdata = inputdata;     addwindowlistener(new windowcloser());     finsets = getinsets();     setsize(width + finsets.left + finsets.right, height + finsets.top + finsets.bottom);     settitle("bar chart");     if (((fimagebuffer = createimage(width, height)) == null) ||             ((g = fimagebuffer.getgraphics()) == null))             system.exit(1);     readdata();     createbars();     getcontentpane().add(new simplebarchart(inputdata), borderlayout.center);     setvisible(true); }  /**  *  * @param g  */ protected void paintcomponent(final graphics g) {     g.drawimage(fimagebuffer, finsets.left, finsets.top, null); }  class windowcloser extends windowadapter {     @override     public void windowclosing(windowevent e)     {         system.exit(0);     } }  private void readdata() {     string[] inputitems = joptionpane.showinputdialog("enter 1 9 integers > 0").trim().split(" +");     int numdata = inputitems.length;     inputdata = new int[numdata];      (int itemindex = 0; itemindex < inputitems.length; itemindex++)         inputdata[itemindex] = numdata;   }  private void createbars() {  //im confused on how create bars program.  //this function requires 25 pixels of space on top , bottom of display-    window, 10 pixels between bars, , has allow bar-heights **scaled form of inputdata items.**       bar[] bars = new bar[];     int pixelbetweenbars = 25;     int width = 800 + 2*outer_margin;     int height = 600 + 2*outer_margin;  }  private void drawbars(final graphics g) {             int outer_margin = 20,             width = 800 + 2 * outer_margin,             height = 600 + 2 * outer_margin;       g.setcolor(background_color);     g.fillrect(0, 0, width, height);      g.setcolor(bar_color);     final int barwidth = 20;     (int itemindex = 0; itemindex < inputdata.length; itemindex++) {         final int x = outer_margin + 25 * itemindex;         final int barheight = 10 * inputdata[itemindex];         final int y = barheight;         g.fillrect(x, y, barwidth, barheight);     } }   public static void main(string[] args)  {     new simplebarchart; } 

i agree uttesh kumar jfreechart excellent chart library , pays off invest time learning it. since doing project learn more graphics, better code drawing yourself.

first few general remarks:

  • the bar class defined 2 times , both implementations not used yet;
  • the createimage([...], [...]).getgraphics() construction looks rather exotic;
  • the readdata method reads space separated list of numbers, allocates memory, not store numbers (you use bar class here);
  • the createbars method declares 3 local variables not used yet (and nothing else).

as shown in performing custom painting tutorial recommended madprogrammer, common approach custom painting subclassing jpanel class , overriding paintcomponent method. studying short tutorial speed pretty quickly, agree recommendation!

in readdata method, add few lines store numbers (and switch using bar class later):

for (int itemindex = 0; itemindex < inputitems.length; itemindex++)     inputdata[itemindex] = [...get number inputitems string here...]; 

after call readdata in constructor, add:

getcontentpane().add(new simplebarpanel(inputdata), borderlayout.center); setvisible(true); 

this uses new simplebarpanel class takes care of custom painting. (the call setvisible should go last if want simplebarpanel painting shown.)

the simplebarpanel class this:

import java.awt.*; import javax.swing.*;  public class simplebarpanel extends jpanel {     private static final color background_color = color.white;     private static final color bar_color = color.red;      private int[] inputdata;      public simplebarpanel(final int[] inputdata) {         this.inputdata = inputdata;     }      @override     protected void paintcomponent(final graphics g) {         super.paintcomponent(g);          drawbars(g);     }      private void drawbars(final graphics g) {         int /*i,*/ outer_margin = 20,                 width = 800 + 2 * outer_margin,                 height = 600 + 2 * outer_margin;                 /*space_between_bars = 10, space_on_top_bottom = 25;*/          g.setcolor(background_color);         g.fillrect(0, 0, width, height);          g.setcolor(bar_color);         final int barwidth = 20;         (int itemindex = 0; itemindex < inputdata.length; itemindex++) {             final int x = outer_margin + 25 * itemindex;             final int barheight = 10 * inputdata[itemindex];             final int y = [...y calculated using barheight; higher bar, lower y should be...];             g.fillrect(x, y, barwidth, barheight);         }     } } 

good luck project.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -