Results 1 to 7 of 7
  1. #1
    GreenRock is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default Drawing staircases

    How would I draw a simple, side-view of a staircase on an applet?
    I have to use "for" statements to loop a drawing so it appears as a staircase.
    It has to start on the bottom left corner of the applet, and end in the upper right.

    No idea how to start.

    Thank you for your time.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What do you know so far? Can you draw lines? a rectangle? have you created applets before and drawn in them? do you use applets or japplets? Please give us something we can work with here.

  3. #3
    GreenRock is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    lines.
    Yes, I am familiar with applets, but I'm lost with this problem.
    I'm not familiar with japplets, but I could say that the applets I'm working with are graphic applets.
    Sorry for the oversight.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    // <applet code="StairApplet" width="400" height="400"></applet>
    // at the prompt>appletviewer StairApplet.java
    import java.awt.*;
    import javax.swing.*;
    
    public class StairApplet extends JApplet {
        public void paint(Graphics g) {
            int w = getWidth();
            int h = getHeight();
            g.setColor(Color.blue);
            g.drawRect(0,0,w-1,h-1);
            g.setColor(Color.red);
            int steps = 8;
            for(int i = 0; i < steps; i++) {
                int x1 = i*(w/steps);
                int x2 = (i+1)*(w/steps);
                int y1 = h-1 - i*(h/steps);
                int y2 = y1;
                g.drawLine(x1, y1, x2, y2);
            }
        }
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by hardwired View Post
    Java Code:
    // <applet code="StairApplet" width="400" height="400"></applet>
    // at the prompt>appletviewer StairApplet.java
    import java.awt.*;
    import javax.swing.*;
    
    public class StairApplet extends JApplet {
        public void paint(Graphics g) {
            int w = getWidth();
            int h = getHeight();
            g.setColor(Color.blue);
            g.drawRect(0,0,w-1,h-1);
            g.setColor(Color.red);
            int steps = 8;
            for(int i = 0; i < steps; i++) {
                int x1 = i*(w/steps);
                int x2 = (i+1)*(w/steps);
                int y1 = h-1 - i*(h/steps);
                int y2 = y1;
                g.drawLine(x1, y1, x2, y2);
            }
        }
    }
    I've always been taught to never draw directly on a JApplet, as you lose all benefits of Swing and can have side effects, but to instead draw in a JPanel's paintComponent method and then add the panel to the applet's contentPane.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    That is the rigorous way to do it. When people learn with applets sometimes they learn to draw in the Container paint method. For simple beginnings this is okay. When you add components things get messy and it is much easier to draw on a separate component. Container drawing can be complicated but sometimes it's just what's needed. Depends on what we're doing. Flexibility can be a good thing.

  7. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    for heavier programs it would really help when you take Panels and Frames in your consideration. It would be kinda foolish if you try to make a loop filled program for even a small movable program.

Similar Threads

  1. Drawing a grid
    By CrystalMoth in forum Java 2D
    Replies: 11
    Last Post: 01-10-2010, 05:07 AM
  2. Drawing Applet
    By Urgle in forum New To Java
    Replies: 6
    Last Post: 11-19-2008, 08:38 AM
  3. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 12:26 PM
  4. Problem with 3D drawing.
    By Supamagier in forum Advanced Java
    Replies: 0
    Last Post: 08-31-2008, 12:39 PM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM

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
  •