Drawing a Box with many rectangles inside

I've been trying to make an applet that has 100 equally defined rectangles going across. They will have different shades going from light to dark. This is what I have gotten so far:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Graphics2D.*;
import java.awt.Rectangle;
import java.awt.BasicStroke;
import java.awt.Color;
public class Flags3 extends Applet
     public Flags3()
     public void paint(Graphics g)
          int w = getWidth();
          int h = getHeight();
          int x = w-w;
          int y = h-h;
          Graphics2D g2 = (Graphics2D)g;
          System.out.println("Width:"+getWidth()+" Height: "+getHeight());
int i;
int grayy = 250;
          for ( i=1; i<100; i++)
                    Color newGrey = new Color(0,0,0,grayy);
                    g2.setColor(newGrey);
                    Rectangle bar = new Rectangle (x,(h/100)*i, w, (h/100)*i);
                    g2.fill(bar);
                    grayy = grayy - 6;
}I want it to look like this but with 100 rectangles instead of the 5.
http://img339.imageshack.us/my.php?image=boxxi9.jpg
Why does the rgb values have 4 integers instead of 3? Also can anyone recommend a good java book for beginners.
Thanks for the help.
Message was edited by:
ButcherBay
Message was edited by:
ButcherBay

public class Flags3 extends JFrame {
    public Flags3() {
        setContentPane( new JPanel() {
            public void paintComponent(Graphics g) {
                int w = getWidth();
                int h = getHeight();
                int x = 0;
                int y = 0;
                System.out.println("Width:"+getWidth()+" Height: "+getHeight());
                for (int i=0; i<100; i++) {
                    Color newGrey = Color.getHSBColor(150, 0, i/100.0f);
                    g.setColor(newGrey);
                    g.fillRect(0, (h/100)*i, w, (h/100));
        Dimension size = new Dimension(800, 600);
        getContentPane().setPreferredSize(size);
        getContentPane().setMinimumSize(size);
        getContentPane().setMaximumSize(size);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    public static void main(String[] args) {
        new Flags3();
}I wrote this mainly to make sure I wasn't giving you bad info, but I also improved your paint method a little bit. It's a JFrame instead of a JApplet, so you'll have to make some changes, but it gives the full range of black to white (although it doesn't include full brightness white, the B value only goes to .99).

Similar Messages

  • Drawing a box with now fill and an outline

    I know this is simple, but I it's driving me crazy. Using the rectangle tool how do you draw a box with: an outline and no fill, and an outline with a different color fill. thanks

    public class Flags3 extends JFrame {
        public Flags3() {
            setContentPane( new JPanel() {
                public void paintComponent(Graphics g) {
                    int w = getWidth();
                    int h = getHeight();
                    int x = 0;
                    int y = 0;
                    System.out.println("Width:"+getWidth()+" Height: "+getHeight());
                    for (int i=0; i<100; i++) {
                        Color newGrey = Color.getHSBColor(150, 0, i/100.0f);
                        g.setColor(newGrey);
                        g.fillRect(0, (h/100)*i, w, (h/100));
            Dimension size = new Dimension(800, 600);
            getContentPane().setPreferredSize(size);
            getContentPane().setMinimumSize(size);
            getContentPane().setMaximumSize(size);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        public static void main(String[] args) {
            new Flags3();
    }I wrote this mainly to make sure I wasn't giving you bad info, but I also improved your paint method a little bit. It's a JFrame instead of a JApplet, so you'll have to make some changes, but it gives the full range of black to white (although it doesn't include full brightness white, the B value only goes to .99).

  • Suddenly I see the usual blue box with question mark inside mail messages instead of pix

    Suddenly I see the usual blue box with question mark inside mail messages instead of pix.
    That did not happen before.
    It happened only on certain pages with Safari, like the google news feed which did not display pix.
    However, this new mail problem is really disturbing.
    I checked Little Snitch and port 80 is open.
    I checked java pref.
    I am at a loss.
    PLEASE HELP!

    Suddenly I see the usual blue box with question mark inside mail messages instead of pix.
    That indicates that your Mail preferences are setup to not display remote images in email messages. You should check Mail->Preferences->Viewing and make surre the box to 'Display remote images in HTML messages' is checked.
    I checked java pref.
    Java has nothing to do with Mail, or displaying images in it or any browser.

  • What is causing a green box with white checkmark inside it, to appear in my images?

    What is causing a green box with white checkmark inside it, to appear in my images?

    Boot to safe mode using the shift key. But it probably will not work. You need to boot into recovery mode.
    Check this article out:
    http://support.apple.com/kb/ts1440

  • When i open .jpg picture in photoshop on MAC the the background thumbnail in the layers panel looks like a white box with the brush inside instead of picture thumbnail. how to fix it?

    when i open .jpg picture in photoshop on MAC the the background thumbnail in the layers panel looks like a white box with the brush inside instead of picture thumbnail. how to fix it?

    Very easy. That means you have a "no layer thumbnails" option selected.
    Go into the Layers Panel menu at the upper right of the Panel, choose Panel Options and you will see Thumbnail sizes to choose from.
    Select one, click OK.
    Gene

  • Draw a box with a stroke, then invert to a fill, causes resizing using the invisible stroke

    ID-CS3
    Draw a box with a stroke, then invert to a fill, causes resizing using the invisible stroke. Is there a way to fix and have the graphic resize using the fill size without redrawing the shape? Something like expand appearance?
    I'm not looking for a workaround or to redraw the shape. This problem description was used to present the simplest explanation to recreate the problem.

    I'm not sure I understand your question/problem, but if I make a box with an outside stroke and assign a stroke color, then... invert the stroke and fill, the stroke gets redrawn as a centered stroke --- it is no longer an outside stroke.
    This seems to me to be incorrect behavior, but it's more likely I just don't understand *why* this happens.
    Tad

  • Images on certain websites come up blank with little blue box with white "?" inside

    I own a MacBook Pro (MAC OS X 10.6.8) that I purchased in 2011. I have never had a single problem before, but here very recently youtube, my school (FSU) website, and a few other random websites will not load images. At all. They just show a little blue box with a white question mark inside. I have tried emptying the catch, resetting safari, nothing works. Help? I need to be able to see the images on my.fsu.edu it's really important!

    See this: <br />
    https://support.mozilla.com/en-US/kb/Images+or+animations+do+not+show

  • How to create an applet, with many squares inside?

    Hey,
    I'm having problems creating an applet. Heres the image I need to recreate. The drawing should scale, so that it reaches the full width and
    height of the applet.
    http://img503.imageshack.us/my.php?image=graphicsafx5.jpg
    I created a similar one (the code is below), but don't know how to modify it to create the image in the above link. Do I need a for loop? like for ( int i = 0; i < 10; ++i )
    import java.applet.Applet;
    import java.awt.*;
    public class AprilTest1 extends Applet
       public void paint(Graphics g)
            Graphics2D g2 = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            g2.setColor( Color.black );
            g2.fillRect( 0, 0, width, height);
            int pen_width = width/90 + 1;
            g2.setStroke(new BasicStroke(pen_width));
            g2.setColor( Color.white );
            g2.drawRect( width/4, height/4, width/2, height/2);
    }

    As CeciNEstPasUnProgrammeur said you do need a loop. You can probably make good use of the % operator inside your loop also.
    for(int i=0; i<NUMBER_OF_RECTANGLES; i++){
         if(i % 2 == 0) {
             //draw black rectangle
         } else {
             //draw white rectangle
    }Just think about what you need to do:
    -Start 1 rectangle at (0,0) and have its width and height as getWidth() and getHeight().
    -Pick an increment [this will be the 'width' of each rectangle]
    -Now for each rectangle you want to move your x and y points right and down by your increment. Moreover, you want to make your width and height smaller by your increment*2
    Essentially what I did is initialize 3 variables to 0 [say x, y, and z]. In your for loop use the % operator in an if-else block. If the loop is on an even number make a white rectangle, if the loop is on an odd number make a black rectangle. Set the x and y position to x+z and y+z respectivley. Set the height and width to getHeight()-(z*2) and getWidth()-(z*2) and then increment z by...lets say 10.
    This means on the first loop:
    x = 0
    y = 0
    width = getWidth()
    height = getHeight()
    Second loop:
    x = 10
    y = 10
    width = getWidth()-20 [10 to compensate for the shift, and 10 for the width]
    height = getHeight()-20
    Here is what I came up with - hope it helps.
    import java.applet.Applet;
    import java.awt.*;
    public class AprilTest1 extends Applet {
         private int x, y, z = 0;
         public void paint(Graphics g) {
              for(int i=0; i<10; i++){
                   if(i%2==1){
                      g.setColor( Color.white );
                      g.fillRect(x+z, y+z, getWidth()-(z*2), getHeight()-(z*2)); 
                   } else {
                      g.setColor( Color.black );
                      g.fillRect(x+z, y+z, getWidth()-(z*2), getHeight()-(z*2));
                   z += 10;
    }Any questions, feel free to ask :)

  • SAP-Script - Draw Table (Boxes) with variable number of lines

    Hello,
    I want to create a Table with Frames in a SAP-Script Form. First of all I don´t know the position of the table in the window main. It can be page 2 or 3 on top or in the middle. But i have to describe a position like
    POSITION YORIGIN '12.95' CM.  How can i position the table in a variable way?
    And i don´t know the number of lines of the table. How can I manage to draw the correct numbers of boxes as numbers of lines exist?
    Kind Regards
    Carsten Fuchs

    Hi carsten,
    1. Drawing GRID (just like excel, matrix)
       is very cumbersome in sapscript layout.
      (either dynamically, or hardcoded)
    2. Moreover, the final look,
       may or maynot appear as requried,.
      ie. exactly with proper alignment and grid lines.
    3. If the font size is required to change,
      then the dynamic position also changes.
    4. If the grid is supposed to go to the next page,
      again the calculation and logic becomes tougher.
    5. To use grid, its better to use smartform,
       which provided FINE CONTROL of the grid
      and its lines and width, height, style etc.
    6. In sapsctip, at most we can use
       UNDERLINE (_) and PIPE (|)
    regards,
    amit m.

  • How to draw a box with transparent background ?

    Hi there,
    I want to do an effect where after I display the text, there will be drawing effect where it draws a rectangle around the text ?
    During the days of Final Cut Express, I bought plugins from LiveType, it has effects to do this.
    But dow to I do this now in FCPX ?
    Thanks

    Thanks Jim,
    It is a good tips that LiveType still works on Maverick !
    Now I gotta ransack my store room to look out for the LiveType dvd/disc ...
    Motion ? I thought Motion is very difficult to learn ... more difficult than After Effects ?
    Ok, i better look out for some tutorials on Motion then ... Motion can integrate with FCPX ?
    I just checked, the Motion that I have is Ver 5.0.7 (got this as a birthday gift). Is this compatible with Maverick ?
    Thnaks
    Jim Wanamaker wrote:
    A couple of thoughts. Livetype still works in Mavericks although I don't know about your plugins. You would have to try them. Obviously you can not send a livetype project to FCP X, but you could design your title and export it and then import to FCP X. That should work although you couldn't make any changes in FCP X. A second thought is to use Motion if you have it. You can easily create a title in Motion to do what you want by opening a title template, add and format text. Then add a bezier shape for your rectangle and use the write on behavior. Very simple to do.

  • Safari 7.0 shows blue box with ? instead of actual picture

    Hi,
    I upgraded my macbook air late 2013 to mavericks and since then, when i open some site like Ebay i dont get to see pictures and i get blue box with ? mark inside.
    Any suggestions?!?!

    Please read this whole message before doing anything.
    This procedure is a test, not a solution. Don’t be disappointed when you find that nothing has changed after you complete it.
    Step 1
    The purpose of this step is to determine whether the problem is localized to your user account.
    Enable guest logins* and log in as Guest. Don't use the Safari-only “Guest User” login created by “Find My Mac.”
    While logged in as Guest, you won’t have access to any of your personal files or settings. Applications will behave as if you were running them for the first time. Don’t be alarmed by this; it’s normal. If you need any passwords or other personal data in order to complete the test, memorize, print, or write them down before you begin.
    Test while logged in as Guest. Same problem?
    After testing, log out of the guest account and, in your own account, disable it if you wish. Any files you created in the guest account will be deleted automatically when you log out of it.
    *Note: If you’ve activated “Find My Mac” or FileVault, then you can’t enable the Guest account. The “Guest User” login created by “Find My Mac” is not the same. Create a new account in which to test, and delete it, including its home folder, after testing.
    Step 2
    The purpose of this step is to determine whether the problem is caused by third-party system modifications that load automatically at startup or login, by a peripheral device, or by corruption of certain system caches.
    Disconnect all wired peripherals except those needed for the test, and remove all aftermarket expansion cards. Boot in safe mode and log in to the account with the problem. Note: If FileVault is enabled on some models, or if a firmware password is set, or if the boot volume is a software RAID, you can’t do this. Ask for further instructions.
    Safe mode is much slower to boot and run than normal, and some things won’t work at all, including sound output and Wi-Fi on certain models.  The next normal boot may also be somewhat slow.
    The login screen appears even if you usually log in automatically. You must know your login password in order to log in. If you’ve forgotten the password, you will need to reset it before you begin.
    Test while in safe mode. Same problem?
    After testing, reboot as usual (i.e., not in safe mode) and verify that you still have the problem. Post the results of steps 1 and 2.

  • What is the little red square with a E inside mean

    When I look at music to buy I notice a little Red Box with a E inside it, what does this mean please ( red box with a E inside ) , Thankyou

    I'm glad someone else asked this question. They should really find a new way to mark something as explicit because I was not sure if it meant explicit or edited. Yes, even thou I know some songs/albums have the word edited in the description.

  • Drawing a rectangle inside a label with a picture

    Hi everyone,
    i want to know how you can draw a rectangle inside a label that have an iconimage
    thanks in advance,
    kimos

    It depends if you want it only to show on the GUI or really change the image inside the IconImage.
    If you just want it to show in your GUI, extend JLabel and implement paintComponent to draw a rectangle on top.
         JLabel label = new JLabel() {
              protected void paintComponent(Graphics g) {
                   super.paintComponent(g);
                   g.drawRect(x, y, width, height);
         };

  • Box with rounded corners displays as rectangle in Web Browser

    I am using the box control (IBoxObject) in my Crystal Reports with rounded corners. However the box displays as a rectangle with square corners when viewed in a web browser. I have tried this in IE 7.0.5730.13, Firefox 3.0.3 and Chrome 0.2.149.30 all give the same problem.
    The interesting thing is that the boxes display correctly at design time and when exported to PDF, but they wonu2019t display correctly in IE/Firefox/Chrome. Here are the specific settings for the box:
                    CornerEllipseHieght = 375
                    CornerEllipseWidth = 375
                    Crystal Reports Version 10.5.3700.0
    I have also attached an image to try and clarify the problem.
    [Picture showing Box at design time, in a Web browser and when exported to PDF|http://i38.tinypic.com/10p6k1v.jpg]
    Thanks in advance for any help with this issue.
    -John

    Thank you Ted, this was my take on the problem but it is good to have a confirmation.
    There are CSS (HTML) solutions for creating boxes with rounded corners, such as [SpiffyBox|http://www.spiffybox.com/] and [CSS Rounded Box Generator|http://www.neuroticweb.com/recursos/css-rounded-box/index.php?idioma=en] and many more that can be found using [Google|http://www.google.com/search?hl=en&q=cssboxeswithroundedcorners&btnG=Google+Search&meta=].
    Do you know of any plans to support rounded corners in the future?
    Thanks for your help!
    John

  • Want to draw some rectangles inside a swing panel object

    HI,
    I want to draw some rectangles inside one JPanel object in swing. The JPanel object should also hold some buttons, labels, etc.. I can add simple buttons, label etc. easily to the panel object but how do I add graphics (like lines, rectangles) to it ??
    an immediate response would be highly appreciated!!

    I am not sure why you are talking about the rectangle.
    To repeat I fill in a bit of code (subject to compiler errors since I Do not have a machine where I can compile or test on at the moment):
      private class ColourCube extends JPanel 
         // The size of each rectangle with colour
         private static final int ROWS = 10;
         private static final int COLS = 10;
         // To store current selection
         private int selectedIndex[] = null;
         public ColourCube()
             addMouseListener( new MouseAdapter() {
                public void mouseReleased(MouseEvent me)
                   // Find the rectangle index by dividing
                   // x and y positions then set selectedIndex
                   // unless it is the same positions as previous
                   // then set it to be null
                   fireActionEvent();
         // needed listener methods for firing events
         public void addActionListener( ActionListener al)
            // add to list
         // remove etc....
         protected void fireActionEvent()
            // Construct action event
            // call actionPerformed( ae ) on all listeners
         public void paintComponent(Graphics g)
             // Depending on size and height of this component
             // calculate out colWidth and rowHeight
             // Loop and paint the rectangles in their different colours
             for(int i = 0; i < ROWS; i++)
                for(int j = 0; j < COLS; j++)
                   // set the colour to the graphics object here from the list of colours available
                   g.setColor( ... );
                   // Added a little bit extra to have some space between
                   // each rectangle
                   g.fillRect( i * rowHeight + 1, j * colWidth + 1, colWidth - 2, rowHeight - 2 );
             if( selectedIndex != null )
                // We have a selection, lets paint the selection
                g.setColor( Color.white ); // Selection colour
                // The index contains the row in index 0 and the col in index 1
                g.drawRect( selectedIndex[0] * rowHeight, selectedIndex[1] * colWidth, colWidth, rowHeight );
      }Lacking from the above component is lacking stuff like preferedSize.
    Now, all you do is instantiate the ColourChooser, add it to your applet iether by drag and drop if you have an WYSIWYG editor, or by hand if you are so inclined.
    Voila! A composite component appears, doing the stuff it needs to be doing.
    Regards,
    Peter Norell

Maybe you are looking for

  • Mark of the Web and Explorer stupidity

    So, we are working on a very clever little program that acts as a browser independent bookmark util, and strips Google's tracking data.  It's not a screen scraper.  It's a bookmark utility. It can also download images to local files. As a browser ind

  • List bullets and IE 11 not right

    Hi, I do not understand what is going on with my unordered list in IE 11. The bullets are on the wrong side of the image. They display correctly in FF and Chrome. But IE is giving me a fit. Here is the structure: <p><img src="/portals/0/Images/patien

  • Is this tough Landscape possible?

    hai friends Actually i have gone through Marketplace even though i couldnt understand Is this Landscape Possible :(Service Procurement using Extended Classic Scenario) Systems connected CCM,SUS,EBP,Strategic Sourcing Supplier directory replicated fro

  • What is meant by " Activation in Parallel"in DSO

    Hi, After loading the data into  DSO , when we R.C>  manage> rEQUEST TAB> select the request and click on activate .We get a screen with different options    Like...Activation in parallel,Generation of SID's,start..etc now my query is What are these

  • How to rotate .out ( stdout) log file in weblogic 9.2 (solaris/linux).

    Hi, Is there a way we can rotate .out log file. I have written a shell script which will backup the log file and dump .out file. #!/bin/bash FILE=/opt/bea10/user_projects/domains/wl102xdomain/servers/ManagedServer1/logs/ManagedServer1.log if cp $FILE