Solved

Question about Paint

Posted on 2004-09-04
19
203 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 62
ejb entity bean example issue 2 16
how to debug htl and js pages 8 38
Selenium docs api java index 3 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

839 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