Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Trying to build new shape program

  1. #1
    JavaChipMocha is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Trying to build new shape program

    I have already written some code to produce the picture below. I just need some help duplicating the shapes on the second line of the picture. If you could help me develop some simple code to produce the output would be extremely appreciative!!

    OUTPUT I NEED:

    Trying to build new shape program-assignment.png

    OUTPUT I HAVE:

    Trying to build new shape program-output-have.png

    THIS IS MY CODE:

    Java Code:
    import java.awt.*;
    
    public class Illusion {
    public static void main(String[] args) {
    DrawingPanel panel = new DrawingPanel (500, 400);
    panel.setBackground(Color.GRAY);
    Graphics g = panel.getGraphics();
    drawBasic(g, 0, 0, 3, 90);
    drawBasic(g, 120, 10, 3, 90);
    drawBasic(g, 250, 50, 5, 90);
    drawGrid(g, 10, 120, 10, 100, 2);
    drawGrid(g, 350, 20, 5, 40, 3);
    drawGrid(g, 230, 160, 5, 50, 4);
    
    }
    public static void drawBasic(Graphics g, int x, int y,
    int circles, int size) {
    int gap=size/(2*circles);
    g.setColor(Color.RED);
    g.fillOval(x, y, size, size);
    g.setColor(Color.BLACK);
    for (int i=0; i<circles; i++) {
    g.drawOval(x+(i*gap), y+(i*gap), size-(2*i*gap), size-(2*i*gap));
    }
    
    Polygon diamond = new Polygon();
    diamond.addPoint(x+(size/2), y);
    diamond.addPoint(x, y+(size/2));
    diamond.addPoint(x+(size/2), y+size);
    diamond.addPoint(x+size, y+(size/2));
    g.drawPolygon(diamond);
    
    }
    public static void drawGrid(Graphics g, int x, int y, int circles, int size, int rows){
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(x, y, size*rows, size*rows);
    g.setColor(Color.RED);
    g.drawRect(x, y, size*rows, size*rows);
    for(int i=0; i<rows; i++) {
    drawBasic(g, x+(i*size), y, circles, size);
    
    }
    
    }
    
    }
    Thank you so much for your help.
    Last edited by Fubarable; 10-20-2012 at 12:37 AM. Reason: Post toned down a bit. Code tags added.

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

    Default Re: Trying to build new shape program

    Moderator edit: code tags added to posted code, extra exclamation marks removed, all references to ASAP removed.

    To the original poster, please calm down. Too many exclamation marks and urgent requests will usually have the opposite effect here. Yes your post is important, but please realize that it is no more important than any other question on this site.
    DarrylBurke likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trying to build new shape program

    It looks to me as if you're missing some for loops that would allow you to create several rows of your images. We're also not seeing a bit of your code including your DrawingPanel class. Also, in general you should avoid obtaining a Graphics object by calling getGraphics() on a GUI component since the Graphics object obtained won't persist. To see what I mean, try minimizing and then restoring your GUI. Do any of your graphics disappear after you've done this?

  4. #4
    JavaChipMocha is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Trying to build new shape program

    I am using jgrasp for the programming and I believe you have to call for graphics, but I could be wrong. Also, I am extreme intermediate at java and it took me awhile to write this code, but now I am stuck with trying the same but duplicating it.

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

    Default Re: Trying to build new shape program

    Quote Originally Posted by JavaChipMocha View Post
    I am using jgrasp for the programming and I believe you have to call for graphics, but I could be wrong.
    And so could I as I'm not very familiar with JGrasp. Unfortunately neither are many of us here since it really isn't used much outside of intro to Java courses.

    Also, I am extreme intermediate at java and it took me awhile to write this code, but now I am stuck with trying the same but duplicating it.
    No one will give you the code of course, and since you're using JGrasp, I doubt that they even could. But just glancing at your images, it looks like you're using a for loop to create a row of each image (I'm guessing here), but that you may need to use nested for loops to create multiple rows of images, one on top of the other.

  6. #6
    lbarowski is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Trying to build new shape program

    Quote Originally Posted by JavaChipMocha View Post
    I am using jgrasp for the programming and I believe you have to call for graphics, but I could be wrong. Also, I am extreme intermediate at java and it took me awhile to write this code, but now I am stuck with trying the same but duplicating it.
    You should be doing your painting by overriding DrawingPanel.paintComponent(Graphics)
    (or just implementing it in DrawingPanel, if DrawingPanel is your own class).

    Also, technically you shouldn't create a DrawingPanel outside the AWT event
    dispatch thread, which means you should create it within a SwingUtilites.invokeLater().
    This rarely causes a problem for simple examples though, and your instructor would
    probably question you about why you did it. Certainly you shouldn't do other things,
    like obtaining a Graphics and using it, outside the EDT.

Similar Threads

  1. Need help ASAP on a math program
    By Swiper in forum New To Java
    Replies: 11
    Last Post: 08-13-2011, 07:16 AM
  2. Help with a PolyGrapher Program ASAP
    By acole5 in forum New To Java
    Replies: 6
    Last Post: 06-15-2011, 04:35 AM
  3. HELP with a plotter program ASAP!
    By acole5 in forum New To Java
    Replies: 3
    Last Post: 06-03-2011, 01:16 AM
  4. Assistance needed ASAP: Postpix program
    By Debonairj in forum New To Java
    Replies: 18
    Last Post: 07-27-2010, 02:37 PM
  5. Need help ASAP with Payroll Program Part 2
    By arrech326 in forum New To Java
    Replies: 10
    Last Post: 11-17-2009, 11:17 PM

Posting Permissions

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