• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Applet code

 
Gustav Robertson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic