Super paint component not showing up (I have tried everything)

105 views Asked by At

Hello guys I am currently having problems with making anything show with my paint component. I have tried numerous things that I have seen online and tried a lot of different things myself but i Just can't get it to show anything. I am trying to get my paint component to show a series of bars that move as an array being sorted using swing but i can't even get it ti show anything. Any help would be appreciated.

package proj2;
import java.awt.*;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;


public class project2 extends JFrame
{
    private JButton button1 = new JButton("Insertion");         
    private UpdateTextFieldThread currentThread;   
    private int[] array = new int[15];  
    private Display display; 

    private class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Object src = e.getSource();
            if (src == button1){
                insertionSort(array);
                (new UpdateTextFieldThread()).execute();
            }
        }
    }

    public project2()
    {
        setTitle("Sorting Charts");
        setSize(400,250);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        for(int i=0; i<array.length;i++)    
            array[i]=(int) (Math.random()*100);
        display=new Display();

        ButtonHandler bh = new ButtonHandler();
        button1.addActionListener(bh);  

        JPanel container = new JPanel();
        container.setLayout(new GridLayout(2, 2));
        container.add(button1);

        JPanel masterPanel=new JPanel();
        masterPanel.setLayout(new BorderLayout());
        masterPanel.add(display, BorderLayout.SOUTH);
        masterPanel.add(container, BorderLayout.NORTH);

        setContentPane(container);
        setVisible(true);
        display.repaint();
    }

    private class Display extends JPanel    {
        private Color color = Color.RED;

        //@Override
        public void paintComponent(Graphics g) {
            super.paintComponents(g);
            g.setColor(Color.RED);
            Dimension d = getSize();
            int clientWidth = d.width;
            int clientHeight = d.height;
            int barWidth = clientWidth / array.length;
            for(int i=0;i<array.length;i++){
                int x=0;
                int y=15;
                int linethickness=5;
                int linelength=10;
                g.setColor(Color.red);
                g.fillRect(x, clientHeight, linelength*array[i] , linethickness);
                g.setColor(Color.black);
                g.drawRect(x,clientHeight, linelength*array[i], linethickness);
                x+=15;
            }
        }
    }

    private class UpdateTextFieldThread extends SwingWorker<Void, Integer>
    {
        protected Void doInBackground()
        {
            display.repaint();
            return null;
            //should have repaint method in here somewhere

            /*for (int i = 0; i < 50; i++) {
                publish(i);
                try {
                    Thread.sleep(THREAD_DELAY);
                } catch (InterruptedException e) { }
            }
            return null;*/
        }

        // The parameter here is a list of all published data
        //  since the last call to process.  We are interested
        //  in displaying only the latest one on the GUI.
        protected void process(java.util.List<Integer> list)
        {
        //    textfield.setText("" + list.get(list.size() - 1));
        }
    }

    public static <T extends Comparable<T>> void insertionSort(int[] hue2)
    {
        for (int i = 1; i < hue2.length; i++) {
            int thingToInsert = hue2[i];

            int j = i - 1;
            while (j >= 0 && thingToInsert<hue2[j]) {
                hue2[j+1] = hue2[j];
                j--;
            }

            hue2[j+1] = thingToInsert;
         }
     }

     public static void main(String[]args)  {
         new project2();
     }
}
1

There are 1 answers

3
camickr On BEST ANSWER
masterPanel.add(display, BorderLayout.SOUTH);

You are adding your Display panel to the SOUTH of a BorderLayout. The SOUTH constraint will respsect the preferred height of the component. Your component has a height of 0, to there is nothing to display.

Whenever you do custom painting you also need to override the getPreferredSize() method of the component to provide the appropriate size of the component so the layout managers can do there job.