Results 1 to 2 of 2
  1. #1
    mp06677 is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default Grid Layout with paintComponent

    I can't seem to figure out how to get one copy of each of my paintComponent in each of the six grid squares. I can get 6 buttons to work but not 6 images. Any help would be greatly appreciated. Here is my code:

    FILE1:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author compuser
    */
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;

    public class Project6 extends JFrame {
    public Project6() {

    Container cp=this.getContentPane();
    cp.setBackground(Color.WHITE);
    cp.setLayout(new BorderLayout());
    YingYang md=new YingYang();
    BorderLayout bl =new BorderLayout();
    cp.setLayout(bl);
    JTextField text= new JTextField("Hello");
    cp.add(text, BorderLayout.SOUTH);
    Date date = new Date();
    text.setText(date.toString());
    this.setTitle("Project62");
    JPanel panel=new JPanel();
    cp.add(panel, BorderLayout.CENTER);
    panel.setLayout(new GridLayout(2, 3));
    for (int i=0; i<6; i++ ) {
    panel.add(md);
    }









    }
    public static void main(String[] args) {
    JFrame frame=new Project6();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setBounds(200, 100, 500, 400);
    frame.setVisible(true);
    }

    }


    FILE2:





    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author compuser
    */
    import javax.swing.*;
    import java.awt.*;
    public class YingYang extends JPanel {
    int a;
    int b;
    public YingYang() {
    a=300;
    b=200;
    }
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.WHITE);
    g.fillArc(100, 100, 100, 100, 90, -180);
    g.setColor(Color.BLACK);
    g.fillArc(100, 100, 100, 100, 90, 180);
    g.setColor(Color.BLACK);
    g.fillOval(125, 100, 50, 50);
    g.setColor(Color.WHITE);
    g.fillOval(125, 150, 50, 50);





















    }
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    you need to declare a new instance of YingYang inside the for loop

    Java Code:
     
    
    for (int i=0; i<6; i++ ) {
    md=new YingYang();
    panel.add(md);
    }
    also adjust the size of the JFrame to see them all properly.

Similar Threads

  1. paintComponent is not working
    By spazattack in forum New To Java
    Replies: 1
    Last Post: 12-18-2009, 03:48 AM
  2. Grid layout frames GUI
    By fritz1474 in forum AWT / Swing
    Replies: 1
    Last Post: 10-15-2008, 02:04 AM
  3. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 04:59 PM
  4. Drawing outside paintComponent()
    By DarkSide1 in forum Java 2D
    Replies: 2
    Last Post: 11-08-2007, 10:36 PM
  5. Help with Grid Layout
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:03 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
  •