I have everything added to the JFrame using absolute positioning. It works fine but when it repaints the pie chart it also repaints the last JNumericField and button at the the very top left of the screen. At postion 0, 0 I presume

PieChartFrame.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class PieChartFrame extends JFrame{

    private JNumericField numfield[] = new JNumericField[4];
    private JButton button = new JButton("Pie");
    private double temp[] = {0, 0, 0, 0};
    private PieChartJPanel pie = new PieChartJPanel(temp);
    private double input[] = new double[4];
    private int position = 50;
    private JLabel labels[] = new JLabel[4];


    public PieChartFrame()
    {
        super("Pie Charter");

        labels[0] = new JLabel("Red");
        labels[1] = new JLabel("Green");
        labels[2] = new JLabel("Blue");
        labels[3] = new JLabel("Yellow");

        for(int counter = 0; counter <= 3; counter++)
        {
            numfield[counter] = new JNumericField(10);
            numfield[counter].setBounds(30, position, 150, 20);
            labels[counter].setBounds(30, position - 35, 100, 50);
            add(labels[counter]);
            add(numfield[counter]);
            position += 40;
        }

        button.setBounds(numfield[3].getX() + 35, numfield[3].getY() + 35, 75, 25);
        add(button);
        add(pie);

        ButtonHandler handler = new ButtonHandler();
        button.addActionListener(handler);

    }

    private class ButtonHandler implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent event) 
        {
            numfield[0].requestFocusInWindow();
            for(int counter = 0; counter <= 3; counter++)
            {
                input[counter] = numfield[counter].getNumber();
                numfield[counter].setText("");
            }

            pie.setPositions(input);

            pie.repaint();

        }

    }

}

PieChartJPanel.java

import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;

public class PieChartJPanel extends JPanel{

    private double ppiece[] = new double[4];
    private double sum = 0.0;
    private double place[] = new double[4];

    public PieChartJPanel(double arr[])
    {
        setPositions(arr);
    }

    public void paintComponent( Graphics g )
    {
        super.paintComponents(g);

        Graphics2D g2d = ( Graphics2D ) g;

        g2d.setPaint(Color.RED);
        g2d.fill(new Arc2D.Double(250, 25, 200, 200, 0, place[0], Arc2D.PIE));

        g2d.setPaint(Color.GREEN);
        g2d.fill(new Arc2D.Double(250, 25, 200, 200, place[1], ppiece[1] / sum * 360, Arc2D.PIE));

        g2d.setPaint(Color.BLUE);
        g2d.fill(new Arc2D.Double(250, 25, 200, 200, place[2], ppiece[2] / sum * 360, Arc2D.PIE));

        g2d.setPaint(Color.YELLOW);
        g2d.fill(new Arc2D.Double(250, 25, 200, 200, place[3], ppiece[3] / sum * 360, Arc2D.PIE));


    }

    public void setPositions(double parray[])
    {
        ppiece = parray;

        for(int counter  = 0; counter <= ppiece.length - 1; counter++)
        {
            sum += ppiece[counter];
        }

        place[0] = ppiece[0] / sum * 360;
        place[1] = place[0];
        place[2] = (ppiece[0] + ppiece[1]) / sum * 360; 
        place[3] = (ppiece[0] + ppiece[1] + ppiece[2]) / sum * 360;
    }

}

JNumericField.java

//Imports
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

public class JNumericField  extends JTextField{

    //Constructors 
    //sets the constructor of JTextField to nothing
    public JNumericField()
    {
        super();
    }

    //sets the constructor of JTextField to  make the length text field 
    public JNumericField(int size)
    {
        super(size);
    }

    //sets the constructor of JTextField to display text in the text field
    public JNumericField(String string)
    {
        super(string);
    }

    //sets the constructor of JTextField to display text in the text field and set the length of the text field
    public JNumericField(String string, int size)
    {
        super(string, size);
    }

    //sets the contructor of the JTextField to display text, set the size, and sends a document object
    public JNumericField(Document doc, String string, int size)
    {
        super(doc, string, size);
    }

    //This Method gets the number from the text field and converts it to a Double
    public double getNumber()
    {
            String x = getText(); //Gets the text from the text field
            double d = 0.0; //will hold the number after it has been parsed to a double
            //Catches and handles NumberFormatException
            try
            {
                d = Double.parseDouble(x); //parses the number to a double
                return d; // Returns the parsed input
            }
            catch(NumberFormatException e)
            {
               //If NumberFormatException is thrown display an error message
               JOptionPane.showMessageDialog( this, "Not Numeric Input", "", JOptionPane.ERROR_MESSAGE );
            }
            return d; //Returns d       
    }

    //This method uses DoubleDocument to handle the textfield 
    protected Document createDefaultModel() {
        return new DoubleDocument();
    }

    //This class makes sure that only number are displayed in the text field
    static class DoubleDocument extends PlainDocument 
    {
        //insertString Method uses the information from the text field to know what is in it and whethor not to except  it
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
        {
            //if str does is null do nothing
            if(str == null) 
                return;
            //The string will hold the text that is in the text field
            String string = getText(0,getLength()) + str;

            //if there is a negative sign at the beginning of the text field let it in
            if(offs == 0 && str.equals("-"))
            {
               super.insertString(offs, str, a); 
            }
            else
            {
                //catches and handles NumberFormatException
                try
                {
                    Double.parseDouble(string); //Changes string to a Double

                    super.insertString(offs, str, a); //displays it on the text field

                }
                catch(NumberFormatException e)
                {
                    //if NumberFormatExcpetion what thrown do nothing
                }
            }
        }
    }

}

PieChartJPaneltest.java

import javax.swing.*;

public class PieChartJPaneltest {

    /**
     * @param args
     */
    public static void main(String[] args) {

        PieChartFrame frame = new PieChartFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 300);
        frame.setVisible(true);
        frame.setResizable(false);

    }

}

Can you explain what the problem is?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.