Solved

Question about Paint

Posted on 2004-09-04
19
204 Views
Last Modified: 2010-03-31
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
Comment
Question by:ticoldam12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 6
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11981742
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
 

Author Comment

by:ticoldam12
ID: 11983145
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11983896
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 11986492
You are responsible for painting the pixels, so you can also remember what colour each pixel contains :)
0
 

Author Comment

by:ticoldam12
ID: 11991216
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11991245
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
 
LVL 92

Expert Comment

by:objects
ID: 11992879
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
 

Author Comment

by:ticoldam12
ID: 12024117
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
 
LVL 92

Expert Comment

by:objects
ID: 12024145
your frame needs a reference to the image (if it can reference the other class it could access it from there).
0
 

Author Comment

by:ticoldam12
ID: 12025900
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 12028931
(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
 
LVL 92

Expert Comment

by:objects
ID: 12031881
>      g.drawImage(secondClass.getImage()......

Thats what I already said :)
0
 
LVL 92

Expert Comment

by:objects
ID: 12031893
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12031913
>>Thats what I already said :)

Where?
0
 

Author Comment

by:ticoldam12
ID: 12032341
:) dont fight guys!! :)
Im going to try your example CEHJ and of course i will let you know if that worked for me!!
0
 
LVL 92

Expert Comment

by:objects
ID: 12032352
> :) dont fight guys!! :)

Not fighting, just pointing out to him that I suggested that earlier.
0
 

Author Comment

by:ticoldam12
ID: 12046030
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12046117
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12048869
Glad my suggestion helped, let me know if you need further assistance in the future :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 85
web application structure 18 117
Java: The Public Class Main 4 46
import as existing maven project 3 42
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question