Is there a way to run part of the Pain() method once?

I'm working on a paint program.
I'm using AWT and Applet supplied by Java, and in the Paint method I have it drawing the UI and checking to see if this or that has been clicked to change color. Problem is, the Paint method is always running, and so the UI is always being redrawn, and so the color is always black, which is the last color to be changed.
So, is there any way I can have it stop drawing the UI, and just checking the colors? Or is there another method to use to put the UI code in?
Here's the code:
// CiPaint
// Version 0.2
// Programmed and Designed by Paul Adamski
// This program is inflexible and exhibits poor design
// ZOMG!!
// TODO:     80% -      Changing colors
import java.awt.*;
import java.applet.*;
public class CiPaint extends Applet
     Rectangle red, green, blue, purple, black, white, yellow;
     Color backgroundColor, eraseColor, currentColor;
     int numColor;
     Image virtualMem;
     Graphics gBuffer;
     int oldX, oldY, newX, newY;
     int appletWidth;          
     int appletHeight;
     public void init()
          backgroundColor = Color.white;
          eraseColor = Color.black;
          red = new Rectangle(10,10,25,25);
          green = new Rectangle(40,10,25,25);
          blue = new Rectangle(70,10,25,25);
          purple = new Rectangle(100,10,25,25);
          yellow = new Rectangle(130,10,25,25);
          black = new Rectangle(160,10,25,25);
          white = new Rectangle(85,40,25,25);
          numColor = 0;
          appletWidth = getWidth();
          appletHeight = getHeight();     
          virtualMem = createImage(appletWidth,appletHeight);
          gBuffer = virtualMem.getGraphics();
          //gBuffer.setColor(Color.white); Makes screen flicker
          //gBuffer.setColor(Color.red); Makes whole right side of menu screen red
          gBuffer.fillRect(200,0,appletWidth-200,appletHeight);
     public void paint(Graphics g)
          // Draws the borders and UI     
          g.drawLine(200,0,200,600);
          g.drawLine(1,1,800,1);
          g.drawLine(800,1,800,600);
          g.drawLine(800,600,1,600);
          g.drawLine(1,600,1,1);
          g.setColor(Color.red);
          g.fillRect(10,10,25,25);
          g.setColor(Color.green);
          g.fillRect(40,10,25,25);
          g.setColor(Color.blue);
          g.fillRect(70,10,25,25);
          g.setColor(Color.magenta);
          g.fillRect(100,10,25,25);
          g.setColor(Color.yellow);
          g.fillRect(130,10,25,25);
          g.setColor(Color.black);     // This line unwantingly is the drawing color
          g.fillRect(160,10,25,25);     // This whole section is repeatidly called, it only should be called ONCE
          g.drawRect(85,40,25,25);
          // End Drawing
          switch (numColor)
               case 1:
                    g.setColor(Color.red);
                    currentColor = Color.red;
                    System.out.println("ITS RED NUBCAKE");
                    break;
               case 2:
                    g.setColor(Color.green);
                    currentColor = Color.green;
                    break;
               case 3:
                    g.setColor(Color.blue);
                    currentColor = Color.blue;
                    break;
               case 4:
                    g.setColor(Color.magenta);
                    currentColor = Color.magenta;
                    break;
               case 5:
                    g.setColor(Color.yellow);
                    currentColor = Color.yellow;
                    break;
               case 6:
                    g.setColor(Color.black);
                    currentColor = Color.black;
                    break;
               case 7:
                    g.setColor(Color.white);
                    currentColor = Color.white;
                    break;
               case 8:
                    break;
          // Works, but doesn't change color
          if (oldX > 200)
                    g.fillRect(oldX,oldY,2,2);
                    //g.drawImage(virtualMem,0,0,this);     
          /*UBER Flicker and doesn't even draw
          if (oldX > 200)
                    gBuffer.fillRect(oldX,oldY,2,2);
                    g.drawImage(virtualMem,0,0,this);     
     public boolean mouseDown(Event e, int x, int y)
          if(red.inside(x,y))
               numColor = 1;
          else if(green.inside(x,y))
               numColor = 2;
          else if(blue.inside(x,y))
               numColor = 3;
          else if(purple.inside(x,y))
               numColor = 4;
          else if(yellow.inside(x,y))
               numColor = 5;
          else if(black.inside(x,y))
               numColor = 6;
          else if(white.inside(x,y))
               numColor = 7;
          else
               numColor = 8;
          newX = x;
          newY = y;
          oldX = newX;
          oldY = newY;
          repaint();
          return true;
     public boolean mouseDrag(Event e, int x, int y)
          newX = x;
          newY = y;
          oldX = newX;
          oldY = newY;
          repaint();
          return true;
     public void update(Graphics g)
          paint(g);
}

You have only to move your switch block from paint method to mouseDown method: in this way you first assign correct value to currentColor variable.... then pain method will use the color you have already set.
Notice that methods mouseDown and mouseDrag are deprecated: you should better use processMouseEvent and processMouseMotionEvent methods (see documentation of class Component to more details.
Below the implementation I tried.
Bye
Diego
import java.awt.*;
import java.applet.*;
public class CiPaint extends Applet
     Rectangle red, green, blue, purple, black, white, yellow;
     Color backgroundColor, eraseColor, currentColor;
     int numColor;
     Image virtualMem;
     Graphics gBuffer;
     int oldX, oldY, newX, newY;
     int appletWidth;          
     int appletHeight;
     public void init()
          backgroundColor = Color.white;
          eraseColor = Color.black;
          red = new Rectangle(10,10,25,25);
          green = new Rectangle(40,10,25,25);
          blue = new Rectangle(70,10,25,25);
          purple = new Rectangle(100,10,25,25);
          yellow = new Rectangle(130,10,25,25);
          black = new Rectangle(160,10,25,25);
          white = new Rectangle(85,40,25,25);
          numColor = 0;
          appletWidth = getWidth();
          appletHeight = getHeight();     
          virtualMem = createImage(appletWidth,appletHeight);
          gBuffer = virtualMem.getGraphics();
          //gBuffer.setColor(Color.white); Makes screen flicker
          //gBuffer.setColor(Color.red); Makes whole right side of menu screen red
          gBuffer.fillRect(200,0,appletWidth-200,appletHeight);
     public void paint(Graphics g)
          // Draws the borders and UI     
          g.drawLine(200,0,200,600);
          g.drawLine(1,1,800,1);
          g.drawLine(800,1,800,600);
          g.drawLine(800,600,1,600);
          g.drawLine(1,600,1,1);
          g.setColor(Color.red);
          g.fillRect(10,10,25,25);
          g.setColor(Color.green);
          g.fillRect(40,10,25,25);
          g.setColor(Color.blue);
          g.fillRect(70,10,25,25);
          g.setColor(Color.magenta);
          g.fillRect(100,10,25,25);
          g.setColor(Color.yellow);
          g.fillRect(130,10,25,25);
          g.setColor(Color.black);     // This line unwantingly is the drawing color
          g.fillRect(160,10,25,25);     // This whole section is repeatidly called, it only should be called ONCE
          g.drawRect(85,40,25,25);
          // End Drawing
//          switch (numColor)
//               case 1:
//                    g.setColor(Color.red);
//                    currentColor = Color.red;
//                    System.out.println("ITS RED NUBCAKE");
//                    break;
//               case 2:
//                    g.setColor(Color.green);
//                    currentColor = Color.green;
//                    break;
//               case 3:
//                    g.setColor(Color.blue);
//                    currentColor = Color.blue;
//                    break;
//               case 4:
//                    g.setColor(Color.magenta);
//                    currentColor = Color.magenta;
//                    break;
//               case 5:
//                    g.setColor(Color.yellow);
//                    currentColor = Color.yellow;
//                    break;
//               case 6:
//                    g.setColor(Color.black);
//                    currentColor = Color.black;
//                    break;
//               case 7:
//                    g.setColor(Color.white);
//                    currentColor = Color.white;
//                    break;
//               case 8:
//                    break;
                g.setColor(currentColor);
          // Works, but doesn't change color
          if (oldX > 200)
                    g.fillRect(oldX,oldY,2,2);
                    //g.drawImage(virtualMem,0,0,this);     
          /*UBER Flicker and doesn't even draw
          if (oldX > 200)
                    gBuffer.fillRect(oldX,oldY,2,2);
                    g.drawImage(virtualMem,0,0,this);     
     public boolean mouseDown(Event e, int x, int y)
          if(red.inside(x,y))
               numColor = 1;
          else if(green.inside(x,y))
               numColor = 2;
          else if(blue.inside(x,y))
               numColor = 3;
          else if(purple.inside(x,y))
               numColor = 4;
          else if(yellow.inside(x,y))
               numColor = 5;
          else if(black.inside(x,y))
               numColor = 6;
          else if(white.inside(x,y))
               numColor = 7;
          else
               numColor = 8;
          newX = x;
          newY = y;
          oldX = newX;
          oldY = newY;
          switch (numColor)
               case 1:
                    currentColor = Color.red;
                    System.out.println("ITS RED NUBCAKE");
                    break;
               case 2:
                    currentColor = Color.green;
                    break;
               case 3:
                    currentColor = Color.blue;
                    break;
               case 4:
                    currentColor = Color.magenta;
                    break;
               case 5:
                    currentColor = Color.yellow;
                    break;
               case 6:
                    currentColor = Color.black;
                    break;
               case 7:
                    currentColor = Color.white;
                    break;
               case 8:
                    break;
          repaint();
          return true;
     public boolean mouseDrag(Event e, int x, int y)
          newX = x;
          newY = y;
          oldX = newX;
          oldY = newY;
          repaint();
          return true;
     public void update(Graphics g)
          paint(g);
}

Similar Messages

  • TS2446 I have window xp is there another way to run and install the icloud

    I have windows xp is there another way to run and install the icloud

    Your question makes no sense and I'm not sure if you typed very quickly and autocorrect went nuts on you, or if you are not a native English speaker and Google Translate didn't work properly. If you are saying that you forgot the passcode to unlock the iPad, follow the instructions here.
    Forgot passcode for your iPhone, iPad, or iPod touch, or your device is disabled - Apple Support

  • Is there a way to run javascript in the url in FF6?

    With the introduction of scratchpad to FF6 the javascript in the url is filtered out and will not execute.
    The only way i have been able to run js scripts is to use the scratchpad even if it's a one line thing that i need.
    I went into about:config and typed javascript into the filter then changed the browser.urlbar.filter.javascript configuration from true to false, nothing changed.

    Running javascript: in the urlbar is no longer supported in version 6 for security reasons. Users were installing malware. Please use the scratchpad.

  • Is there any way to run Flash on the Minefield 4.0 Beta?

    I know this is a long shot as Minefield is only a beta at the moment and flash is notoriously problematic for 64-bit users! I'm trying to run Flash in the browser on the 64-bit version of 7, it is installed on the laptop but whenever I try to run it in Minefield a message comes up saying I need to install Flash! Is there any solution of workaround to this? Thanks :)

    Your above posted system details show outdated plugin(s) with known security and stability risks.
    *Next Generation Java Plug-in 1.6.0_15 for Mozilla browsers
    Update the [[Java]] plugin to the latest version.
    *http://java.sun.com/javase/downloads/index.jsp (Java Platform: Download JRE)

  • I made a windows partition on my mac but now i cant swap back to the mac part so is there any way that i can delete the windows partition whilst running windows 7 so i can go back to the mac part

    i made a windows partition on my mac but now i cant swap back to the mac part so is there any way that i can delete the windows partition whilst running windows 7 so i can go back to the mac part

    You are telling us you read and tried the normal methods?
    Boot Camp or Taskbar. Option on restart. "x" on startup. And none of those work.

  • Is there a way to run a program designed for PC on my Mac?

    I would like to use the Firearms Management System offered by one of my suppliers on my Mac but the only operating system shown to use is with is Windows. Is there a way to run it on my iMac?

    Sure, many:
    (Borrowed from Kappy)
    Windows on Intel Macs
    There are presently several alternatives for running Windows on Intel Macs.
    Install the Apple Boot Camp software.  Purchase Windows XP w/Service Pak2, Vista, or Windows 7.  Follow instructions in the Boot Camp documentation on installation of Boot Camp, creating Driver CD, and installing Windows.  Boot Camp enables you to boot the computer into OS X or Windows.
    Parallels Desktop for Mac and Windows XP, Vista Business, Vista Ultimate, or Windows 7.  Parallels is software virtualization that enables running Windows concurrently with OS X.
    VM Fusionand Windows XP, Vista Business, Vista Ultimate, or Windows 7.  VM Fusion is software virtualization that enables running Windows concurrently with OS X.
    CrossOver which enables running many Windows applications without having to install Windows.  The Windows applications can run concurrently with OS X.
    VirtualBox is a new Open Source freeware virtual machine such as VM Fusion and Parallels that was developed by Solaris.  It is not as fully developed for the Mac as Parallels and VM Fusion.
    Note that Parallels and VM Fusion can also run other operating systems such as Linux, Unix, OS/2, Solaris, etc.  There are performance differences between dual-boot systems and virtualization.  The latter tend to be a little slower (not much) and do not provide the video performance of the dual-boot system. See MacTech.com's Virtualization Benchmarking for comparisons of Boot Camp, Parallels, and VM Fusion. Boot Camp is only available with Leopard or Snow Leopard. Except for Crossover and a couple of similar alternatives like DarWine you must have a valid installer disc for Windows.
    You must also have an internal optical drive for installing Windows. Windows cannot be installed from an external optical drive.

  • Is There a Way to Run a Redo log for a Single Tablespace?

    I'm still fairly new to Oracle. I've been reading up on the architecture and I am getting the hang of it. Actually, I have 2 questions.
    1) My first question is..."Is there a way to run the redo log file...but to specify something so that it only applies to a single tablespace and it's related files?"
    So, in a situation where, for some reason, only a single dbf file has become corrupted, I only have to worry about replaying the log for those transactions that affect the tablespace associated with that file.
    2) Also, I would like to know if there is a query I can run from iSQLPlus that would allow me to view the datafiles that are associated with a tablespace.
    Thanks

    1) My first question is..."Is there a way to run the
    redo log file...but to specify something so that it
    only applies to a single tablespace and it's related
    files?"
    No You can't specify a redolog file to record the transaction entries for a particular tablespace.
    In cas if a file gets corrupted.you need to apply all the archivelogs since the last backup plus the redologs to bring back the DB to consistent state.
    >
    2) Also, I would like to know if there is a query I
    can run from iSQLPlus that would allow me to view the
    datafiles that are associated with a tablespace.Select file_name,tablespace_name from dba_data_files will give you the
    The above will give you the number of datafiles that a tablespace is made of.
    In your case you have created the tablespace iwth one datafile.
    Message was edited by:
    Maran.E

  • Is there a way to run Motion and Final Cut Pro on Separate Machines?

    Is there a way to run Motion and Final Cut Pro from the same Final Cut Studio license on separate machines? We hadn't realised when we bought Final Cut Studio that it wouldn't allow this. Our editor doesn't use motion, and while I mainly use After Effects on our motion graphics machine, I was wanting to try using motion for some particle effects. Unfortunately, it won't let me open it while Final Cut is running on our editor's machine. I can understand not letting two copies of same application run under the same serial number at the same time, but it would be nice to be able to use both programs that we own concurrently. At very least it would be good to warn you that this isn't possible in the product description, not in some fine print license agreement once you have already bought the software.
    -Matt

    I do not know of any software, in any platform that can operate from the same license over a network simultaneously.This is the exact reason for the existence of Volume Licensing. If for example a project ran in each one of the included applications from different sources that were not an active network, I doubt you would have a problem.Like, doing a project at work and geting the outcome with the Powerbook at home...Sorta!

  • Is there a way to run Internet Explorer on a Mac mini with mountain lion?

    I need to access a professional website that only works with IE (don't get me started on THAT) I need to do billing using this site so need to have full functionality.  Is there a way to run IE on my mac mini with mountain lion?

    The key takeaway here is that yes, you can access Windows 7 (and IE 9) from OS X Mountain Lion.
    The crucial question is about your workflow. If you need concurrent access to both Windows 7 and Mountain Lion, then the Bootcamp solution included with Mountain Lion would not be the right choice. You would need one of the virtualization solutions.
    If you need helpdesk support from a paid product, rather than entirely from a community forum, then the latest Parallel's Desktop or VMware would be a matter of choice, with Parallel's the better decision at this point. Either of these products are under $100, in addition to the cost of a Windows 7 license. You may need the Windows 7 installation media as an .ISO file, rather than on DVD. Check requirements.
    By example, I use Oracle's VirtualBox (free) on Mountain Lion, with a Windows 7 Home Premium 64-bit guest. The interactive and network performance of this Windows guest are more than adequate on my 2011 mini. I assigned 3 GB memory and 2 of 4 processor threads to the Windows 7 guest. I also restricted guest CPU use to 80%. VirtualBox is installed on my SSD, but the Windows guest is on an external HDD. That is not a default configuration. There are multiple choices for display; from full-screen 1920x1200 (mine) to an isolated resizeable window, to seamless integration with OS X. The Apple keyboard, bluetooth devices (trackpad/mouse), and printer all are recognized. I also have a shared OS X folder with VirtualBox, and copy/paste integration.
    I use VirtualBox as it meets my support (forum) and functional requirements.
    Best wishes,

  • Is there a way to run a 2005 PreSonus Firebox(with the firewire) to a mid-2011 iMac 0SX 10.7.2? It says its no longer supporting Power PC? Anything I can do without spending more money on new equipment or a new computer? Please help me.

    Is there a way to run a 2005 PreSonus Firebox(with the firewire) to a mid-2011 iMac 0SX 10.7.2? It says its no longer supporting Power PC? Anything I can do without spending more money on new equipment or a new computer? Please help me.

    Nevermind, I got it working.
    I ended up extracting the new vob within MPEG Streamclip, which gave me an m2v and an aiff audio. I multiplexed those two back together in FFMpegX which gave me a complete VIDEO_TS folder with BUP and IFO files I needed.

  • Is there a way to run a external ssd with my mid 2011 iMac hdd in a raid configuration and basically have a fusion drive

    is there a way to run a external ssd with my mid 2011 iMac hdd in a raid configuration and basically have a fusion drive

    A Fusion Drive is not a RAID; it's a CoreStorage logical volume group. While it's technically possible to do as you suggest, there would be little or no benefit from it.

  • Is there any way to access files from the Classic environment to my iMac running on Leopard (10.5.8)?

    Is there any way to access files from the Classic environment to my iMac running on Leopard (10.5.8)?

    Sorry, no.

  • The hotkey for Private Browsing ( CTRL Shift P ) interferes with another program I have running in the background. Is there a way to disable or change the Firefox Hotkey?

    Question:
    The hotkey for Private Browsing (<CTRL><Shift> <P>) interferes with another program I have running in the background. Is there a way to disable or change the Firefox Hotkey?

    You can try this extension:
    *Customizable Shortcuts: https://addons.mozilla.org/firefox/addon/customizable-shortcuts/

  • Is there any way to run abap program in dialog process with process chain?

    Hi.
    I just want to run custom abap program in every 30min.
    so I made process chain and connect abap program.
    but it returns NOTHING when background running.
    the program has ABSOLUETLY no problem when it's running DIALOG process.
    custom program is modified from sap standard program for my use.
    so complicated and hard to read that I cann't figure out what's problem in backgound job and even hard to debug in background process.
    is there any way to run abap program in dialog process in every 30 min.?
    or call BSP page or call function.
    any solution will be helpful that excuting PROGRAM or FUNCTION or BSP PAGE in schedule job and should running dialog process.
    thanks.
    Lee.

    Hi,
    did you try using sm36 transaction?
    1Give these values in Gereral data
    Create one sample job "SAMPLE JOB"
    Priority "A"
    Execu target - Your application server
    2Click start conditon
    schedule according to ur requirements.
    3.Click Step button
    Give your ABAP program name.
    fill variants if it has any variants.
    if it is useful assign points
    Regard,
    Senthil Kumar.P

  • I forgot my password and email Forgot your private cloud small my daughter and I can not use the device jewelery Is there another way to run the machine?

    I forgot my password and email Forgot your private cloud small my daughter and I can not use the device jewelery Is there another way to run the machine?
    I hope there is a solution to this problem note that I can not use the iPad now

    Your question makes no sense and I'm not sure if you typed very quickly and autocorrect went nuts on you, or if you are not a native English speaker and Google Translate didn't work properly. If you are saying that you forgot the passcode to unlock the iPad, follow the instructions here.
    Forgot passcode for your iPhone, iPad, or iPod touch, or your device is disabled - Apple Support

Maybe you are looking for

  • X100e Hanging and Freezing on Wireless

         There was a different thread addressing freezing and hanging when connected via ethernet; however, I have had ethernet disabled from the BIOS since day one of receiving my system but I found my system to be locking up extremely often particularl

  • Deletion of records

    Hello, i am getting from user..and i want to delete the records that starts with that user input...how to write query for this? delete * from emp where emp_name like 's%'... the above query is for deleting records of the employee whose name starts wi

  • NHL app not working

    I have gamecenter live and for some reason on the Apple TV app it won't connect. It allows me to see what games are playing but when I try to watch a game it says that I must wither login or restore, if I hit restore it says I can't connect to iTunes

  • Is there a program to extract email addresses from a searchable pdf?

    Is there a program that will extract email addresses from a searchable pdf? I scanned a 75 page excel spreadsheet and used OCR to create a searchable pdf. I've verified that the OCR did work, the email address are searchable, but I need a way to extr

  • How can i make a family account

    ok so heres the story my brother has an ipod 5 and i have an ipad mini 2 and we wanna use our seperate apple id because when i go to purchases i see all of his history and i like to keep mine clean and tidy for some reason... i want my ipad mini 2 to