Results 1 to 5 of 5
- 09-05-2008, 06:54 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 1
- Rep Power
- 0
I want to recreate the method drawRect with a nested for-loop
I have absolutely no idea how to start to create a method that will draw an unfilled rect. on the picture instantiated.
The constructor of the class eats the following parameters:
int width, int height
I have the Color class imported and I use the usual parameters:
int xStartingPt, int yStartingPt, int width, int height, Color color
Please help me get started.Last edited by chelseacortez; 09-05-2008 at 07:00 AM.
- 09-05-2008, 02:41 PM #2
Can you post the code you are working on to show where your problem is?
What does that mean?constructor of the class eats the following parameters:
A rectangle has lines on 4 sides. It's upper left corner would be at a point say x,y. Given that position, you can compute the x,y points for the other 3 corners and draw connecting lines.
- 09-05-2008, 03:25 PM #3
Java Code:import java.awt.*; import javax.swing.*; public class DrawRect extends JPanel { int width; int height; public DrawRect(int width, int height) { this.width = width; this.height = height; } protected void paintComponent(Graphics g) { super.paintComponent(g); int x = 50; int y = 25; drawRect(g, x, y); } private void drawRect(Graphics g, int x0, int y0) { for(int i = 0; i < 4; i++) { int x = x0 + i*25; int y = y0 + i*25; g.drawRect(x, y, width, height); } } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new DrawRect(125, 75)); f.setSize(300,300); f.setLocation(200,200); f.setVisible(true); } }
- 09-05-2008, 04:07 PM #4
You left off the color
What are the hardcoded(not parameterized) values: 25 & 50 used for?Last edited by Norm; 09-05-2008 at 04:11 PM.
- 09-05-2008, 04:47 PM #5
You left off the color
chelseacortez asked to help me get started.
I just wanted to offer a minimal suggestion.
What are the hardcoded(not parameterized) values: 25 & 50 used for?
Some values to send off to the drawRect method which will use them to determine the origin of the first (and subsequent) rectangle(s) that it draws.
Similar Threads
-
Nested(Inner) Classes
By Z.S.Tehrani in forum New To JavaReplies: 7Last Post: 08-13-2008, 10:54 AM -
nested for loop question
By javabob in forum New To JavaReplies: 3Last Post: 05-20-2008, 11:00 PM -
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08-06-2007, 04:51 PM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM -
Nested Tags JSP
By Marcus in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-25-2007, 05:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks