Solved

drawing many rectangles at once.

Posted on 2004-04-14
4
346 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
  • 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 56
java stored proc example 9 44
spring jars download 1 35
Problem to Alipay 10 44
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

829 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