Results 1 to 2 of 2

Thread: A little graphics help[resolved]

  1. #1

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Resolved A little graphics help[resolved]

    I am interested in learning how to do things like piecharts for a program i am writing.

    Would anyone have any simple examples or know what i could look under on the internet to help me find a simple example.

    I tried typing Java pie chart code in google and it returned all of these retail sites wanting to sell me their code or products. I dont want to buy the code just to put in a (hopefully simple) piechart. Any help would be appreciated.

    I was told that fillArc() would be useful, but am not sure how to use it.
    Last edited by ddmeightball; Mar 18th, 2005 at 11:14 AM.
    Otaku no Kamisama – God of the Geeks

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: A little graphics help

    Here's an example I wrote A LONG TIME AGO:

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PieApplet extends JApplet {
    	Color uneasyBeingGreen = new Color(0xCC, 0xCC, 0x99);
    	Color zuzusPetals = new Color(0xCC, 0x66, 0xFF);
    	Color zootSuit = new Color(0x66, 0x66, 0x99);
    	Color sweetHomeAvocado = new Color(0x66, 0x99, 0x66);
    	Color shrinkingViolet = new Color(0x66, 0x66, 0x99);
    	Color miamiNice = new Color(0x33, 0xFF, 0xFF);
    	Color inBetweenGreen = new Color(0x00, 0x99, 0x66);
    	Color norwegianBlue = new Color(0x33, 0xCC, 0xCC);
    	Color purpleRain = new Color(0x66, 0x33, 0x99);
    	Color freckle = new Color(0x99, 0x66, 0x33);
    	
        public void init() {
    	Container pane = getContentPane();
    	PiePanel pie = new PiePanel(10);
    	pie.addSlice(uneasyBeingGreen, 1284);
    	pie.addSlice(zuzusPetals, 1046);
    	pie.addSlice(zootSuit, 281);
    	pie.addSlice(sweetHomeAvocado, 232);
    	pie.addSlice(shrinkingViolet, 176);
    	pie.addSlice(miamiNice, 148);
    	pie.addSlice(inBetweenGreen, 143);
    	pie.addSlice(norwegianBlue, 133);
    	pie.addSlice(purpleRain, 130);
    	pie.addSlice(freckle, 127);
    	pane.add(pie);
    	setContentPane(pane);
        }
    }

    Next class

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class PiePanel extends JPanel {
      private PieSlice[] slice;
      private int current = 0;
      private float totalSize = 0;
      private Color background;
    
      public PiePanel(int sliceCount) {
    	slice = new PieSlice[sliceCount];
    	background = getBackground();
      }
    
      public void addSlice(Color sColor, float sSize) {
    	if (current <= slice.length) {
    		slice[current] = new PieSlice(sColor, sSize);
    		totalSize += sSize;
    		current++;
            }
      }
    
      public void paintComponent(Graphics comp) {
    	super.paintComponent(comp);
    	Graphics2D comp2D = (Graphics2D) comp;
    	int width = getSize().width - 10;
    	int height = getSize().height - 15;
    	int xInset = 5;
    	int yInset = 5;
    	if (width < 5)
    		xInset = width;
    	if (height < 5)
    		yInset = height;
    	comp2D.setColor(background);
    	comp2D.fillRect(0,0,getSize().width, getSize().height);
    	comp2D.setColor(Color.lightGray);
    	Ellipse2D.Float pie = new Ellipse2D.Float(xInset, yInset, width, height);
    	comp2D.fill(pie);
    	float start = 0;
    	for (int i =0; i < slice.length; i++) {
    		float extent = slice[i].size * 360F / totalSize;
    	comp2D.setColor(slice[i].color);	
    	Arc2D.Float drawSlice = new Arc2D.Float(xInset, yInset, width, height, start, extent, Arc2D.Float.PIE);
    	start += extent;
    	comp2D.fill(drawSlice);
          }
      }
    }
    
    class PieSlice {
      Color color = Color.lightGray;
      float size = 0;
      
    
      PieSlice(Color pColor, float pSize) {
    	color = pColor;
    	size = pSize;
      }
    }

    Sorry, I can't find the other class, I think I deleted it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width