Results 1 to 4 of 4
  1. #1
    mike.x.cao is offline Member
    Join Date
    Jul 2014
    Posts
    1
    Rep Power
    0

    Default Can somebody help me with my Java program?

    Can somebody please tell me what's wrong with my Java program? When I open it using appletviewer, the applet opens but stays blank and an error message appears in Terminal.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DemoColor extends Applet
    
    {
    
        Font littleFont = new Font("Helvetica", Font.ITALIC, 6);
    
        public void paint(Graphics gr)
        
        {
    
            int r, g, b;
    
            int x = 0, y = 0;
    
            gr.setFont(littleFont);
    
            for(r = 255; r >= 0; r -= 20)
                for(g = 255; g >= 0; g -= 20)
                    for(b = 255; b >= 0; b -= 20)
    
            {
        
                Color variety = new Color(r, g, b);
                gr.setColor(variety);
                gr.drawString("X" ,x,y);
                x += 5;
                    if (x >= 400)
    
                    {
                        x = 0;
                        y += 10;
                    }
    
            }
    
        }
    
    }
    Error Message:

    Exception in thread "AWT-EventQueue-1" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Green Blue
    at java.awt.Color.testColorValueRange(Color.java:310)
    at java.awt.Color.<init>(Color.java:395)
    at java.awt.Color.<init>(Color.java:369)
    at DemoColor.paint(DemoColor.java:24)
    at sun.awt.RepaintArea.paintComponent(RepaintArea.jav a:264)
    at sun.awt.X11.XRepaintArea.paintComponent(XRepaintAr ea.java:73)
    at sun.awt.RepaintArea.paint(RepaintArea.java:240)
    at sun.awt.X11.XComponentPeer.handleEvent(XComponentP eer.java:591)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4937)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

    HTML file:

    <HTML>
    <APPLET CODE="DemoColor.class" WIDTH = 420 HEIGHT = 300>
    </APPLET>
    </HTML>

    Can somebody tell me what the mistake is? Thanks!
    Last edited by mike.x.cao; 07-29-2014 at 05:58 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: Can somebody help me with my Java program?

    I recommend you forget about the applet part and see what your color values are (i.e. print them out). Clearly there
    is a bug in your program as was explained by the exception. Your values appear to be okay but you never know.
    Also, what does you html look like?

    And when you resume your applet coding, use JApplet instead.

    Edit: Well, I just ran you applet and it worked (I ran it as an Applet in Eclipse).
    is the posted code the same as the code you ran?

    Regards,
    Jim
    Last edited by jim829; 07-29-2014 at 06:17 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,998
    Rep Power
    5

    Default Re: Can somebody help me with my Java program?

    Strangely enough when I run the offending code on the command prompt, no exception... Looking at the code it stays within the range of 0-255 so there should be no reason for that exception to happen.

    Are you really, really sure that the code you post here is the code that is actually executed?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Can somebody help me with my Java program?

    Quote Originally Posted by gimbal2 View Post
    Strangely enough when I run the offending code on the command prompt, no exception... Looking at the code it stays within the range of 0-255 so there should be no reason for that exception to happen.

    Are you really, really sure that the code you post here is the code that is actually executed?
    I was just about to ask the same thing...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  2. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  3. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •