0

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.

2 Answers 2

1

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

1
  • But JPanel does not allow to create user defined Constructors.. And how can I pass value through Interface??
    – prasad
    Mar 14, 2013 at 15:46
0

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.