Two Laptop Bag*
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 Android Security Essentials Live Lessons this week in the Android 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: 19655
    
  18

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Applet code
 
Similar Threads
Background image
move about?
How to shrink/grow lines
help w/ game involving threads and drawImage
Function about the class