Beginner with a programming problem

I am getting quite frustrated trying to figure out how to proceed with a Java program I've been assigned. I'm hoping that my problem is not too amorphous to warrant assistance.
The program is an exercise in Swing and creating GUI's, and at this stage, I only need to create an array of dice objects which can paint themselves on a JPanel. The instructor has given us two files -- one extending JPanel and containing a main method to test the classes, and another which represents a die.
Some issues are
1) I don't know where to call repaint.
2) When the overridden paintComponent method is called, it wipes out
the background color on my panel, which I set in the constructor.
3) I got null pointer exceptions when I ran the instructor 's main method... I moved the setVisible up in the chain and they went away (Though he stated not to alter the main method, I don't know enough about Java Graphics to know if it's his mistake or mine)
4) I'd just like to see some something appear.
I should add that I rewrote this whole thing in one file (without using an array) and was able to get the graphics to paint.
Sorry for posting copious amounts of code, but hopefully it will clear up what I'm trying to do. If anyone could just give me some pointers in the right direction, I would greatly appreciate it. Thank you for your time.
//      File:  DicePanel.java
public class DicePanel extends JPanel
  private int number_of_dice = 0;
  private DiceDrawn [] dice; 
  /** The x,y coordinates where the first die is drawn */
  private int xCoordinate = 5; //default
  private int yCoordinate = 5; //default
  /** Constructor with default properties */
  public DicePanel()
    this.setSize(getPreferredSize());
    this.setBackground(Color.blue);
    // set size and color
  /*  Set the Dice array  */
  public void setDice(DiceDrawn[] d)
  }// end setDice
  /** Return xCoordinate */
  public int getXCoordinate()
    return xCoordinate;
  /** Set a new xCoordinator */
  public void setXCoordinate(int x)
     xCoordinate = x;
  /** Return yCoordinator */
  public int getYCoordinate()
    return yCoordinate;
  /** Set a new yCoordinator */
  public void setYCoordinate(int y)
     yCoordinate = y;
  /** Paint the Dice Panel */
  protected void paintComponent(Graphics g)
    super.paintComponent(g);
    for (int i = 0; i < dice.length; i++ )
      dice.drawDie(g, xCoordinate, yCoordinate);
setXCoordinate(getXCoordinate() + 41);
// draw each die using default start coordinates,
// moving right each time
/** Override get method for preferredSize */
public Dimension getPreferredSize()
return new Dimension(200, 50);
// the main method to test DicePanel and DiceDrawn
// do not change this method.
public static void main(String [] args)
JFrame f = new JFrame();
// f.setVisible(true);
f.setTitle("Draw Dice");
f.setSize(500,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = f.getContentPane();
// show 6 dice initially
DiceDrawn [] dice = new DiceDrawn [6];
for(int i = 0; i < dice.length; i++)
dice[i] = new DiceDrawn(i+1);
DicePanel dicePanel = new DicePanel();
dicePanel.setDice(dice);
System.out.println(dice.length);
cp.add(dicePanel, BorderLayout.NORTH);
f.setVisible(true);
// show only one die for the rest of the program
dice = new DiceDrawn [1];
System.out.println("Dice Drawn Class Test");
int i = Io.readInt("Enter an integer between 1 and 6");
while( i > 0 )
dice[0] = new DiceDrawn(i);
dice[0].setSelected( i%2 == 0 ? true : false);
dicePanel.setDice(dice);
i = Io.readInt("Enter an integer between 1 and 6");
System.exit(0);
// File: DiceDrawn.java
public class DiceDrawn
private final int SIZE = 36;
private final int QUARTER = SIZE/4;
private int face; // face value
private boolean selected; // pressed = true
public DiceDrawn( int f)
setFace(f);
setSelected(true);
}// end DiceDrawn
public void setSelected( boolean s )
selected = s;
public boolean isSelected()
return selected;
public void setFace(int i)
face = i;
public int getFace()
return face;
// Draw a die starting at the given coordinates
public void drawDie(Graphics g, int xcoor, int ycoor)
g.setColor(Color.WHITE);
g.fillRect(xcoor,ycoor, SIZE, SIZE);
g.setColor(Color.BLACK);
switch(face)
case 1:
g.fillOval((xcoor-4)+(2*QUARTER), (ycoor-4)+(2*QUARTER), SIZE/8, SIZE/8);
break;
case 2:
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4) + QUARTER, SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+(3*QUARTER), (ycoor-4)+(3*QUARTER), SIZE/8, SIZE/8);
break;
case 3:
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4) + QUARTER, SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+(3*QUARTER), (ycoor-4)+(3*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+(2*QUARTER), (ycoor-4)+(2*QUARTER), SIZE/8, SIZE/8);
break;
case 4:
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4) + QUARTER, SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+(3*QUARTER), (ycoor-4)+(3*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4) + (3*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+ (3*QUARTER), (ycoor-4)+ QUARTER, SIZE/8, SIZE/8);
break;
case 5:
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4) + QUARTER, SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+(3*QUARTER), (ycoor-4)+(3*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4) + (3*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+ (3*QUARTER), (ycoor-4)+ QUARTER, SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+(2*QUARTER), (ycoor-4)+(2*QUARTER), SIZE/8, SIZE/8);
break;
case 6:
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4) + QUARTER, SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+(3*QUARTER), (ycoor-4)+(3*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4)+(2*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+ (3*QUARTER), (ycoor-4)+(2*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+ QUARTER, (ycoor-4) + (3*QUARTER), SIZE/8, SIZE/8);
g.fillOval((xcoor-4)+ (3*QUARTER), (ycoor-4)+ QUARTER, SIZE/8, SIZE/8);
break;
// draw a die using a rectangle of 36 by 36 pixels
// at the coordinates given.
// use a switch statement to draw the appropriate face.
}// end DrawDie
}// end DiceDrawn

Once you get beyond your initial problems, you'll find that there is another problem. When it displays the dice corresponding to the numbers you select, the x coordinate eventually becomes greater than the frame width. You need to make a change to the paintComponent method so it looks like:
  /** Paint the Dice Panel */
  protected void paintComponent(Graphics g)
    super.paintComponent(g);
    for (int i = 0; i < dice.length; i++ )
      dice.drawDie(g, xCoordinate, yCoordinate);
if(dice.length > 1)
setXCoordinate(getXCoordinate() + 41);
// draw each die using default start coordinates,
// moving right each time
Note that I have added an if above the setXCoordinate so that it will only add to the X coordinate when it is displaying the 6 dice initially. It won't add anything when it is displaying the single dice.

Similar Messages

  • Coldfusion server problem or programming problem?

    Hello experts,
    I have been experiencing problems when accessing this website using Mac (and Parallels/WinXP).
    http://tinyurl.com/3yh3d8l
    Because there is no problem when using Windows PC, the programmer suggested that it might be a CF server problem, but one of my IT said it's programming issue (basically he said the web programmer is wrong). I have very limited access to PC at work but I need the data from that website. I'd like to give input to the webprogrammer but I don't know what to say. Could you please give me some suggestions? Thank you.

    hi Adam,
    Just tested in Mac, the site works in FireFox. But Safari won't load it at all. Progress bar keep spinning but screen doesn't change.
    So is this something to do with the programming problem or Safari is being picky? I can use FireFox during presentation but I'd like the option to be able to use Safari (iPad?).

  • The problem I have since I upgraded to Mavericks version 10.9.1 The problem appears only with Mail not with other programs, not even with my browser. When I try to zoom the text of an e-mail I received or sent , I can no longer use the keys Command   to e

    the problem I have since I upgraded to Mavericks version 10.9.1
    The problem appears only with Mail not with other programs, not even with my browser.
    When I try to zoom the text of an e-mail I received or sent , I can no longer use the keys Command + to enlarge the text, although I can reduce it with Command -.
    As I have a problem with my eyes, This is a serious matter for me.
    When I write an e-mail, if I select text and press Command +, it just displaces the text to the right.
    Now, my husband has a USB keyboard. If he connects it to my computer, his regular Command + does not work either, but  he uses the extended keyboard, then it works. Unfortunately, he needs it for a musical application which does not work with a wireless keyboard.

    Firefox 3.6.4 and 3.6.6 use a process called, "plugin-container.exe" which was using up most of my CPU when I opened up multiple tabs that contained Adobe Flash files, and caused Firefox to lock up.
    My solution was to use Firefox 3.5.10 which you can get from the Mozilla website at [http://www.mozilla.com/en-US/firefox/all-older.html]
    I am using Adobe Flash 10.1.53.64 without any problem in this version of Firefox. Check the release notes, I believe it contains all the latest security fixes in "Firefox 3.6.4".
    Hopefully, they will fix Firefox 3.6 in the next version (e.g. Firefox 3.6.7), until then you should probably use "Firefox 3.5.10".

  • Problem with the program symbols in quality

    Dear all,
    I am facing a problem with the program symbols.
    I have added new global fields in development, i have called those definations in text names. It is working fine.
    After transporting these  program changes in to quality these new global definations are not displaying in quality and varibles are not working.
    Please give me some suggestion.
    Thanks and advance

    Hi Chandra,
    Please check the following.....
    (1)The transport request status in se10 and see if has been successfully imported to QA..
    (2)if all the declarations or code have been collected in a Transport and not saved as temporary objects
    To confirm the same we have to compare the code in quality and development and see if they are the same
    Pls check and revert
    Regards
    Byju

  • Description: A problem caused this program to stop interacting with Windows. Problem signature: Problem Event Name: AppHangB1 Application Name: firefox.exe Application Version: 1.9.2.3855 Application Timestamp: 4c48d5ce Hang Signature: 9962

    I am having this problem, in the first window when I try to do anything.
    Description:
    A problem caused this program to stop interacting with Windows.
    Problem signature:
    Problem Event Name: AppHangB1
    Application Name: firefox.exe
    Application Version: 1.9.2.3855
    Application Timestamp: 4c48d5ce
    Hang Signature: 9962
    Hang Type: 0
    OS Version: 6.0.6002.2.2.0.768.3
    Locale ID: 1033
    Additional Hang Signature 1: 5df72ce88195c0212c542e9c8c172716
    Additional Hang Signature 2: 2b94
    Additional Hang Signature 3: 9acafbb8ad01bf9d2eb258d8fddad3ca
    Additional Hang Signature 4: 9962
    Additional Hang Signature 5: 5df72ce88195c0212c542e9c8c172716
    Additional Hang Signature 6: 2b94
    Additional Hang Signature 7: 9acafbb8ad01bf9d2eb258d8fddad3ca
    == This happened ==
    Every time Firefox opened
    == User Agent ==
    Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4

    I suspect a RAM or hardware problem.

  • Photoshop Elements 12 no longer see's my scanner.  So I cannot scan my pictures, help.  Windows 8.1 with HP Officejet 6500A Plus printer.  Scanner works with other programs in my PC.  Problem is PS12

    So I cannot scan my pictures, help.  Windows 8.1 with HP Officejet 6500A Plus printer.  Scanner works with other programs in my PC.  Problem is PS12

    I bought the Officejet 6500A Plus about one month and have experienced the very same problem. I tried to remedy this problem following suggestions in a number of different posts (static IP, router, firewalls etc), all to no avail. Last week I called support. The support I received by the tech over several hours and different days for a week was very good. However none of his suggestions corrected the connection issue. After today's recommended fix failed, he advised HP is aware of this issue with the 6500 and other affected printers. He claimed HP was working on a firmware upgrade. He did not know when the update would be provided. The tech also claimed HP acknowledged this problem in a written announcement. He agreed to email the announcement to me. I haven't received it. This has been the only problem I've had with the 6500

  • I installed Adobe CS6 Design & Web Premium on my Mac Book Pro a few years ago but have repeatedly encountered problems with the programs freezing or not opening properly. It's a Student and Teacher Licensing version so an academic ID is required but I am

    I installed Adobe CS6 Design & Web Premium on my Mac Book Pro a few years ago but have repeatedly encountered problems with the programs freezing or not opening properly. It's a Student and Teacher Licensing version so an academic ID is required but I am no longer a student--can I still reinstall it since my academic ID may no longer be valid?

    Erin Elzo as long as the serial number has been registered under your account at http://www.adobe.com/ then you should be able to continue to utilize the software.  You can find details on how to locate your registered serial number at Find your serial number quickly - http://helpx.adobe.com/x-productkb/global/find-serial-number.html.

  • Problem with various programs freezing/not working

    I'm not sure if it's a problem with the computer itself or with the software, but perhaps someone on here can help me.
    For about a week now I've been having problems with various programs on my iMac. These programs include iTunes, Force Quit, Shut Down, Safari, Finder, and certain functions on the keyboard.
    The problems I've been having revolve around the programs freezing up when you either first try to operate them or after opening the programs and trying to click on things within the program itself.
    A good example: I haven't been able to use iTunes past the point of opening it up. Once I try to play a song, it freezes up and the rainbow wheel comes up and just keeps on spinning for eternity, or until I manually reboot the computer via the power button.
    The volume controls on the keyboard don't work at all. When I first boot up the computer, I can press a volume button and the symbol will appear on the screen, but the level won't move up or down and if you press it again after that disappears then it doesn't reappear.
    Force Quit and other applications tend to stay on the screen after you click the quit button or other end/cancel button and won't go away until iMac is rebooted manually by button on back.
    Safari gets stuck on occasion depending on which site I go to. It happened once when I went to MySpace.com, but only cos of one of the flash sites they were advertising. I went to the same site an hour later and it was fine since it was advertising something else. I have a job site I can't access because when I click on the 'jobs' link to look for a job, it gets 3/4 of the way through the upload and freezes.
    And right now I can't tell you which model I have since I can't access the 'About This Mac' info, other than to tell you it's the 20" with 320 GB with Leopard software, Mac OS X v10.5. It gets regular updates.
    So... if there's anyone who could help me, I would greatly appreciate it.
    Please note, I don't have Apple Care since I can't afford it yet, and I just bought my computer in October 2007. Thanks!

    Start with http://www.thexlab.com/faqs/multipleappsquit.html

  • A problem caused this program to stop interacting with Windows.  Problem signature:   Problem Event Name:     AppHangB1

    I have received the following error message with Adobe Acrobat Pro XI when working with Portfolio Files. I am operating Windows 7 Professional.
    Has anyone found a fix for this issue?
    Description:
      A problem caused this program to stop interacting with Windows.
    Problem signature:
      Problem Event Name: AppHangB1
      Application Name: Acrobat.exe
      Application Version: 11.0.10.32
      Application Timestamp: 547e97af
      Hang Signature: 8dc7
      Hang Type: 0
      OS Version: 6.1.7601.2.1.0.256.48
      Locale ID: 1033
      Additional Hang Signature 1: 8dc7ed9d7ff41b8cc5ee35b7294b45e9
      Additional Hang Signature 2: e6d0
      Additional Hang Signature 3: e6d001594873a6b1363ccd82616a4edf
      Additional Hang Signature 4: 8dc7
      Additional Hang Signature 5: 8dc7ed9d7ff41b8cc5ee35b7294b45e9
      Additional Hang Signature 6: e6d0
      Additional Hang Signature 7: e6d001594873a6b1363ccd82616a4edf
    Read our privacy statement online:
      http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409
    If the online privacy statement is not available, please read our privacy statement offline:
      C:\Windows\system32\en-US\erofflps.txt
    Please help
    John Sorkin [email protected]

    jamodi
    What computer operating system is your Premiere Elements 12 installed?
    How far into the opening of the program do you get? Do you get to the Expert or Quick workspaces?
    And, if so, how long can you stay there? What are you doing when you get this message, opening a new project
    or opening an existing one that has been saved/closed or just trying to launch the program from the desktop icon.
    Are you running the program Run As Administrator and from a User Account with Administrative Privileges?
    Do you have the McAfee antivirus program? Do you have the latest version of QuickTime installed on your computer
    with Premiere Elements 12?
    What video card/graphics card does your computer use? Have you verified at the web site of the manufacturer of the
    card that the video card/graphics card driver is up to date?
    If you do not know, check the Device Manager/Display
    Adapters to determine if you have 1 or 2 cards.
    Depending on your answers, the next step will be
    Uninstall the program the usual Control Panel way.
    (If you can get into the program long enough to go to Help Menu/Sign Out, please do that to deactivate the program
    before going to the uninstall in Control Panel area.)
    Do a run through with ccleaner (both the regular cleaner and registry cleaner parts) to get rid of leftovers from
    incomplete uninstalls and reinstalls.
    https://www.piriform.com/ccleaner
    Reinstall the program with the antivirus and firewalls disabled.
    (If you have McAfee as the antivirus, that may be problem, and we can take care of that without having to go
    through this uninstall/ccleaner/reinstall.)
    Let us start here and then decide what next.
    Thank you.
    ATR

  • HT201487 i have a problem with xcode programming app when i lunch to it it asking me to enter my password and when i enter it the program show that it is wrong but am 100% sure it's wright so please help me with this issue thanks

    i have a problem with xcode programming app when i lunch to it it asking me to enter my password and when i enter it the program show that it is wrong but am 100% sure it's wright so please help me with this issue thanks

    That's not very intuitive
    Check your mail server setup (mail>preferences>accounts>) choose your MobileMe account and select Outgoing Mail Server (SMTP) dropdown box, select Edit SMTP server list, verify that each instance of the me server has a password, if there are more than one and you only have one account then delete the one without a password.

  • I cannot connect my ipad with the program of yamaha stagemix, is urgent that they solve this problem to me, buys ipad esfecificamente to use that application

    I cannot connect my ipad with the program of yamaha stagemix, is urgent that they solve this problem to me, buys ipad esfecificamente to use that application, has somebody been able it to solve?

    I cannot connect my ipad with the program of yamaha stagemix, is urgent that they solve this problem to me, buys ipad esfecificamente to use that application, has somebody been able it to solve?

  • I have a problem with the program Lightroom on Mac

    Hello!
    I have a problem with the program Lightroom on Mac
    Today set a new Lightroom CC 2015 through Creative Cloud but it does not run , I do not know what is the reason
    Please help resolve this issue as soon as possible , I can not continue its work
    Thank you for attention

    Hi Musik
    Take a look at this link and see if it helps:
    Lightroom doesn't launch or quits automatically after splash screen
    Pattie

  • Hello! Help me please, I have a problem with the program occurred after the upgrade to version 3. 6. 16, namely by pressing the button 'Open a new tab' nothing happens. ?

    Hello! Help me please, I have a problem with the program occurred after the upgrade to version 3. 6. 16, namely by pressing the button 'Open a new tab' nothing happens. ?

    This issue can be caused by the Ask<i></i>.com toolbar (Tools > Add-ons > Extensions)
    See:
    * [[Troubleshooting extensions and themes]]

  • Problems with audio programs after update to Maverics

    After updating to Mavericks on my Mac Mini there is problem with audio programs. I cant open iTunes at all, I click it and it disappear in a second. Also when I press play in GuitarPro 6 or in Vox player nothing happens. It just wont start playing. I really dont like this update.. and my mac is slower now. I cant downgrade my system back so this is really frustrating to me as a musician.

    I have the same problem with my Conceptronic CH3HNAS and have found a free and easy solution.
    Since a lot of people report they are able to access their NAS using apps other than the Finder itself, I decided to look for a free Lion-compatible File Manager to use instead of Finder.
    I have found muCommander ( http://www.mucommander.com/ ) and indeed it works fine. All I did was click on the button highlighted bellow, go to "bonjour services" and select my NAS. It prompted me for my username and password and voilá, it works fine.
    I keep Finder for everyday use and just load muCommander when I want to access the NAS. At least now I don't have to start my WinXP VM anymore just to access it.
    Hope it helps you.

  • Is anyone having problems with the program MacKeeper not runing a full scan in Mavericks?

    Is anyone having problems with the program MacKeeper not runing a full scan in Mavericks?

    Do not install MacKeeper (and how to uninstall it if you have):
    https://discussions.apple.com/docs/DOC-6221
    by Klaus1
    (Please note that references to the original developers, Zeobit, also now refer to Kromtech Alliance Corp, who acquired MacKeeper and PCKeeper from ZeoBit LLC in early 2013.
    And Here  >  Beware MacKeeper
    In General 3rd Party AV Software is Not Required as Mac OS X tends to look after itself.
    Read Here  > https://discussions.apple.com/thread/4545776?tstart=0
    See Here  >  Antivirus Discussion

Maybe you are looking for

  • Zen Vision:M 30GB Keeps Crash

    I've had my 30GB Vision:M for about 8 months now, and recently, it's been starting to crash a lot. Often, it will not start up and when it does, I'm often greeted with a "hardware error" or "rebuilding." Usually, it will seem to be working fine, but

  • HRMD_A status 53 with NO ACTUAL DATA TO SAP t-code PA30

    Hi, We are trying to load the HR minimaster reocord from a flat data file to R/3. I have created an iDoc through WE19 for message type hrmd_a and basic type hrmd_a06. Populated infotypes for 0000, 0001, 0002 and 0003. idoc created with status 53 (gre

  • How to specify alias for local filesystem in JBOSS

    hi, I want to specify alias for localfilesystem in JBOSS. I know how to do it in Apache_2.0.52-Openssl_0.9.7e-Win32. In this we can specify alias as, Alias /icons/ "C:/Program Files/Apache_2.0.52-Openssl_0.9.7e-Win32/icons/" in httpd.conf and futher

  • Mac Mini emitting a constant clicking soud,

    I have a Mac Mini that started exhibiting the following problem. From the moment I turn it on I hear a clicking sound coming from the machine. It is rapid and constant. I did all of the tests, hardware test, boot into single user mode and did a fsck

  • HELP! Macbook Crashing-Safari Problems

    Every time I have been using Safari it has been causing problems. A few days ago, Safari kept closing unexpectedly. The little "Safari has unexpectedly closed..." window would pop up and I would send the report to apple and reopen Safari. But then la