Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Adding an array of shapes to a GUI window

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding an array of shapes to a GUI window

    I have to do the following steps:
    1) Project6 class will have to extend the JFrame class
    2) Project6 constructor will have to set up the GUI window.
    3) A new abstract method: public void display(Graphics g); should be added to the base and derived classes
    4) A custom JPanel must be set up with a paintComponent method (see project 3)
    5) The new display(Graphics g) method will have to draw the shapes on the GUI window and be called from a loop in the paintComponent method

    I've done pretty much everything and the GUI window will pop up with the title "Shapes" and the size/location that I've specified. Except, I cannot get the array of shapes to show in the GUI window.

    Here is the code:

    package project6;

    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JFrame;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    public class Project6 extends JFrame{

    private static Shape [] thearray = new Shape[100]; // 100 Shapes, circle's, tri's and rects
    static MyPanel panel;

    public static void main (String [] args) {

    Project6 tpo = new Project6();
    tpo.run();
    } // end of main

    public void run () {
    int count = 0;

    // ------------------------ Fill the array section ----------------------

    thearray[count++] = new Circle(-4, 20, 40);
    thearray[count++] = new Circle(900, 120, 40);
    thearray[count++] = new Circle(220, 220, 40);
    thearray[count++] = new Triangle(70, 70, 20, 30);
    thearray[count++] = new Triangle(170, 170, 20, 30);
    thearray[count++] = new Triangle(270, 270, 20, 30);

    // ------------------------------ array fill done ------------------------

    //--------------------------- loop through the array --------------------

    for (int i = 0; i < count; i ++ ) { // loop through all objects in the array
    thearray[i].display(); // don’t care what kind of object, display it
    } // end for loop

    int offset = 0;
    double totalarea = 0.0;
    while (thearray[offset] != null) { // loop through all objects in the array
    totalarea = totalarea + thearray[offset].area(); // get area for this object
    offset++;
    } // end while loop
    System.out.println("The total area for " + offset + " Shape objects is " + totalarea);

    } // end of run

    //Project6 constructor w/out parameters to set up new JFrame
    public Project6(){
    JFrame frame= new JFrame("Shapes");
    frame.setSize(900, 700);
    frame.setBackground (Color.WHITE);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }

    public static class MyPanel extends JPanel {
    public static JPanel showJPanel(Graphics g){
    panel=new MyPanel();
    return panel;
    }
    @Override
    public void paintComponent (Graphics g) {
    super.paintComponent(g);
    for (int i = 0; i < thearray.length && thearray[i] !=null; i ++ )
    thearray[i].display();
    } // end of paintComponent
    } // end of MyPanel


    } // end of class Project6


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Adding an array of shapes to a GUI window

    If you want help, you'll have to provide an SSCCE that demonstrates the problem. Don't forget the highlight tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding an array of shapes to a GUI window

    how bout this:

    public class Project6 extends JFrame{

    private static Shape [] thearray = new Shape[100]; // 100 Shapes, circle's, tri's and rects
    static MyPanel panel;

    public static JPanel showJPanel(Graphics g){
    panel=new MyPanel();
    return panel;
    }

    public Project6(){
    JFrame frame= new JFrame("Shapes");
    frame.setSize(900, 700);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }
    public static class MyPanel extends JPanel {
    @Override
    public void paintComponent (Graphics g) {
    super.paintComponent(g);
    for (int i = 0; i < thearray.length && thearray[i] !=null; i ++ ) {
    thearray[i].display(g);
    }
    }

    }

    public void run () {
    int count = 0;

    // ------------------------ Fill the array section ----------------------

    thearray[count++] = new Circle(20, 20, 40);
    thearray[count++] = new Triangle(70, 70, 20, 30);
    thearray[count++] = new Rectangle(150, 150, 40, 40);

    // ------------------------------ array fill done ------------------------

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding an array of shapes to a GUI window

    never mind got it working

Similar Threads

  1. Adding image to my simple window
    By TP-Oreilly in forum AWT / Java Swing
    Replies: 6
    Last Post: December 8th, 2011, 03:50 PM
  2. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 2
    Last Post: September 15th, 2011, 07:11 AM
  3. Replies: 1
    Last Post: July 20th, 2011, 08:03 AM
  4. [SOLVED] adding to a multidimensional array.
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 27th, 2011, 10:09 AM
  5. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 3
    Last Post: January 5th, 2010, 06:19 PM