Object Class extends JPanel, Won't Appear

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 487 Views - Last Post: 20 January 2013 - 03:21 PM Rate Topic: -----

#1 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Object Class extends JPanel, Won't Appear

Posted 18 January 2013 - 05:25 PM

I'm making my own object class Diamond for a school project which extends JPanel. I'm not getting any compile-time or run-time errors, but my object won't appear in the JFrame. Any help?

Here's my Diamond class:

import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Container;

public class Diamond extends javax.swing.JPanel
{
    JPanel diamond;
    
    JPanel a1;
    JPanel a2;
    JPanel a3;
    JPanel a4;
    JPanel a5;
    JPanel a6;
    JPanel a7;
    JPanel a8;
    JPanel a9;
    JPanel a10;
    JPanel a11;
    JPanel b1;
    JPanel b2;
    JPanel b3;
    JPanel b4;
    JPanel b5;
    JPanel b6;
    JPanel b7;
    JPanel b8;
    JPanel b9;
    JPanel b10;
    JPanel b11;
    JPanel c1;
    JPanel c2;
    JPanel c3;
    JPanel c4;
    JPanel c5;
    JPanel c6;
    JPanel c7;
    JPanel c8;
    JPanel c9;
    JPanel c10;
    JPanel c11;
    JPanel d1;
    JPanel d2;
    JPanel d3;
    JPanel d4;
    JPanel d5;
    JPanel d6;
    JPanel d7;
    JPanel d8;
    JPanel d9;
    JPanel d10;
    JPanel d11;
    JPanel e1;
    JPanel e2;
    JPanel e3;
    JPanel e4;
    JPanel e5;
    JPanel e6;
    JPanel e7;
    JPanel e8;
    JPanel e9;
    JPanel e10;
    JPanel e11;
    JPanel f1;
    JPanel f2;
    JPanel f3;
    JPanel f4;
    JPanel f5;
    JPanel f6;
    JPanel f7;
    JPanel f8;
    JPanel f9;
    JPanel f10;
    JPanel f11;
    JPanel g1;
    JPanel g2;
    JPanel g3;
    JPanel g4;
    JPanel g5;
    JPanel g6;
    JPanel g7;
    JPanel g8;
    JPanel g9;
    JPanel g10;
    JPanel g11;
    JPanel h1;
    JPanel h2;
    JPanel h3;
    JPanel h4;
    JPanel h5;
    JPanel h6;
    JPanel h7;
    JPanel h8;
    JPanel h9;
    JPanel h10;
    JPanel h11;
    JPanel i1;
    JPanel i2;
    JPanel i3;
    JPanel i4;
    JPanel i5;
    JPanel i6;
    JPanel i7;
    JPanel i8;
    JPanel i9;
    JPanel i10;
    JPanel i11;
    JPanel j1;
    JPanel j2;
    JPanel j3;
    JPanel j4;
    JPanel j5;
    JPanel j6;
    JPanel j7;
    JPanel j8;
    JPanel j9;
    JPanel j10;
    JPanel j11;
    JPanel k1;
    JPanel k2;
    JPanel k3;
    JPanel k4;
    JPanel k5;
    JPanel k6;
    JPanel k7;
    JPanel k8;
    JPanel k9;
    JPanel k10;
    JPanel k11;
    
    public void makeDiamond()
    {
        diamond = new JPanel(new GridLayout(11,11));
        
        a1 = new JPanel();
        a2 = new JPanel();
        a3 = new JPanel();
        a4 = new JPanel();
        a5 = new JPanel();
        a6 = new JPanel();
        a7 = new JPanel();
        a8 = new JPanel();
        a9 = new JPanel();
        a10 = new JPanel();
        a11 = new JPanel();
        b1 = new JPanel();
        b2 = new JPanel();
        b3 = new JPanel();
        b4 = new JPanel();
        b5 = new JPanel();
        b6 = new JPanel();
        b7 = new JPanel();
        b8 = new JPanel();
        b9 = new JPanel();
        b10 = new JPanel();
        b11 = new JPanel();
        c1 = new JPanel();
        c2 = new JPanel();
        c3 = new JPanel();
        c4 = new JPanel();
        c5 = new JPanel();
        c6 = new JPanel();
        c7 = new JPanel();
        c8 = new JPanel();
        c9 = new JPanel();
        c10 = new JPanel();
        c11 = new JPanel();
        d1 = new JPanel();
        d2 = new JPanel();
        d3 = new JPanel();
        d4 = new JPanel();
        d5 = new JPanel();
        d6 = new JPanel();
        d7 = new JPanel();
        d8 = new JPanel();
        d9 = new JPanel();
        d10 = new JPanel();
        d11 = new JPanel();
        e1 = new JPanel();
        e2 = new JPanel();
        e3 = new JPanel();
        e4 = new JPanel();
        e5 = new JPanel();
        e6 = new JPanel();
        e7 = new JPanel();
        e8 = new JPanel();
        e9 = new JPanel();
        e10 = new JPanel();
        e11 = new JPanel();
        f1 = new JPanel();
        f2 = new JPanel();
        f3 = new JPanel();
        f4 = new JPanel();
        f5 = new JPanel();
        f6 = new JPanel();
        f7 = new JPanel();
        f8 = new JPanel();
        f9 = new JPanel();
        f10 = new JPanel();
        f11 = new JPanel();
        g1 = new JPanel();
        g2 = new JPanel();
        g3 = new JPanel();
        g4 = new JPanel();
        g5 = new JPanel();
        g6 = new JPanel();
        g7 = new JPanel();
        g8 = new JPanel();
        g9 = new JPanel();
        g10 = new JPanel();
        g11 = new JPanel();
        h1 = new JPanel();
        h2 = new JPanel();
        h3 = new JPanel();
        h4 = new JPanel();
        h5 = new JPanel();
        h6 = new JPanel();
        h7 = new JPanel();
        h8 = new JPanel();
        h9 = new JPanel();
        h10 = new JPanel();
        h11 = new JPanel();
        i1 = new JPanel();
        i2 = new JPanel();
        i3 = new JPanel();
        i4 = new JPanel();
        i5 = new JPanel();
        i6 = new JPanel();
        i7 = new JPanel();
        i8 = new JPanel();
        i9 = new JPanel();
        i10 = new JPanel();
        i11 = new JPanel();
        j1 = new JPanel();
        j2 = new JPanel();
        j3 = new JPanel();
        j4 = new JPanel();
        j5 = new JPanel();
        j6 = new JPanel();
        j7 = new JPanel();
        j8 = new JPanel();
        j9 = new JPanel();
        j10 = new JPanel();
        j11 = new JPanel();
        k1 = new JPanel();
        k2 = new JPanel();
        k3 = new JPanel();
        k4 = new JPanel();
        k5 = new JPanel();
        k6 = new JPanel();
        k7 = new JPanel();
        k8 = new JPanel();
        k9 = new JPanel();
        k10 = new JPanel();
        k11 = new JPanel();
        
        diamond.add(a1);
        diamond.add(a2);
        diamond.add(a3);
        diamond.add(a4);
        diamond.add(a5);
        diamond.add(a6);
        diamond.add(a7);
        diamond.add(a8);
        diamond.add(a9);
        diamond.add(a10);
        diamond.add(a11);
        diamond.add(b1);
        diamond.add(b2);
        diamond.add(b3);
        diamond.add(b4);
        diamond.add(b5);
        diamond.add(b6);
        diamond.add(b7);
        diamond.add(b8);
        diamond.add(b9);
        diamond.add(b10);
        diamond.add(b11);
        diamond.add(c1);
        diamond.add(c2);
        diamond.add(c3);
        diamond.add(c4);
        diamond.add(c5);
        diamond.add(c6);
        diamond.add(c7);
        diamond.add(c8);
        diamond.add(c9);
        diamond.add(c10);
        diamond.add(c11);
        diamond.add(d1);
        diamond.add(d2);
        diamond.add(d3);
        diamond.add(d4);
        diamond.add(d5);
        diamond.add(d6);
        diamond.add(d7);
        diamond.add(d8);
        diamond.add(d9);
        diamond.add(d10);
        diamond.add(d11);
        diamond.add(e1);
        diamond.add(e2);
        diamond.add(e3);
        diamond.add(e4);
        diamond.add(e5);
        diamond.add(e6);
        diamond.add(e7);
        diamond.add(e8);
        diamond.add(e9);
        diamond.add(e10);
        diamond.add(e11);
        diamond.add(f1);
        diamond.add(f2);
        diamond.add(f3);
        diamond.add(f4);
        diamond.add(f5);
        diamond.add(f6);
        diamond.add(f7);
        diamond.add(f8);
        diamond.add(f9);
        diamond.add(f10);
        diamond.add(f11);
        diamond.add(g1);
        diamond.add(g2);
        diamond.add(g3);
        diamond.add(g4);
        diamond.add(g5);
        diamond.add(g6);
        diamond.add(g7);
        diamond.add(g8);
        diamond.add(g9);
        diamond.add(g10);
        diamond.add(g11);
        diamond.add(h1);
        diamond.add(h2);
        diamond.add(h3);
        diamond.add(h4);
        diamond.add(h5);
        diamond.add(h6);
        diamond.add(h7);
        diamond.add(h8);
        diamond.add(h9);
        diamond.add(h10);
        diamond.add(h11);
        diamond.add(i1);
        diamond.add(i2);
        diamond.add(i3);
        diamond.add(i4);
        diamond.add(i5);
        diamond.add(i6);
        diamond.add(i7);
        diamond.add(i8);
        diamond.add(i9);
        diamond.add(i10);
        diamond.add(i11);
        diamond.add(j1);
        diamond.add(j2);
        diamond.add(j3);
        diamond.add(j4);
        diamond.add(j5);
        diamond.add(j6);
        diamond.add(j7);
        diamond.add(j8);
        diamond.add(j9);
        diamond.add(j10);
        diamond.add(j11);
        diamond.add(k1);
        diamond.add(k2);
        diamond.add(k3);
        diamond.add(k4);
        diamond.add(k5);
        diamond.add(k6);
        diamond.add(k7);
        diamond.add(k8);
        diamond.add(k9);
        diamond.add(k10);
        diamond.add(k11);
        
        a1.setBackground(Color.WHITE);
        a2.setBackground(Color.WHITE);
        a3.setBackground(Color.WHITE);
        a4.setBackground(Color.WHITE);
        a5.setBackground(Color.WHITE);
        a6.setBackground(Color.WHITE);
        a7.setBackground(Color.WHITE);
        a8.setBackground(Color.WHITE);
        a9.setBackground(Color.WHITE);
        a10.setBackground(Color.WHITE);
        a11.setBackground(Color.WHITE);
        b1.setBackground(Color.WHITE);
        b2.setBackground(Color.WHITE);
        b3.setBackground(Color.WHITE);
        b4.setBackground(Color.WHITE);
        b5.setBackground(Color.WHITE);
        b6.setBackground(Color.RED);
        b7.setBackground(Color.WHITE);
        b8.setBackground(Color.WHITE);
        b9.setBackground(Color.WHITE);
        b10.setBackground(Color.WHITE);
        b11.setBackground(Color.WHITE);
        c1.setBackground(Color.WHITE);
        c2.setBackground(Color.WHITE);
        c3.setBackground(Color.WHITE);
        c4.setBackground(Color.WHITE);
        c5.setBackground(Color.WHITE);
        c6.setBackground(Color.RED);
        c7.setBackground(Color.WHITE);
        c8.setBackground(Color.WHITE);
        c9.setBackground(Color.WHITE);
        c10.setBackground(Color.WHITE);
        c11.setBackground(Color.WHITE);
        d1.setBackground(Color.WHITE);
        d2.setBackground(Color.WHITE);
        d3.setBackground(Color.WHITE);
        d4.setBackground(Color.WHITE);
        d5.setBackground(Color.RED);
        d6.setBackground(Color.RED);
        d7.setBackground(Color.RED);
        d8.setBackground(Color.WHITE);
        d9.setBackground(Color.WHITE);
        d10.setBackground(Color.WHITE);
        d11.setBackground(Color.WHITE);
        e1.setBackground(Color.WHITE);
        e2.setBackground(Color.WHITE);
        e3.setBackground(Color.WHITE);
        e4.setBackground(Color.WHITE);
        e5.setBackground(Color.RED);
        e6.setBackground(Color.RED);
        e7.setBackground(Color.RED);
        e8.setBackground(Color.WHITE);
        e9.setBackground(Color.WHITE);
        e10.setBackground(Color.WHITE);
        e11.setBackground(Color.WHITE);
        f1.setBackground(Color.WHITE);
        f2.setBackground(Color.WHITE);
        f3.setBackground(Color.WHITE);
        f4.setBackground(Color.RED);
        f5.setBackground(Color.RED);
        f6.setBackground(Color.RED);
        f7.setBackground(Color.RED);
        f8.setBackground(Color.RED);
        f9.setBackground(Color.WHITE);
        f10.setBackground(Color.WHITE);
        f11.setBackground(Color.WHITE);
        g1.setBackground(Color.WHITE);
        g2.setBackground(Color.WHITE);
        g3.setBackground(Color.WHITE);
        g4.setBackground(Color.RED);
        g5.setBackground(Color.RED);
        g6.setBackground(Color.RED);
        g7.setBackground(Color.RED);
        g8.setBackground(Color.RED);
        g9.setBackground(Color.WHITE);
        g10.setBackground(Color.WHITE);
        g11.setBackground(Color.WHITE);
        h1.setBackground(Color.WHITE);
        h2.setBackground(Color.WHITE);
        h3.setBackground(Color.WHITE);
        h4.setBackground(Color.WHITE);
        h5.setBackground(Color.RED);
        h6.setBackground(Color.RED);
        h7.setBackground(Color.RED);
        h8.setBackground(Color.WHITE);
        h9.setBackground(Color.WHITE);
        h10.setBackground(Color.WHITE);
        h11.setBackground(Color.WHITE);
        i1.setBackground(Color.WHITE);
        i2.setBackground(Color.WHITE);
        i3.setBackground(Color.WHITE);
        i4.setBackground(Color.WHITE);
        i5.setBackground(Color.WHITE);
        i6.setBackground(Color.RED);
        i7.setBackground(Color.WHITE);
        i8.setBackground(Color.WHITE);
        i9.setBackground(Color.WHITE);
        i10.setBackground(Color.WHITE);
        i11.setBackground(Color.WHITE);
        j1.setBackground(Color.WHITE);
        j2.setBackground(Color.WHITE);
        j3.setBackground(Color.WHITE);
        j4.setBackground(Color.WHITE);
        j5.setBackground(Color.WHITE);
        j6.setBackground(Color.RED);
        j7.setBackground(Color.WHITE);
        j8.setBackground(Color.WHITE);
        j9.setBackground(Color.WHITE);
        j10.setBackground(Color.WHITE);
        j11.setBackground(Color.WHITE);
        k1.setBackground(Color.WHITE);
        k2.setBackground(Color.WHITE);
        k3.setBackground(Color.WHITE);
        k4.setBackground(Color.WHITE);
        k5.setBackground(Color.WHITE);
        k6.setBackground(Color.WHITE);
        k7.setBackground(Color.WHITE);
        k8.setBackground(Color.WHITE);
        k9.setBackground(Color.WHITE);
        k10.setBackground(Color.WHITE);
        k11.setBackground(Color.WHITE);
    }
    
    public void paint(Graphics g)
    {
        diamond.setVisible(true);
        
        a1.setVisible(true);
        a2.setVisible(true);
        a3.setVisible(true);
        a4.setVisible(true);
        a5.setVisible(true);
        a6.setVisible(true);
        a7.setVisible(true);
        a8.setVisible(true);
        a9.setVisible(true);
        a10.setVisible(true);
        a11.setVisible(true);
        b1.setVisible(true);
        b2.setVisible(true);
        b3.setVisible(true);
        b4.setVisible(true);
        b5.setVisible(true);
        b6.setVisible(true);
        b7.setVisible(true);
        b8.setVisible(true);
        b9.setVisible(true);
        b10.setVisible(true);
        b11.setVisible(true);
        c1.setVisible(true);
        c2.setVisible(true);
        c3.setVisible(true);
        c4.setVisible(true);
        c5.setVisible(true);
        c6.setVisible(true);
        c7.setVisible(true);
        c8.setVisible(true);
        c9.setVisible(true);
        c10.setVisible(true);
        c11.setVisible(true);
        d1.setVisible(true);
        d2.setVisible(true);
        d3.setVisible(true);
        d4.setVisible(true);
        d5.setVisible(true);
        d6.setVisible(true);
        d7.setVisible(true);
        d8.setVisible(true);
        d9.setVisible(true);
        d10.setVisible(true);
        d11.setVisible(true);
        e1.setVisible(true);
        e2.setVisible(true);
        e3.setVisible(true);
        e4.setVisible(true);
        e5.setVisible(true);
        e6.setVisible(true);
        e7.setVisible(true);
        e8.setVisible(true);
        e9.setVisible(true);
        e10.setVisible(true);
        e11.setVisible(true);
        f1.setVisible(true);
        f2.setVisible(true);
        f3.setVisible(true);
        f4.setVisible(true);
        f5.setVisible(true);
        f6.setVisible(true);
        f7.setVisible(true);
        f8.setVisible(true);
        f9.setVisible(true);
        f10.setVisible(true);
        f11.setVisible(true);
        g1.setVisible(true);
        g2.setVisible(true);
        g3.setVisible(true);
        g4.setVisible(true);
        g5.setVisible(true);
        g6.setVisible(true);
        g7.setVisible(true);
        g8.setVisible(true);
        g9.setVisible(true);
        g10.setVisible(true);
        g11.setVisible(true);
        h1.setVisible(true);
        h2.setVisible(true);
        h3.setVisible(true);
        h4.setVisible(true);
        h5.setVisible(true);
        h6.setVisible(true);
        h7.setVisible(true);
        h8.setVisible(true);
        h9.setVisible(true);
        h10.setVisible(true);
        h11.setVisible(true);
        i1.setVisible(true);
        i2.setVisible(true);
        i3.setVisible(true);
        i4.setVisible(true);
        i5.setVisible(true);
        i6.setVisible(true);
        i7.setVisible(true);
        i8.setVisible(true);
        i9.setVisible(true);
        i10.setVisible(true);
        i11.setVisible(true);
        j1.setVisible(true);
        j2.setVisible(true);
        j3.setVisible(true);
        j4.setVisible(true);
        j5.setVisible(true);
        j6.setVisible(true);
        j7.setVisible(true);
        j8.setVisible(true);
        j9.setVisible(true);
        j10.setVisible(true);
        j11.setVisible(true);
        k1.setVisible(true);
        k2.setVisible(true);
        k3.setVisible(true);
        k4.setVisible(true);
        k5.setVisible(true);
        k6.setVisible(true);
        k7.setVisible(true);
        k8.setVisible(true);
        k9.setVisible(true);
        k10.setVisible(true);
        k11.setVisible(true);
    }
}


Here's my code for the testing class:

import java.awt.*;
import javax.swing.*;
import java.awt.Component;


public class DiamondTesting
{
    public static void main(String[] args)
    {
        JFrame frm = new JFrame();
        
        Diamond yes = new Diamond();
        Diamond no = new Diamond();
        
        frm.setLayout(new GridLayout(2,1));
        
        yes.makeDiamond();
        no.makeDiamond();
        
        frm.add(yes);
        frm.add(no);
        
        no.repaint();
        yes.repaint();
        
        frm.setVisible(true);
    }
}


I'm absolutely lost as to what my problem is, as this is my first dive into declaring my own Component class. Any help here?

Is This A Good Question/Topic? 0
  • +

Replies To: Object Class extends JPanel, Won't Appear

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Object Class extends JPanel, Won't Appear

Posted 18 January 2013 - 05:42 PM

I recommend using arrays, it would make the problem much easier to see.
Was This Post Helpful? 2
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Object Class extends JPanel, Won't Appear

Posted 18 January 2013 - 06:18 PM

I am going to ignore all of the obvious problems that natecat pointed out.

Just like my girlfriends always tells me, SIZE MATTERS. do a:

diamons.setPreferredSize(new Dimension(1024,768));    



public class Diamond extends javax.swing.JPanel
{
    {
        setPreferredSize(new Dimension(800,800));
        makeDiamond();
    }

}//end class



on your JPanel diamond, and on the Diamond class. Then call pack() in the JFrame. You will see that something pops up. I didn't see you calling makeDiamond either. If you're going to override paint(), make the first call in that method "super.paint(g)".

I hope you're just playing around or are just learning with this code. :)/>

This post has been edited by farrell2k: 18 January 2013 - 06:20 PM

Was This Post Helpful? 2
  • +
  • -

#4 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: Object Class extends JPanel, Won't Appear

Posted 18 January 2013 - 06:51 PM

Okay, its still not working (I finally figured out how to describe my problem. the windows popping up when I run main(String[] args) from my testing class, but all that's in it is a bunch of gray stuff). To help you guys, I'm gonna not include most of the redundant code from what I post:

new class Diamond:

import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Container;

public class Diamond extends javax.swing.JPanel
{
    JPanel diamond;
    
    JPanel a1;
    JPanel a2;
    JPanel a3;
    JPanel a4;
    JPanel a5;
    JPanel a6;
    //redundant code goes here
    JPanel k9;
    JPanel k10;
    JPanel k11;
    {
        setPreferredSize(new Dimension(100,100));
        makeDiamond();
    }
    
    public void makeDiamond()
    {
        diamond = new JPanel(new GridLayout(11,11));
        diamond.setPreferredSize(new Dimension(1024,768));
        
        a1 = new JPanel();
        a2 = new JPanel();
        a3 = new JPanel();
        a4 = new JPanel();
        a5 = new JPanel();
        a6 = new JPanel();
        //redundant code goes here
        k9 = new JPanel();
        k10 = new JPanel();
        k11 = new JPanel();
        
        diamond.add(a1);
        diamond.add(a2);
        diamond.add(a3);
        diamond.add(a4);
        diamond.add(a5);
        diamond.add(a6);
        //redundant code goes here
        diamond.add(k9);
        diamond.add(k10);
        diamond.add(k11);
        
        a1.setBackground(Color.WHITE);
        a2.setBackground(Color.WHITE);
        a3.setBackground(Color.WHITE);
        a4.setBackground(Color.WHITE);
        a5.setBackground(Color.WHITE);
        a6.setBackground(Color.WHITE);
        //redundant code goes here
        k9.setBackground(Color.WHITE);
        k10.setBackground(Color.WHITE);
        k11.setBackground(Color.WHITE);
        
        this.add(diamond);
    }
    
    public void paint(Graphics g)
    {
        diamond.setVisible(true);
        
        a1.setVisible(true);
        a2.setVisible(true);
        a3.setVisible(true);
        a4.setVisible(true);
        a5.setVisible(true);
        a6.setVisible(true);
        //redundant code goes here
        k9.setVisible(true);
        k10.setVisible(true);
        k11.setVisible(true);
    }
}


New class testing (with what became redundant because of changes commented out)

import java.awt.*;
import javax.swing.*;
import java.awt.Component;


public class DiamondTesting
{
    public static void main(String[] args)
    {
        JFrame frm = new JFrame();
        
        Diamond yes = new Diamond();
        Diamond no = new Diamond();
        
        frm.setLayout(new GridLayout(2,1));
        
        //yes.makeDiamond();
        //no.makeDiamond();
        
        frm.add(yes);
        frm.add(no);
        
        no.repaint();
        yes.repaint();
        
        frm.setVisible(true);
    }
}

Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Object Class extends JPanel, Won't Appear

Posted 18 January 2013 - 07:10 PM

You are just not listening.

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

import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Container;

public class Diamond extends javax.swing.JPanel
{
    JPanel diamond;
    
    JPanel a1;
    JPanel a2;
    JPanel a3;
    JPanel a4;
    JPanel a5;
    JPanel a6;
    //redundant code goes here
    JPanel k9;
    JPanel k10;
    JPanel k11;
    {
        makeDiamond();
    }
    
    public void makeDiamond()
    {
        diamond = new JPanel(new GridLayout(11,11,2,2));
        diamond.setPreferredSize(new Dimension(200,200));
        
        a1 = new JPanel();
        a2 = new JPanel();
        a3 = new JPanel();
        a4 = new JPanel();
        a5 = new JPanel();
        a6 = new JPanel();
        //redundant code goes here
        k9 = new JPanel();
        k10 = new JPanel();
        k11 = new JPanel();
        
        diamond.add(a1);
        diamond.add(a2);
        diamond.add(a3);
        diamond.add(a4);
        diamond.add(a5);
        diamond.add(a6);
        //redundant code goes here
        diamond.add(k9);
        diamond.add(k10);
        diamond.add(k11);
        
        a1.setBackground(Color.WHITE);
        a2.setBackground(Color.WHITE);
        a3.setBackground(Color.WHITE);
        a4.setBackground(Color.WHITE);
        a5.setBackground(Color.WHITE);
        a6.setBackground(Color.WHITE);
        //redundant code goes here
        k9.setBackground(Color.WHITE);
        k10.setBackground(Color.WHITE);
        k11.setBackground(Color.WHITE);
        
        this.add(diamond);
    }
    
    public void paint(Graphics g)
    {
        super.paint(g);
        diamond.setVisible(true);
        
        a1.setVisible(true);
        a2.setVisible(true);
        a3.setVisible(true);
        a4.setVisible(true);
        a5.setVisible(true);
        a6.setVisible(true);
        //redundant code goes here
        k9.setVisible(true);
        k10.setVisible(true);
        k11.setVisible(true);
    }
}



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

import java.awt.*;
import javax.swing.*;
import java.awt.Component;


public class DiamondTesting
{
    public static void main(String[] args)
    {
        JFrame frm = new JFrame();
        
        Diamond yes = new Diamond();
        Diamond no = new Diamond();
        
        frm.setLayout(new GridLayout(2,1));
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //yes.makeDiamond();
        //no.makeDiamond();
        
        frm.add(yes);
        frm.add(no);
        
        no.repaint();
        yes.repaint();
        
        frm.setVisible(true);
        
        frm.pack();
    }
}



super.paint(g) is important. Your background and components will not draw properly without it.
Was This Post Helpful? 1
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Object Class extends JPanel, Won't Appear

Posted 18 January 2013 - 07:28 PM

import java.awt.*;
import javax.swing.*;


public class DiamondTesting
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frm = new JFrame();
        
                Diamond yes = new Diamond();
                Diamond no = new Diamond();
                
                JPanel center = new JPanel() {
                    {
                        setPreferredSize(new Dimension(200,200));
                    }
                };

                frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frm.add(yes, BorderLayout.NORTH);
                frm.add(no, BorderLayout.SOUTH);
                frm.add(center, BorderLayout.CENTER);
                frm.pack();
                frm.setVisible(true);

            }
        });
    }
}



import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JPanel;

/**
 *
 * @author tj
 */
public class Diamond extends JPanel {
    public Diamond() {
        setLayout(new GridLayout(10,10,2,2));
        
        JPanel[] panels = new JPanel[100];
        
        for (int i = 0; i < panels.length; i++) {
            JPanel p = new JPanel() {
                {
                    setPreferredSize(new Dimension(10,10));
                    setBackground(Color.white);
                }
            };
            
            panels[i] = p;
        }
        
        for (JPanel p : panels) {
            add(p);
        }
    }
}//end class.


Was This Post Helpful? 0
  • +
  • -

#7 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: Object Class extends JPanel, Won't Appear

Posted 19 January 2013 - 07:57 AM

@farrell2k Thanks for the help. Sorry that I somehow (I don't even know how) missed the thing about super.paint(g) the first time. Hopefully I wasn't too frustrating.
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Object Class extends JPanel, Won't Appear

Posted 19 January 2013 - 10:14 AM

Your class Diamond IS a JPanel
you shouldn't declare a JPanel diamond inside it and you shouldn't execute

new JDiamond(); inside it

class Diamond extends JPanel {

   // constructor
   Diamond() {
      super(new LayoutOfSomeKind...
      add(other panels
      setSize(...
   }


Was This Post Helpful? 0
  • +
  • -

#9 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: Object Class extends JPanel, Won't Appear

Posted 19 January 2013 - 06:04 PM

@pbl thanks to farrell2k, creating objects of this class works fine. extending JPanel instead of JContainer (or is it JComponent? I forget) does not seem to hinder the creation of the object, or the visibility of the object.

And where inside my Diamond class do you see a call to new Diamond()? The creation of my object of class Diamond is done inside my testing class.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Object Class extends JPanel, Won't Appear

Posted 20 January 2013 - 12:10 PM

Check out this section. What pbl is saying is that this doesn't make sense. Why extend JPanel to create a Diamond and have an internal JPanel named Diamond? It seems redundant to use both an is-a and a has-a relationship.
public class Diamond extends javax.swing.JPanel
07	{
08	    JPanel diamond;


Was This Post Helpful? 0
  • +
  • -

#11 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Object Class extends JPanel, Won't Appear

Posted 20 January 2013 - 12:17 PM

Hmmm. He's nesting JPanels within a JPanel. Maybe later he will add a Ruby JPanel, then a Sapphire Jpanel. One father JPanel with 3 child JPanels. That doesn't seem unreasonable to me.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Object Class extends JPanel, Won't Appear

Posted 20 January 2013 - 12:20 PM

The naming conventions could use some work in that case then. :)
Was This Post Helpful? 1
  • +
  • -

#13 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: Object Class extends JPanel, Won't Appear

Posted 20 January 2013 - 02:19 PM

@farrell2k Actually, its a card game, and I'm gonna also have a Club JPanel, Spade JPanel, Heart JPanel, Ace JPanel, Ten JPanel, King JPanel, Queen JPanel, and Jack JPanel (I actually already do). I've already used these JPanels to create AceClub JPanel, TenClub JPanel, etc.
Was This Post Helpful? 0
  • +
  • -

#14 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Object Class extends JPanel, Won't Appear

Posted 20 January 2013 - 02:22 PM

View Postextremeblueness, on 20 January 2013 - 09:19 PM, said:

@farrell2k Actually, its a card game, and I'm gonna also have a Club JPanel, Spade JPanel, Heart JPanel, Ace JPanel, Ten JPanel, King JPanel, Queen JPanel, and Jack JPanel (I actually already do). I've already used these JPanels to create AceClub JPanel, TenClub JPanel, etc.


Why? What's your thinking behind this? It's a slow day here, so maybe we can talk about it. :)

This post has been edited by farrell2k: 20 January 2013 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Object Class extends JPanel, Won't Appear

Posted 20 January 2013 - 02:49 PM

The way I would do this is to do some graphics work outside of Java and make the icons yourself, and save them as image files. Then just have your program read in the 52 ImageIcons. It's much easier to setIcon() on your JLabels than to do a lot of custom drawing with a bunch of JPanels.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2