• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pixels on applets

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry no one answered in the beginners forum so I thought it might belong in the intermediate forum...
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still don't have an answer on this one...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Justin Porter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lol, don't worry about it! I'm just happy I finally got it working! Thanks for taking the time to help!
 
reply
    Bookmark Topic Watch Topic
  • New Topic