Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem Getting Graphics to Show

$
0
0
Hi. I'm having trouble getting my graphics to actually show up. As far as I'm aware, this is the only problem. It worked perfectly before I switched to arrays, but alas, I deleted my old graphics classes, and now I can't compare the classes. I know very little about making my own graphics classes to create objects with.

Here's my code for the graphics class. It's a bit long at 464 lines of code, but I don't know what I need to leave out from my post:

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

public class Card extends JPanel
{
    JPanel[][] card;
    JPanel background;
    
    public Card(String c)
    {
        final String cardType = c;
        background = new JPanel();
        background.setLayout(new GridLayout(22,11));
        card = new JPanel[22][11];
        
        for (int i = 0; i < 22; )
        {
            for (int j = 0; j < 11; )
            {
                card[i][j] = new JPanel();
                card[i][j].setBackground(Color.WHITE);
            }
            i++;
        }
        
        if (cardType.contains("jack"))
        {
            for (int i = 0; i < 11; )
            {
                for (int j = 0; j < 11; )
                {
                    if ((i == 1) || (i == 2))
                    {
                        if ((j >= 2) && (j <= 8))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if ((i == 3) || (i == 4) || (i == 5))
                    {
                        if ((j == 5) || (j == 6))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if ((i == 6) || (i == 7))
                    {
                        if ((j >= 2) && (j <= 6) && (j != 4))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if ((i == 8) || (i == 9))
                    {
                        if ((j >= 2) && (j <= 6))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        else if (cardType.contains("queen"))
        {
            for (int i = 0; i < 11; )
            {
                for (int j = 0; j < 11; )
                {
                    if ((i == 1) || (i == 2))
                    {
                        if ((j >= 2) && (j <= 7))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if ((i == 3) || (i == 4))
                    {
                        if ((j == 2) || (j == 3) || (j == 6) || (j == 7))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 5)
                    {
                        if ((j >= 2) && (j <= 7) && (j != 4))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 6)
                    {
                        if ((j >= 2) && (j <= 7))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 7)
                    {
                        if ((j >= 2) && (j <= 8))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 8)
                    {
                        if ((j == 7) || (j == 8))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        else if (cardType.contains("king"))
        {
            for (int i = 0; i < 11; )
            {
                for (int j = 0; j < 11; )
                {
                    if (i == 1)
                    {
                        if ((j == 2) || (j == 3) || (j == 8) || (j == 9))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 2)
                    {
                        if ((j == 2) || (j == 3) || ((j >= 7) && (j <= 9)))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 3)
                    {
                        if ((j == 2) || (j == 3) || ((j >= 6) && (j <= 8)))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 4)
                    {
                        if ((j >= 2) && (j <= 7) && (j != 4))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if ((i == 5) || (i == 6))
                    {
                        if ((j >= 2) && (j <= 6))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 7)
                    {
                        if ((j == 2) || (j == 3) || (j == 6) || (j == 7))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 8)
                    {
                        if ((j == 2) || (j == 3) || ((j >= 6) && (j == 8)))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 9)
                    {
                        if ((j == 2) || (j == 3) || ((j>= 7) && (j <= 9)))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        else if (cardType.contains("ten"))
        {
            for (int i = 0; i < 11; )
            {
                for (int j = 0; j < 11; )
                {
                    if ((i >= 1) || (i <= 9))
                    {
                        if ((j == 2) || (j == 3) || ((j >= 5) && (j <= 9) && (j != 7)))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                        if ((i == 1) || (i == 2) || (i == 8) || (i == 9))
                        {
                            if (j == 7)
                            {
                                card[i][j].setBackground(Color.BLUE);
                            }
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        else
        {
            for (int i = 0; i < 11; )
            {
                for (int j = 0; j < 11; )
                {
                    if (i == 1)
                    {
                        if (j == 5)
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 2)
                    {
                        if ((j >= 4) || (i <= 6))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 3)
                    {
                        if ((j == 4) || (j == 6))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if ((i == 4) || (i == 5))
                    {
                        if ((j >= 3) && (j <= 7) && (j != 5))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 6)
                    {
                        if ((j >= 2) && (j <= 8))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if (i == 7)
                    {
                        if ((j == 2) || (j == 3) || (j == 7) || (j == 8))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    else if ((i == 8) || (i == 9))
                    {
                        if ((j == 1) || (j == 2) || (j == 8) || (j == 9))
                        {
                            card[i][j].setBackground(Color.BLUE);
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        
        if (cardType.contains("diamond"))
        {
            for (int i = 11; i < 22; )
            {
                for (int j = 0; j < 11; )
                {
                    if ((j == 3) || (j == 7))
                    {
                        if ((i == 5) || (i == 6))
                        {
                            card[i][j].setBackground(Color.RED);
                        }
                    }
                    else if ((j == 4) || (j == 6))
                    {
                        if ((i >= 3) && (i <= 7))
                        {
                            card[i][j].setBackground(Color.RED);
                        }
                    }
                    else if (j == 5)
                    {
                        if ((i >= 1) && (i <= 9))
                        {
                            card[i][j].setBackground(Color.RED);
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        else if (cardType.contains("spade"))
        {
            for (int i = 11; i < 22; )
            {
                for (int j = 0; j < 11; )
                {
                    if ((j == 2) || (j == 8))
                    {
                        if ((i >= 4) && (i <= 7))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    else if ((j == 3) || (j == 7))
                    {
                        if ((i >= 3) && (i <= 7))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    else if ((j == 4) || (j == 6))
                    {
                        if ((i >= 2) && (i <= 6))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    else if (j == 5)
                    {
                        if ((i >= 1) || (i <= 9))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        else if (cardType.contains("heart"))
        {
            for (int i = 11; i < 22; )
            {
                for (int j = 0; j < 11; )
                {
                    if ((j == 1) || (j == 9))
                    {
                        if ((i >= 2) && (i <= 4))
                        {
                            card[i][j].setBackground(Color.RED);
                        }
                    }
                    else if ((j == 2) || (j == 8))
                    {
                        if ((i >= 1) && (i <= 6))
                        {
                            card[i][j].setBackground(Color.RED);
                        }
                    }
                    else if ((j == 3) || (j == 7))
                    {
                        if ((i >= 1) && (i <= 7))
                        {
                            card[i][j].setBackground(Color.RED);
                        }
                    }
                    else if ((j == 4) || (j == 6))
                    {
                        if ((i >= 2) && (i <= 8))
                        {
                            card[i][j].setBackground(Color.RED);
                        }
                    }
                    else if (j == 5)
                    {
                        if ((i >= 3) && (i <= 9))
                        {
                            card[i][j].setBackground(Color.RED);
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        else
        {
            for (int i = 11; i < 22; )
            {
                for (int j = 0; j < 11; )
                {
                    if ((j == 1) || (j == 9))
                    {
                        if ((i == 4) || (i == 5))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    else if ((j == 2) || (j == 8))
                    {
                        if ((i >= 3) && (i <= 6))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    else if ((j == 3) || (j == 7))
                    {
                        if ((i >= 4) && (i <= 6))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    else if ((j == 4) || (j == 6))
                    {
                        if ((i >= 1) && (i <= 5))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    else if (j == 5)
                    {
                        if ((i >= 1) || (i <= 9))
                        {
                            card[i][j].setBackground(Color.BLACK);
                        }
                    }
                    j++;
                }
                i++;
            }
        }
        
        for (int i = 0; i < 22; )
        {
            for (int j = 0; j < 11; )
            {
                background.add(card[i][j]);
                j++;
            }
            i++;
        }
        
        repaint();
    }
    
    public void paint(Graphics g)
    {
        super.paint(g);
        background.setVisible(true);
        for(int i = 0; i < 22; )
        {
            for (int j = 0; j < 11; )
            {
                card[i][j].setVisible(true);
                j++;
            }
            i++;
        }
    }
}


Also, here's the code that uses my graphics class starting at line 139 of class Game:

        Card[] cardsInHand = new Card[30];
        for (int i = 0; i < 25; )
        {
            cardsInHand[i] = new Card(hand[i]);
            i++;
        }
        for (int i = 0; i < 25; )
        {
            cardsInHand[i].setVisible(true);
            i++;
        }
        for (int i = 0; i < 25; )
        {
            deckSpace.add(cardsInHand[i]);
        }
        
        Card[] kittyCards = new Card[5];
        for (int i = 0; i < 5; )
        {
            kittyCards[i] = new Card(kitty[i]);
            i++;
        }

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>