• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JFrame How do i make multiple coloured rectangles using nested loops?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.awt.*;
import javax.swing.*;
public class DigitalMedia extends JFrame
{
private static final Dimension WindowSize = new Dimension(600,600);

public DigitalMedia() {

//Create and set up the window.
this.setTitle("Squarebes");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window, centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

int x = screensize.width/2 - WindowSize.width/2;
int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
}

public void paint(Graphics g)
{

for (int i = 0; i <= 800; i+= 10)
{
g.fillRect(10,35,20,50);
g.fillRect(10,60,20,50);
}
}


public static void main(String [ ] args) {
DigitalMedia w = new DigitalMedia();


}
}
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, you should consider extending JPanel instead of JFrame, and doing the drawing in the panel's paintComponent() method (be sure to call super.paintComponent(g) as its first statement).
Second, why are you drawing the same two rectangles 80 times each time the paint method is invoked?
 
Mike Gregory
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was hoping to draw the rectangle 80 time in a distance (maybe of 2) between each other. I cant tell why this method won't work. I have changed it so that i initialize a variable w=x+2 to move the width each time through the loop.

Thanks for the reply Fred
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean when you say "it won't work"? What did you expect to happen, and what actually happened?
Note that your rectangle height is 50, but you draw the second rectangle only 25 pixels below the top of the first one, so they intersect and will look like one larger rectangle.

You might try something like this (it is not really clear what you want to do. I note that your window size is much smaller than the space needed to draw these):
 
Mike Gregory
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will the loop you provided continue to print out rectangles?

Whats happening with mine is that it is only printing out the one triangle( i is printing out several but on the exact same location making it seem like just one)

Do you know how i could print out the rectangles to fill the whole window?
 
Mike Gregory
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The loop that you provide works. Thank you. It displays 2 rows would it be possible to this for columns too? so that the whole window is filled with squares?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Gregory wrote:. . . I have changed it so that i initialize a variable w=x+2 to move the width each time through the loop. . . .

…And what happened? If you draw the same rectangle without changing colours 80 times in different locations, will you see 80 rectangles or one? If you use fillXXX, you will only see one.

And welcome to the Ranch
 
Mike Gregory
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using fillRect. I was thought that this would fill the whole window wth rectangle. Using the the loop that you provided, it now prints two rows of black rectangles.
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This fills the window with rectangles, regardless of the window size:
 
Mike Gregory
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try it out thanks
 
Mike Gregory
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works perfectly! i'm going to use int red = 0 + ran.nextInt(255 - 0 + 1) to get the random colors. What do you think?
 
Mike Gregory
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally got it!!!
Thanks so much for helping
reply
    Bookmark Topic Watch Topic
  • New Topic