Solved

drawing many rectangles at once.

Posted on 2004-04-14
4
343 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
Comment Utility
Maybe this link can help: http://www.javaalmanac.com/egs/java.awt/pkg.html#Shapes
Browse around.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
Comment Utility
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
Comment Utility
msberth,

Thanks for the points and grade. :)

Delphi3

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now