aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JPanel or JFrame problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel or JFrame problem?" Watch "JPanel or JFrame problem?" New topic
Author

JPanel or JFrame problem?

jay smith
Greenhorn

Joined: May 03, 2004
Posts: 23
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;
_cardFronts = _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;
}
}
jay smith
Greenhorn

Joined: May 03, 2004
Posts: 23
OK, I took these two lines out of the program:
JPanel drawAndDealPanel = new JPanel();
this.add(drawAndDealPanel);
So now I got rid of the little gray squares, but I can not figure out why my cards change every time I resize the frame.
Thanks in advance,
E
jay smith
Greenhorn

Joined: May 03, 2004
Posts: 23
Is anyone available to answer my question?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Your code wouldn't compile until I made these changes in shuffle

ps - now I see the forum software has messed up the [i] parts... so never mind the above...

paintComponent is called to render its JComponent. It should contain only code that pertains to painting.

Each time the component is resized it calls paintComponent to repaint itself in the newly–resized container. So all the code in paintComponent is executed. Your call to shuffle is the cause of your cards changing after resizing. shuffle should only be called in an event handler.

setBackground is a Component method and belongs in the class constructor (which I see you already have).

Although SolitaireProject extends JApplet you have written it as an application (with a constructor). To write it as an applet you would use init and load the applet into your JFrame in main.

Now you can also run it in appletviewer with this comment at the top of the SolitaireProject file

[ May 04, 2004: Message edited by: Craig Wood ]
[ May 04, 2004: Message edited by: Craig Wood ]
[ May 04, 2004: Message edited by: Craig Wood ]
jay smith
Greenhorn

Joined: May 03, 2004
Posts: 23
Thanks so much. I fixed my problems by taking these lines out of the code:
//(In public void paintComponent)
shuffle();
JPanel drawAndDealPanel = new JPanel();
.
.
.
this.add(drawAndDealPanel);
Thanks for the help!!!
jay smith
Greenhorn

Joined: May 03, 2004
Posts: 23
If I want to separate this into 3 files (main, gui, and logic), what would be the best way?
I would leave paintComponent in the GUI, but I want to move the dealing of the cards to the logic.
But, I am using paintIcon to draw the cards.
Any suggestions?
Thanks in advance,
Jay
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
file 1 - JApplet SolitaireProject class

file 2 - main with classes SolitaireProjectPanel and Card

file 3 - logic with new class that includes newGameButton, exitButton, their listeners and the shuffle method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPanel or JFrame problem?