Solved

Question about Paint

Posted on 2004-09-04
19
199 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now