JPanel or JFrame problem?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: JPanel or JFrame problem?

  1. #1
    Join Date
    May 2004
    Posts
    37

    JPanel or JFrame problem?

    Hello Everyone,
    I am working on a solitaire program. I am able to run and compile the program. Every time I resize the frame, I get small gray squares to the right of my buttons. Also, my cards get changed every time I resize the frame. Any suggestions are appreciated.
    Thanks,
    E

    //SolitaireProject.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class SolitaireProject extends JApplet
    {
    public SolitaireProject()
    {
    this.setContentPane(new SolitaireProjectPanel());
    }
    public static void main(String[] args)
    {
    JFrame window = new JFrame();
    window.setTitle("Solitaire by E");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setContentPane(new SolitaireProjectPanel());
    window.pack();
    window.show();
    }
    }

    //SolitaireProjectPanel.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*;
    import java.applet.*;

    public class SolitaireProjectPanel extends JPanel
    {
    private static final int CARD_WIDTH = 77;
    private static final int CARD_HEIGHT = 110;

    private final int ACES_DISTANCE_BETWEEN_CARDS = 97;
    private final int ACES_DISTANCE_FROM_LEFT = 425;
    private final int ACES_DISTANCE_FROM_TOP = 50;
    private final int ACES_MAXIMUM = 793;

    private final int PLAYCARDS_DISTANCE_FROM_LEFT = 300;
    private final int PLAYCARDS_DISTANCE_FROM_TOP = 200;
    private final int PLAYCARDS_DISTANCE_BETWEEN_COLUMNS = 87;
    private final int PLAYCARDS_DISTANCE_BETWEEN_ROWS_FACEDOWN = 5;
    private final int PLAYCARDS_DISTANCE_BETWEEN_ROWS_FACEUP = 30;
    private final int PLAYCARDS_MAXIMUM = 899;

    private final int PILE_DISTANCE_FROM_LEFT = 75;
    private final int PILE_DISTANCE_FROM_TOP = 50;

    private final int SHOWCARD_DISTANCE_FROM_LEFT = 75;
    private final int SHOWCARD_DISTANCE_FROM_TOP = 200;

    private int _initX = 0; // x coord - set from drag
    private int _initY = 0; // y coord - set from drag

    private Card[] _cardFronts = new Card[52]; //Creates an array of cards
    private Card _currentCard = null; //Current draggable card

    private Card[] _cardBacks = new Card[52];

    public SolitaireProjectPanel()
    {
    String suits = "CDHS";
    String faces = "A23456789TJQK";
    int cardPosition = 0;
    int crdBackPosition = 0;

    for(int suit = 0; suit < suits.length(); suit++)
    {
    for(int face = 0; face<faces.length(); face++)
    {
    ImageIcon img = new ImageIcon("card " + faces.charAt(face) + suits.charAt(suit) + ".jpg");
    Image image = img.getImage();
    Image scaledImage = image.getScaledInstance(77, 110, Image.SCALE_FAST);
    img.setImage(scaledImage);
    _cardFronts[cardPosition ++] = new Card (img, _initX++, _initY++);
    }
    }

    for(int crdBack = 0; crdBack < 52; crdBack++)
    {
    ImageIcon img2 = new ImageIcon("cardBack.jpg");
    Image image = img2.getImage();
    Image scaledImage = image.getScaledInstance(CARD_WIDTH, CARD_HEIGHT, Image.SCALE_FAST);
    img2.setImage(scaledImage);
    _cardBacks[crdBackPosition ++] = new Card (img2, _initX++, _initY++);
    }

    //shuffle();

    JPanel setUpPanel = new JPanel();

    setPreferredSize(new Dimension(1000, 700));
    setBackground(Color.white);

    JButton newGameButton = new JButton("New Game");
    JButton exitButton = new JButton("Exit");

    exitButton.addActionListener(new ExitAction());
    newGameButton.addActionListener(new NewGameAction());

    this.add(newGameButton);
    this.add(exitButton);

    }

    class ExitAction implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }

    class NewGameAction implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    shuffle();
    repaint();
    }
    }

    public void paintComponent(Graphics g)
    {
    shuffle();
    JPanel drawAndDealPanel = new JPanel();
    super.paintComponent(g);
    setBackground(Color.white);
    g.drawRect(PILE_DISTANCE_FROM_LEFT, PILE_DISTANCE_FROM_TOP, CARD_WIDTH, CARD_HEIGHT);
    g.drawRect(SHOWCARD_DISTANCE_FROM_LEFT, SHOWCARD_DISTANCE_FROM_TOP, CARD_WIDTH, CARD_HEIGHT);

    for(int position1 = ACES_DISTANCE_FROM_LEFT; position1 <= ACES_MAXIMUM; position1 += ACES_DISTANCE_BETWEEN_CARDS)
    {
    g.drawRect(position1, ACES_DISTANCE_FROM_TOP, CARD_WIDTH, CARD_HEIGHT);
    }
    for(int position2 = PLAYCARDS_DISTANCE_FROM_LEFT; position2 <= PLAYCARDS_MAXIMUM; position2 += PLAYCARDS_DISTANCE_BETWEEN_COLUMNS)
    {
    g.drawRect(position2, PLAYCARDS_DISTANCE_FROM_TOP, CARD_WIDTH, CARD_HEIGHT);
    }

    int crd1 = 1;
    int column1 = 300;
    int fromTop1 = PLAYCARDS_DISTANCE_FROM_TOP;
    Card cf = _cardFronts[crd1];
    cf.image.paintIcon(this, g, column1, fromTop1);

    crd1 = 2;
    column1 = 300 + PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    for(int count = 0; count < 6; count ++)
    {
    Card cb = _cardBacks[crd1];
    cb.image.paintIcon(this, g, column1, fromTop1);
    crd1 ++;
    column1 += PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    }

    int crd2 = 8;
    int column2 = 387;
    int fromTop2 = fromTop1 + PLAYCARDS_DISTANCE_BETWEEN_ROWS_FACEDOWN;
    cf = _cardFronts[crd2];
    cf.image.paintIcon(this, g, column2, fromTop2);

    crd2 = 9;
    column2 = 387 + PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    for(int count = 0; count < 5; count ++)
    {
    Card cb = _cardBacks[crd2];
    cb.image.paintIcon(this, g, column2, fromTop2);
    crd2 ++;
    column2 += PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    }

    int crd3 = 14;
    int column3 = 474;
    int fromTop3 = fromTop2 + PLAYCARDS_DISTANCE_BETWEEN_ROWS_FACEDOWN;
    cf = _cardFronts[crd3];
    cf.image.paintIcon(this, g, column3, fromTop3);

    crd3 = 15;
    column3 = 474 + PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    for(int count = 0; count < 4; count ++)
    {
    Card cb = _cardBacks[crd3];
    cb.image.paintIcon(this, g, column3, fromTop3);
    crd3 ++;
    column3 += PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    }

    int crd4 = 19;
    int column4 = 561;
    int fromTop4 = fromTop3 + PLAYCARDS_DISTANCE_BETWEEN_ROWS_FACEDOWN;
    cf = _cardFronts[crd4];
    cf.image.paintIcon(this, g, column4, fromTop4);

    crd4 = 20;
    column4 = 561 + PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    for(int count = 0; count < 3; count ++)
    {
    Card cb = _cardBacks[crd4];
    cb.image.paintIcon(this, g, column4, fromTop4);
    crd4 ++;
    column4 += PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    }

    int crd5 = 23;
    int column5 = 648;
    int fromTop5 = fromTop4 + PLAYCARDS_DISTANCE_BETWEEN_ROWS_FACEDOWN;
    cf = _cardFronts[crd5];
    cf.image.paintIcon(this, g, column5, fromTop5);

    crd5 = 24;
    column5 = 648 + PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    for(int count = 0; count < 2; count ++)
    {
    Card cb = _cardBacks[crd5];
    cb.image.paintIcon(this, g, column5, fromTop5);
    crd5 ++;
    column5 += PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    }

    int crd6 = 26;
    int column6 = 735;
    int fromTop6 = fromTop5 + PLAYCARDS_DISTANCE_BETWEEN_ROWS_FACEDOWN;
    cf = _cardFronts[crd6];
    cf.image.paintIcon(this, g, column6, fromTop6);

    crd6 = 27;
    column6 = 735 + PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    for(int count = 0; count < 1; count ++)
    {
    Card cb = _cardBacks[crd6];
    cb.image.paintIcon(this, g, column6, fromTop6);
    crd6 ++;
    column6 += PLAYCARDS_DISTANCE_BETWEEN_COLUMNS;
    }

    int crd7 = 28;
    int column7 = 822;
    int fromTop7 = fromTop6 + PLAYCARDS_DISTANCE_BETWEEN_ROWS_FACEDOWN;
    cf = _cardFronts[crd7];
    cf.image.paintIcon(this, g, column7, fromTop7);

    for(int playCard = 29; playCard < 52; playCard ++)
    {
    Card cb = _cardBacks[playCard];
    cb.image.paintIcon(this, g, PILE_DISTANCE_FROM_LEFT, PILE_DISTANCE_FROM_TOP);
    }
    this.add(drawAndDealPanel);

    }

    //shuffle
    public void shuffle()
    {
    Random rgen = new Random();

    for (int i=0; i<52; i++)
    {
    int randomPosition = rgen.nextInt(52);
    Card temp = _cardFronts[i];
    _cardFronts[i] = _cardFronts[randomPosition];
    _cardFronts[randomPosition] = temp;
    }
    }
    }

    class Card
    {
    ImageIcon image;
    int x;
    int y;

    public Card(ImageIcon image, int x, int y)
    {
    this.image = image;
    this.x = x;
    this.y = y;
    }
    }

  2. #2
    Join Date
    May 2004
    Posts
    37

    hello?

    Is anyone available to answer questions?

  3. #3
    Join Date
    May 2004
    Posts
    37

    anyone?

    Hello?

    Is this forum active or not?

    Is my question legit???

  4. #4
    Join Date
    May 2004
    Posts
    37

    Nevermind

    Nevermind, I figured out what was wrong. Thanks to everyone for the help. You are some true pros.

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    next time, try www.codeguru.com

    ps; you really should consider using a Layout Manager, instead of positioning all your components absolutely
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center