Solved

drawing many rectangles at once.

Posted on 2004-04-14
4
350 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 45 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

631 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