September 27th, 2012, 06:13 PM
Need help with java applet
Hi everyone! i need some help with a lab for my computer science class
so far i have
public class GraphicsLab02st extends Applet
public void paint(Graphics g)
int width = 980;
int height = 630;
int x1 = 10;
int y1 = 640;
int x2 = 990;
int y2 = 640;
for (x1 = 10; x1<= 980; x1 += 20)
y2 -= 10;
i have to make this shape in all four corners and i cant figure out how, ill attach an image of what its supposed to look like.
September 27th, 2012, 07:59 PM
Re: Need help with java applet
From what I can see, these are strait lines, with their ends sliding along the edges of the drawing area, right? Just divide the width and height of the drawing area by the same amount (number of steps needed to draw the shape in one corner), and then loop, changing the angle of the line ever so slightly, until the line has been rotated by 90deg. You could wrap this loop in a method, and use it's parameters to determine which corner should be painted.
For example, if you're drawing in the top-left corner, and you're starting, say, from the vertical position, then, as the loop progresses, and as the line slides, only one coordinate at each of the ends is changing:
Now, instead of hard-coding x1, y2, yMax, increments etc., you can modify the code, put it in a method, and use method parameters instead. For example, if xMax and yMax are known, and if you have a predetermined number of steps, you can calculate the increments. The method can look something like this: drawWeb(int end1Start, int end1Anchor, int end2Start, int end2Anchor, Graphics g), where start-parameters mark the starting (variable) coordinate components for each end, and anchor-parameters define the edges along which the ends are going to slide.
// Ends: (x1, y1) ------------ (x2, y2)
x1 = 0;
y2 = 0;
for (int i = 0; i < numSegments; ++i)
y1 = yMax - i * yIncrement; // move end1
x2 = i * xIncrement; // move end2
g.drawLine(x1, y1, x2, y2);
Then you call the method 4 times with different parameters, from within paint(), passing the graphics object as well.
That's the basic idea - you can, of course, vary it and adapt it to suit your needs.
Last edited by TheGreatCthulhu; September 27th, 2012 at 08:01 PM.
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.