Need help with java applet
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Need help with java applet

  1. #1
    Join Date
    Sep 2012
    Posts
    1

    Need help with java applet

    Hi everyone! i need some help with a lab for my computer science class
    so far i have




    import java.awt.*;
    import java.applet.*;

    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;
    g.drawRect(10,10,width,height);
    for (x1 = 10; x1<= 980; x1 += 20)
    {
    g.drawLine( x1,y1,x2,y2);
    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.Name:  applet.png
Views: 369
Size:  87.1 KB

  2. #2
    Join Date
    Jan 2010
    Posts
    1,099

    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:
    Code:
    // pseudocode
    // 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);
    }
    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.
    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 07:01 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center