GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Pixels on applets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Pixels on applets" Watch "Pixels on applets" New topic
Author

Pixels on applets

Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
What is the easiest way to draw pixels on an applet? All I want to do is have a white background and be able to draw single black pixels on it using specific x,y coordinates. Any help would be appreciated, thanks!


Want a free iPod, digital camera, or $300 cash? Completely legit offer at <a href="http://www.geocities.com/pricklymustard/freestuff.html" target="_blank" rel="nofollow">http://www.geocities.com/pricklymustard/freestuff.html</a> . I am looking for referrals which is why I need YOU!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Please do not post the same message in several forums. It makes the discussion harder to follow and decreases your likelihood of getting a good response.
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Sorry no one answered in the beginners forum so I thought it might belong in the intermediate forum...
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Given that it's a holiday (in the U.S.) and a weekend, I'd give it more than two hours.

Also, you might consider trying the Applets forum to get people with specific experience. This is really a basic Swing/AWT question, though. You should be able to place a JPanel (or its AWT equivalent) in your Applet and override the paint() method. Also, you could place an Image in the same place and then alter the image buffer behind it. I'm not as sure about this last one.
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Still don't have an answer on this one...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to Swing/AWT...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The answer is very slightly different for a Swing JApplet or a plain old Applet. Let's assume Applet.

Hava a member variable that's a LinkedList.

Have a method addPoint(Point) which takes the java.awt.Point object and adds it to the LinkedList.

Have another method removePoint(Point) which, predicatably, does the opposite of addPoint().

So, you override the paint() method. In paint(), traverse the LinkedList.

In paint, call Graphics.setForeground() and Graphics.setBackground() to set up the colors you want.

Then, write a loop. For each Point, call Graphics.drawLine() to draw a one-pixel long line at each Point's coordinates.

Now, in some code, somewhere, call addPoint() to add the points you want to draw.

And you're done.

If it's a JApplet, you should override paintComponent() instead of paint(), and call super.paintComponent() first; otherwise, it's the same.

OK?


[Jess in Action][AskingGoodQuestions]
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Hey thanks so much for taking the time to help me out. I still have a few questions relating directly to compile errors. Anyway, here is my code:

- It says it can't resolve symbol for super.paintComponent()
- It says it can't resolve symbol for Graphics.setForeground() and Graphics.setBackground()
- It says it can't resolve symbol for each of list.get(i).getX() (and getY) Specifically it says the getX() or getY() parts.

Any help would be greatly appreciated! Thanks!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The paintComponent() method you're overriding takes a Graphics object as an argument. You have to accept this argument, and pass it to the super() call.

Then, you have to call the setForeground(), etc, methods on that actual object -- they're not static methods.

Finally, when you get a Point object out of the LinkedList, you'll need to cast it back to a Point reference, and then you can call getX(), etc.
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Alright, let me see if I'm doing this right. Here is the code for the updated method (I didn't change any of the other code)


It is still giving me the same errors though, except the getX() and getY() errors are no longer there. But all of the Graphics related ones are... help! :-)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Never trust anything you read on the Internet

Sorry, I steered you wrong here; the setBackground and setForeground methods you want to call are members of the Component class (distant ancestor of JApplet). They also take java.awt.Color objects as arguments, not integers. So you want to say

setForeground(Color.BLACK);
setBackground(Color.WHITE);

It's a good idea to get to know how to use the online API docs. Whenever the compiler says a method doesn't exist, look it up and see what's going on.
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Hey,
Once again, thanks for the help! I finally got rid of all the compiler errors! I have one problem though - it's not drawing the pixels! I think it is because I need to call a method (update()?) to actually "paint" it on. I was going to put that call in my init() method, but it wouldn't let me call it without a Graphics object parameter. Since I can't instantiate a Graphics object, how am I supposed to call the update() method?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I can console myself with the thought that it takes a big man to admit his mistakes in public. I unfortunately told you something else wrong, although I suspect many here would sympathize. In a JApplet, you override paint(), not paintComponent(). With this small change -- and no call to super.paint() -- you should see, finally, your points being drawn. Painting is taken care of automatically, actually. You just have to implement
the right method, and Java does the rest.
Justin Porter
Ranch Hand

Joined: Nov 20, 2004
Posts: 34
Lol, don't worry about it! I'm just happy I finally got it working! Thanks for taking the time to help!
 
GeeCON Prague 2014
 
subject: Pixels on applets