Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Question about Paint

I have a frame 1024x768 pixels and i want to get the color of one pixel given x and y. Im using public void paint (graphics g) and i have all the frame in black color.
Basically what id like to find is what do i need to do to get the color of the pixel. Also id like to create another class that generates random x and y numbers and get the color of the pixel in the frame.
Thank you for your help..I appreciate it!!
0
ticoldam12
Asked:
ticoldam12
  • 7
  • 6
  • 6
1 Solution
 
CEHJCommented:
You could use a BufferedImage for the drawing and use http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/BufferedImage.html#getRGB(int, int), and also the corresponding setRGB
0
 
ticoldam12Author Commented:
so how would the code be to paint with Color.black a 1024x768 area?? I have never used BUfferedImage to paint an area like that...
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
objectsCommented:
You are responsible for painting the pixels, so you can also remember what colour each pixel contains :)
0
 
ticoldam12Author Commented:
Thank you for the information CEHJ!! I could find how to "paint" the my image in memory.
based on the research i did, i found the following: (this is what i want)

public void paint (Graphics g) {

             BufferedImage img = new BufferedImage (1024,768,BufferedImage.TYPE_INT_RGB);
             g = img.createGraphics;
             g.setColor(Color.black);
             g.fillRect(0,0,1024,768);
             ....
             ....
             ....
             int a = img.getRGB(100,100);   //for example
}

but what happens is that nothing is being painted in the frame. Im using awt, and i know that maybe i need something where to paint the color, let's say a frame. When i create a method like paint, is this supposed to paint what i want in the frame it just created??
What should i do to fix the problem in the code??
0
 
CEHJCommented:
You shouldn't create the image in paint. Create it beforehand and just draw on it in paint before displaying it. You will at any time be able to call getRGB on it and determine the latest value of the pixel - although in that example it's not really in doubt is it? ;-)
0
 
objectsCommented:
Try the following, and when you need to change what is displayed just set img to null.

private Image getOffscreen() {

    if (img==null)
    {
             img = new BufferedImage (1024,768,BufferedImage.TYPE_INT_RGB);
             g = img.createGraphics();
             g.setColor(Color.black);
             g.fillRect(0,0,1024,768);
             g.dispose();
   }
}

public void paint(Graphics g) {
    Image i = getOffscreen();
    if (i!=null)
    {
        g.drawImage(img, 0, 0, null);
    }
}
0
 
ticoldam12Author Commented:
Thank you for all the information CEHJ and object, i have got a lot of progress in my software,thanks to you guys, but now im getting a problem that i dont know how to resolve.

I have in my main frame the paint method, which is fine. Cool!, but i have a second class that has in it an image:

Image temp = this.getToolkit().getImage("Picture.jpg");

I would like to draw "temp" in my frame, but the *.drawImage method needs to be in the second class. In other words, if i have drawImage in my second class...how do i reach my main frame in order to draw "temp"??
Is there something i need to read??

I know this is weird, but due to the kind of program im doing, i'll need to do it in that way..:(
0
 
objectsCommented:
your frame needs a reference to the image (if it can reference the other class it could access it from there).
0
 
ticoldam12Author Commented:
I just found that i could make something like this in my second class:

Graphics g = getGraphics();
g.drawImage(image,100,100,this);

Im not sure if this is going to get from my second class my main frame where the paint method is...with this little code in my second class, this should resolve the problem??
0
 
CEHJCommented:
(Let your second class be called 'SecondClass'):

public class SecondClass {
      private Image image;

      public void setImage(Image image) {
        this.image = image;
      }

      public Image getImage() {
        return image;
      }
      
      //etc.

}

(In JFrame)
public void paint(Graphics g) {
      super.paint(g);
      g.drawImage(secondClass.getImage()......
}
0
 
objectsCommented:
>      g.drawImage(secondClass.getImage()......

Thats what I already said :)
0
 
objectsCommented:
> with this little code in my second class, this should resolve the problem??

I don't think so.
If you want the image to be painted on the frame then you'll need to paint it from the frames paint method.

You could always just load the image again.
Or have whatever class loads the image pass it to your frame instance.
0
 
CEHJCommented:
>>Thats what I already said :)

Where?
0
 
ticoldam12Author Commented:
:) dont fight guys!! :)
Im going to try your example CEHJ and of course i will let you know if that worked for me!!
0
 
objectsCommented:
> :) dont fight guys!! :)

Not fighting, just pointing out to him that I suggested that earlier.
0
 
ticoldam12Author Commented:
Ok guys, finally im really happy because i could make my software. I was doing a firefliy simulator with genetic algorithms. Now that im done i would like to appreciate all the help i recieved from you:

>>Let your second class be called 'SecondClass'):

public class SecondClass {
     private Image image;

     public void setImage(Image image) {
       this.image = image;
     }

     public Image getImage() {
       return image;
     }
     
     //etc.

}

(In JFrame)
public void paint(Graphics g) {
     super.paint(g);
     g.drawImage(secondClass.getImage()......
>>}

this little example saved my life!! thank you CEHJ, and also object for the info provided!!
0
 
CEHJCommented:
8-)
0
 
objectsCommented:
Glad my suggestion helped, let me know if you need further assistance in the future :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now