Solved

Question about Paint

Posted on 2004-09-04
19
205 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

691 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