prasad
prasad

Reputation: 387

Passing value from JFrame to JPanel

I am trying to draw a histogram of the Color values of the pixel of an image. I have done the job of getting the values but I want to draw the Histogram from that values. I am trying to draw it on the panel using paintComponent() method.

if(ae.getActionCommand()=="Hist")
{      
     jf1.add(new Histo());
     jf1.pack();         
     jf1.setVisible(true);
}

and

class Histo extends JPanel
{
  Zoom z = new Zoom();
  int x=800;


  public void paintComponent(Graphics g) {
         super.paintComponent(g);
         Graphics2D g2d = (Graphics2D) g;
         g2d.setColor(Color.red);
         g2d.drawLine(100, 100, 100,z.RedC[1]);
         for(int c=1;c<z.count;c++)
         {
           g2d.drawLine(x, z.RedC[c], 500, z.RedC[c]);
           x++;
         }
       }
  }

Zoom is the name of My class and with 'z' i can access the variables but their value is Zero. I cannot get the values that I have read from the pixel. So how can I access it in JPanel. Please help me in it.

Upvotes: 0

Views: 1192

Answers (2)

Null_Pointer
Null_Pointer

Reputation: 1

You can create a subclass of JPanel that holds the value and include getters and setters for the value like so:

public class Mystery extends JPanel{
    int x;

    public Mystery(int passMe){
        super();
        x = passMe;
    }

    public void setX(int changeto){
        x = changeTo; //could also make a method that says x++
    }

    public int getX(){
        return x;
    }
}

Upvotes: 0

D.R.
D.R.

Reputation: 21194

Add a constructor to your JPanel and give it the value or at least an interface to obtain the value later on.

Upvotes: 1

Related Questions