Stuck with the paint and repaint methods

I am supposed to create a JApplet with a button and a background color. When the button is clicked the first time a word is supposed to appear (I got that part), when the button is clicked the second time the word is supposed to appear again in a different color and in a different location on the Applet (I got that part).
The problem is that the first name is supposed to disappear when the second word appears. So I know I have to repaint the screen when the button is clicked the second time and draw the first string in the same color as the background color to make it invisible.
My problem is I am not sure how I can code to apply different settings each time the button is clicked. Can anyone help? Please let me know if my explanation sucks. I will try to explain better. However here is the code I have so far. I added a counter for the button just for testing purposes.
I just need some hints on what to do and if there is a easier way than using that if statement please let me know. I probably make it harder than it is.
Thanks in advance and Merry Christmas.
import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
    public class DisplayMyName extends JApplet
    implements ActionListener
      String myName = "DOG";
      String myName1 = "DOG";
      JButton moveButton = new JButton("Move It");
      Font smallFont = new Font("Arial", Font.BOLD, 12);
      Font largeFont = new Font("Lucida Sans", Font.ITALIC, 20);
      int numClicks = 0;
      JLabel label = new JLabel("Number of button clicks:" + numClicks);
       public void init()
         Container con = getContentPane();
         con.setBackground(Color.RED);
         con.setLayout( new FlowLayout() );
         con.add(moveButton);
         con.add(label);
         moveButton.addActionListener(this);
       public void paint(Graphics g)
         numClicks++;
         label.setText("Number of button clicks: " + numClicks);
         if (numClicks == 2)
         { g.setFont(smallFont);
            g.setColor(Color.BLUE);
            g.drawString(myName, 50, 100);
               else
         if (numClicks == 3)
         { g.setFont(largeFont);
            g.setColor(Color.YELLOW);
            g.drawString(myName, 100, 200);
       public void actionPerformed(ActionEvent move)
         repaint();
   }

You're putting your program logic in the paint method, something you should not do. For instance, try resizing your applet and see what effect that has on number of button clicks displayed. This is all a side effect of the logic being in the paint method.
1) Don't override paint, override paintComponent.
2) Don't draw/paint directly in the JApplet. Do this in a JPanel or JComponent, and then add this to the JApplet. In fact I'd add the button, the and the label to the JPanel and add the label to the JApplet's contentPane (which usually uses BorderLayout, so it should fill the applet).
3) Logic needs to be outside of paint/paintComponent. the only code in the paintComponent should be the drawing/painting itself. The if statements can remain within the paintComponent method though.
4) When calling repaint() make sure you do so on the JPanel rather than the applet itself.
For instance where should numClicks++ go? Where should the code to change the label go? in the paint/paintComponent method? or in the button's actionlistener? which makes more sense?
Edited by: Encephalopathic on Dec 24, 2008 9:37 AM

Similar Messages

  • Lightroom 5.2 my clone tool is stuck with the scissors and won't clone a spot. How do I get rid of the scissors?   HELP!!!!!

    My clone tool in Lr is stuck in the scissors mode. How do I get it back to normal clone function.
    Spending hours with this FRUSTRATION.
    CAN SOMEONE HELP ME?

    DOWNLOADED 5.7 CAN'T POST CLIP BECAUSE WHEN i LET GO OF THE ALT KEY THEY DISAPPEAR.
          Fr5.7om: Bob Somrak <[email protected]>
    To: Cat Anders <[email protected]>
    Sent: Sunday, February 15, 2015 5:22 PM
    Subject:  lightroom 5.2 my clone tool is stuck with the scissors and won't clone a spot. How do I get rid of the scissors?   HELP!!!!!
    lightroom 5.2 my clone tool is stuck with the scissors and won't clone a spot. How do I get rid of the scissors?   HELP!!!!!
    created by Bob Somrak in Photoshop Lightroom - View the full discussionFirst thing to do is update to LR 5.7.1.     No use trying to solve a problem on an outdated version. BTW, could you post a screen clip of these SCISSORS? If the reply above answers your question, please take a moment to mark this answer as correct by visiting: https://forums.adobe.com/message/7197183#7197183 and clicking ‘Correct’ below the answer Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: Please note that the Adobe Forums do not accept email attachments. If you want to embed an image in your message please visit the thread in the forum and click the camera icon: https://forums.adobe.com/message/7197183#7197183 To unsubscribe from this thread, please visit the message page at , click "Following" at the top right, & "Stop Following"  Start a new discussion in Photoshop Lightroom by email or at Adobe Community For more information about maintaining your forum email notifications please go to https://forums.adobe.com/thread/1516624.

  • Try updating my ipad  2 ,came back with error 1602, and now my ipad is stuck with the itune and usb icon on the screen

    Try updating my ipad 2, pop up with error 1602 and now my ipad is stuck with the itune and USB icon on the screen. I tried to restart it, but screen still comes up the same.

    See if this page helps http://support.apple.com/kb/TS3694#error1602

  • Stuck with the apple and the scrolling gear while booting

    Hi there
    i am using my mac for more than 2 years , i did not format it or even fix any thing into it but now something bad happend...
    accdentaly my genius friend preesed the power putton for more than 5 seconds to start up the computer and i heared a sound and a the normal flashing light under the track pad and suddenly i was stuck with the apple gray booting screen and the scrooling gear and the computer does not go further .. i left for 2 hours and it still stuck with the same screen ... try to restart do any thing no results also ...
    NOTE: THE BIG PROPLEM IS THAT I PUT A PASSWORD FOR THE FRIMWARE AND I FORGOOT IT SO I CAN NOT USE THE FOLLWOING:
    1-the ability to use the "C" key to start up from an optical disc.
    2-the ability to use the "N" key to start up from a NetBoot server.
    3-the ability to use the "T" key to start up in Target Disk Mode (on computers that offer this feature).
    4-Diagnostic volume of the Install DVD.
    5-mode by pressing the Command-S key combination during startup.
    6- a reset of Parameter RAM (PRAM) by pressing the Command-Option-P-R key combination during startup.
    Requires the password to use the Startup Manager, accessed by pressing the Option key during startup (see below).
    7- Safe mood.
    8-Single mode acsses and the other one i forrgot its name.
    SO I FIGURED THERE IS NO WAY BETER THAN THE PRIVIOUS WAYS TO SOLVE THE PROPLEM
    IF YOU HAVE ANY IDEAS OR ANY THING SO I CAN FIX THIS THING ?
    Thanks in advance >>>
    iBook G4 Mid 2005   Mac OS X (10.4.8)  

    I was mistaken about the PMU..with a little more digging I found that Open Firmware password can be removed if you change the amount of memory installed.
    I found this info here: http://archive.macosxlabs.org/documentation/firmwaresecurity/faqs/faqs.html#remove_passwordprotection
    The following is a link from Apple's site to download the user's manual for your iBook:
    http://manuals.info.apple.com/en/iBookG4(Mid2005)_UsersGuide.pdf
    It provides the instructions to add or remove memory from the machine. If you do have additional memory installed then you'll be able to remove the memory, and reset the PRAM, that way your password is gone. Hopefully from there we can figure out and fix your startup issue.
    Ben

  • Stuck with the Apple and nothing else

    It seems many are having problems with the recent software update. Me too - can anyone help?
    Following the update I am now stuck with the Apple sign. When I do a restore (Menu/Play) the apple sign dissaperars, then comes back and I am back where I started.
    My computer no longer recognises my iPod either!
    Any ideas?

    The most likely solution, Angel, is to restore your iPod then re-sync with iTunes to load your songs on. See this:
    Restoring iPod to Factory Settings

  • My iPod said that it needed to do an update but halfway thru it stopped and said there was an error and now it's stuck with the apple and an empty loading bar. Also it won't register with my computer. Help?

    I plugged in my iPod and it needed an update. Before the update could complete it said there was an error and then closed the window. Now my iPod is stuck in the loading screen and won't register in iTunes. Please Help.

    Try:
    - iOS: Not responding or does not turn on
    - Also try DFU mode after try recovery mode
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings
    - If not successful and you can't fully turn the iOS device fully off, let the battery fully drain. After charging for an least an hour try the above again.
    - Try on another computer
    - If still not successful that usually indicates a hardware problem and an appointment at the Genius Bar of an Apple store is in order.
    Apple Retail Store - Genius Bar       

  • I just tried to install a the new update for my iPad and it started but now its stuck with the apple in the window and it won't finish the update

    My iPad is stuck with the apple and the updating line but it is not finishing the update and the iPad is stuck I can't shut it off nothing is working just the bright white apple in the window.

    Hi rm44,
    Try resetting your device (nothing will be lost): Hold down the Home and Power buttons at the same time and continue to hold them down until the Apple appears (up to 30 seconds). Once the reset is complete, the Home or Lock screen will display. Once the device is back up, go to Settings>General>About, and see what version is displaying. If it has the iOS number you were updating to, then you are all set.
    If it has the old iOS version number, then the update did not take place. The best thing to do in that situation is to hook the device up to iTunes on the computer you sync with, and to update via iTunes.
    Cheers,
    GB

  • I wanted to restore my iphone 4s from 6.1 to 6.1.1 and itunes showed me (error -1) and i cant do nothing because its stuck with the itunes logo and the cable what should i do to fix it helppp please...

    i just wanted to restore from 6.1 to 6.1.1 on my iphone 4s and itunes gave me an error -1 and know it doesnt work i dont know if the warranty from 1 year of apple can fix it or what i can do to solve the problem... as i said i just wanted to update and know im stuck with the restore logo ( the itunes logo and the cable) need some help please!!!!

    no so i dont know what happened

  • My Iphone 4 will not show up in Itunes, and it wont turn on, it is stuck with the apple logo on the screen

    One day my Iphone would not turn back on after I turned it off, now it is stuck with the apple logo on it. Also I have tried plugging it into 3 different computers now, and Itunes will not recognize it. Please help.

    Brunolucy wrote:
    One day my Iphone would not turn back on after I turned it off, now it is stuck with the apple logo on it. Also I have tried plugging it into 3 different computers now, and Itunes will not recognize it. Please help.
    Try this:
    http://www.youtube.com/watch?v=fiTcKMZfyfk

  • Just tried to update my iPad 2 to the current iOS, and I got an error that said the update wouldn't install. The iPad is stuck in a funny mode with the apple and spinning wheel showing. I can't power it down or restart it. What next? Thanks.

    Just tried to update my iPad 2 to the current iOS, and I got an error that said the update wouldn't install. The iPad is stuck in a funny mode with the apple and spinning wheel showing. I can't power it down or restart it. What next? Thanks.

    Try a hard reset: hold the power and home buttons simultaneously until the Apple logo appears.

  • When trying to install os x mavericks to my macbook pro as it started to install the computer now is stuck with the OS X Mavericks intall showing and I can't get out of it or force quit?

    When trying to upgrade and install OS X Mmavericks to my Macbook Pro as it started to install the computer stopped the install and now is stuck with the OS X Mavericks intall window showing and I can't get out of it or force quit? I can not do anything else because this is stuck with this window/install screen showing. I have tried everything to get back to my regular desktop. I do not want to wipe the drive because I have photos on it that I want to save. So if there is any way to bypass this installer and get back to my regular desktop I would greatly appreciate the help.

    UPDATE*  Macbook7,1
    Mavericks 10.9.4
    8GB ram

  • TS3992 my ipad mini is stuck with the message on the screen 'this ipad hasn't been backed up in 5 weeks.  Backups happen when this ipad is plugged in, locked, and connected to Wi-Fi' with OK button.  If you click button nothing happens and I can't move an

    Ipad mini is stuck with the message 'this ipad hasn't been backed up in 5 weeks....' if you click the OK button nothing happens, cannot get rid of the message and cannot even power off b/c the message is on the screen.
    The ipad had not been backed up b/c it kept saying not enough storage available

    A reset may help. Tap and hold the Home button and the On/Off buttons for approximately 15-20 seconds, until the Apple logo reappears. When the logo appears, release both buttons.
    No content is affected by this procedure.

  • TS1702 Phone stuck on the updating screen. Black with the apple and loading bar.

    Updated to iPhone ios6. It started uploading the software. Iphone screen black with the apple and the loading bar. It's stuck there. Won't do anything. Itunes doesnt even recognize it anymore.

    iOS: Unable to update or restore

  • Iphone display stuck with the itunes icon and picture of usb drive

    My iphone display is stuck with the itunes icon and picture of usb drive. I've tried resetting the phone but it comes back each time. When connected to PC nothing happens. When I open itunes the phone is not recognized.

    see if this link helps http://support.apple.com/kb/HT1808

  • Help I just get stuck with the blank screen and the white circle

    help I just get stuck with the blank screen and the white circle after i erase all content and setting
    i dont know how to do pls help.

    Try connecting the device to your iTunes library and restoring it.  If necessary place the iPod into recovery mode using the instructions in this Apple support document.
    iOS: Unable to update or restore
    B-rock

Maybe you are looking for