0

Hi guys I know this is a common one, but i've searched around quite a bit and can't seem to get my paint method to draw over the components in my JPanel.

My paint method is linked to a button press. It prints out about 1500 data points and their assigned cluster (kmeans)

 package eye_pathscanner;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ReplayData extends JPanel {

// replay type can be parsed as argument into draw() to change paints behaviour
private int ReplayType = 0;

public ArrayList<DataPoint> points;

//Initialise records
public ReplayData()
{
    points = new ArrayList<DataPoint>();
}

public void ReplaceData() {
    points = new ArrayList<DataPoint>();
}

public void PrintPoints()
{

}
public void addPoint(DataPoint point) {
    points.add(point);

}

@Override
public void paintComponent(Graphics g) {

    Color black = new Color(0, 0, 0);
    Random random = new Random();
    final float luminance = 0.9f;

    if (ReplayType == 1)
    {
        super.paintComponent(g);

        for (int x = 0; x < kMeans.NUM_CLUSTERS; x++)
        {
            // Saturation ideal between 0.1 and 0.3
            float saturation = (random.nextInt(2000) + 1000) / 10000f;
            float hue = random.nextFloat();
            Color cluster_colour = Color.getHSBColor(hue, saturation, luminance);

            // Randomise the border colour
            saturation = (random.nextInt(2000) + 1000) / 10000f;
            hue = random.nextFloat();
            Color cluster_colour_border = Color.getHSBColor(hue, saturation, luminance);

            double centroidx = kMeans.centroids.get(x).getmX();
            double centroidy = kMeans.centroids.get(x).getmY();

            for (int i = 0; i < kMeans.TOTAL_DATA; i++) 
                if(kMeans.dataSet.get(i).cluster() == x){
                    // Set each child data point to a colour so you can see which cluster it belongs too
                    g.setColor(cluster_colour);
                    g.fillRect((int)TrackerData.getRecordNumber(i).getEyeX(),(int)TrackerData.getRecordNumber(i).getEyeY(), 3, 3);
                    g.drawLine((int)kMeans.dataSet.get(i).X(),(int)kMeans.dataSet.get(i).Y(), (int)centroidx, (int)centroidy);
                    //g.setColor(Color.black);
                    g.setColor(cluster_colour_border);
                    g.drawRect((int)TrackerData.getRecordNumber(i).getEyeX(),(int)TrackerData.getRecordNumber(i).getEyeY(), 3, 3);
                }

            g.setColor(black);
            g.fillOval((int)centroidx,(int)centroidy, 15, 15);


        }
    }


}

// 1 for K-means with different colour cluster groups
// 2 for slow replay

public void draw(int i) {
    ReplayType = i;
    repaint();
}

}

This code all works great for me, however I lose the image that was drawn beneath the paint after using it. I can maximize the page and the image shows up again but over the paint? (can anyone explain this behavior).

    JLabel picture_panel = new JLabel();
    picture_panel.setBounds(70, 130, 640, 797);
    picture_panel.addMouseListener(this);   
    BufferedImage img = null;

    try 
    {
        img = ImageIO.read(new File("C:/Eyetracker_Images/Random.jpg")); // eventually C:\\ImageTest\\pic2.jpg
        ImageIcon icon = new ImageIcon(img);
        picture_panel.setIcon(icon);

    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

Heres where my image is created, and it's called as shown below on one my buttons

 replayData.setBounds(0, 0, 802, 977);
 frame.getContentPane().add(replayData); 
 replayData.draw(1);  

Any help would be much appreciated, Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
does_not_compute
  • 476
  • 1
  • 7
  • 20
  • See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Apr 11 '15 at 11:13

1 Answers1

1

This maybe an artifact of using setBounds(). Moreover, you appear to be using the default layout manager without invoking pack() on the enclosing container.

As you already have a BufferedImage containing the rendered image, simply invoke drawImage() in your implementation of paintComponent(). Examples are seen here, here and here.

Also consider overriding the getPreferredSize() method of ReplayData, as suggested here and here, to establish its dimensions.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045