Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

drawing many rectangles at once.

hi.
i need assistance with drawing a many rectangles 0f the same size  at diffrent positions. say i want to draw four rectangles to be displayed. all the rectangles should have some string in them. how do i go about that? are loops gonna be used? assistance with sample code will be appreciated
0
msberth
Asked:
msberth
  • 2
1 Solution
 
zzynxSoftware engineerCommented:
Maybe this link can help: http://www.javaalmanac.com/egs/java.awt/pkg.html#Shapes
Browse around.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> are loops gonna be used?

If you have only 4 to be displayed, you can do it without using a loop (using a loop would still be better). The advantage of using a loop is - you can modify the number of rectangles to be drawn at any point of time. Have a look at Java Almanac's examples.
0
 
delphi3Commented:
msberth,
Here is a sample written with the directions that were supplied by you.

Delphi3

/*
 Much of this was written by repeated testing
 of known "sample code"
 */
import java.awt.*;
import javax.swing.*;
/*
 <HTML>
 <HEAD>
 <TITLE>RandomRectangles TwoWay: a Applet or a Application</TITLE>
 </HEAD>
 <BODY>
 <H1>Show RandomRectangles Demo</H1>
 <APPLET CODE="RandomRectangles.class" WIDTH=450 HEIGHT=400>
 </APPLET>
 </BODY>
 </HTML>
 */  

public class RandomRectangles
  extends JApplet {
  // assignment of strings to array.
  String [] arryPhrase  = {"How are you?","Welcome to java","Just follow me", "Wondering"};
  // each phrase has a number of letters and spaces: just keep it 16 letters/spaces
  // or expand the fixed width of the rectangle
  int [] arryPhraseLength = {12*6,15*6,14*6,9*6} ;    
 
  public void paint(Graphics g) {
    g.setColor(Color.green);
    for( int rects = 1;rects < 5;rects++){        
      int rndmx = (int) (150 * Math.random() + 25);
      int rndmy = (int) (150 * Math.random() + 25);
      int w = 100;
      int h = 130;
      Color rndm= new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
      g.setColor(rndm);
      g.fillRect(rndmx,rndmy,w,h);
      g.setColor(Color.black);    
      g.drawString (arryPhrase [rects-1],((int)(rndmx+w/2)- ((arryPhraseLength[rects-1])/2)),(int)(rndmy + h /2));
    }  
  }
  //  main()called only when run as a stand-alone application
  public static void main(String args[])  {
    MyAppletFrame appletFrame= new MyAppletFrame ("My Rectangles");
    //The following makes a new instance of RandomRectangles
    RandomRectangles myNewRandomRectangles= new RandomRectangles();
    WinListen off = new WinListen();
    myNewRandomRectangles.init();
    myNewRandomRectangles.start();
    appletFrame.addWindowListener(off);
    appletFrame.add("Center",myNewRandomRectangles);
    appletFrame.setSize(450,400);
    appletFrame.show();
  }    
}
0
 
delphi3Commented:
msberth,

Thanks for the points and grade. :)

Delphi3

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now