File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java Applet code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Applet code" Watch "Java Applet code" New topic

Java Applet code

Gustav Robertson

Joined: May 18, 2011
Posts: 1
Need help with my code! Trying to do a mousedrag to be able to "draw" in an applet. That part is succesful and works fine(kind of blinks a lot tho), but the button and random color do not work(the line is always black)So why isn't the button showing up in the applet and why wont the random color work?! thank you.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

Welcome to the Ranch!

Your button is doing nothing. You need to attach an ActionListener to it for it to work. But that won't solve all your problems.

First of all, whenever you override paint(Graphics) or paintComponent(Graphics) (the latter in Swing) you should always first call super.paint(g) / super.paintComponent(g). Furthermore, you are not telling the Graphics object to use another colour, unless that's what Expo.setRandomColor(g) does. Keep in mind that this method is called in paint, so each repaint will be in a new random colour.

Apart from migrating this code from AWT to Swing (using JApplet and JButton), I also suggest you don't use two int[]s. Instead, I'd create a new class (or use an existing one, like java.awt.Point) and use a List of that class.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Java Applet code
It's not a secret anymore!