Help with assigment asap!!!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Help with assigment asap!!!

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Help with assigment asap!!!

    So I have this assignment I have been working on for a class, but having trouble trying to duplicate the shapes within the boundaries I have set. The code is below for what I already have and a picture for what I need. I have the circles and shapes down, but just need a little insight on how to repeat them on the second row like the picture. Any help would be the best!!!

    WHAT I NEED:
    Name:  assignment.jpg
Views: 113
Size:  60.5 KB


    WHAT I HAVE:


    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);

    }

    }

    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Help with assigment asap!!!

    Surely you should have a class that extends JComponent that draws the shape and you create an instances of it with certain parameters like num circles. Then you add these components to a JPanel. You may also want to add the square grid groupings to their own JPanel with a border and background colour set and a GridLayout and add this JPanel to your root panel.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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