Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

drawing many rectangles at once.

Posted on 2004-04-14
4
Medium Priority
?
352 Views
Last Modified: 2008-02-26
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
Comment
Question by:msberth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10827347
Maybe this link can help: http://www.javaalmanac.com/egs/java.awt/pkg.html#Shapes
Browse around.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10830239
>> 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
 
LVL 4

Accepted Solution

by:
delphi3 earned 180 total points
ID: 10844707
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
 
LVL 4

Expert Comment

by:delphi3
ID: 10943882
msberth,

Thanks for the points and grade. :)

Delphi3

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question