aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Applet code" Watch "Java Applet code" New topic
Author

Java Applet code

Gustav Robertson
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Applet code