Article View: borland.public.jbuilder.java.swing
Article #8753help with repainting
From: Randy
Date: Tue, 18 Jul 2000 00:00
Date: Tue, 18 Jul 2000 00:00
244 lines
7774 bytes
7774 bytes
can someone please tell me why this code doesnt repaint the card?
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import RandyClasses.*;
public class DeckOfCards extends JApplet {
private Cards dealt;
private Card deck[];
private int currentCard;
private JButton dealButton,shuffleButton;
private JTextField displayCard;
private JLabel status;
private Container c = getContentPane();
public void shuffle() {
currentCard = -1;
for(int i=0;i<52;i++) {
int j = (int) (Math.random()*52);
Card temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
dealButton.setEnabled(true);
}
public Card dealCard() {
if(++currentCard < deck.length) {
return deck[currentCard];
}
else {
dealButton.setEnabled(false);
return null;
}
}
public void paintit(Card newCard) {
dealt = newCard.toCards(0);
displayCard.setText(dealt.toString());
dealt.repaint();
}
public void init() {
JTextField dummyField;
deck = new Card[52];
currentCard=-1;
c.setLayout(new FlowLayout());
dealt = new Cards();
c.add(dealt);
dummyField = new JTextField(40);
dummyField.setEditable(false);
c.add(dummyField);
for(int i=0;i <deck.length;i++)
deck[i] = new Card(i % 13,i/13);
dealButton = new JButton("Deal Button");
dealButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Card d = dealCard();
if(dealt != null){
paintit(d);
status.setText("Card #:"+currentCard);
}
else {
displayCard.setText("NO MORE CARDS TO DEAL");
status.setText("Shuffle cards to continue");
}
}
}
);
c.add(dealButton);
shuffleButton = new JButton("Shuffle Cards");
init2();
c.add(shuffleButton);
displayCard = new JTextField(20);
displayCard.setEditable(false);
c.add(displayCard);
status = new JLabel();
c.add(status);
}
public void init2() {
shuffleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayCard.setText("Shuffling....");
shuffle();
displayCard.setText("DECK IS SHUFFLED");
}
});
}
}
import java.awt.*;
import RandyClasses.*;
public class Cards extends Canvas{
private int value,face,suit;
private SuitImage image;
private int x,y;
private Point p;
public Cards () {
face = 0;
suit = 0;
value = 0;
image = new SuitImage("heart.jpg");
p = new Point(this.getLocation());
x = p.x;
y = p.y;
}
public Cards(int f,int s,int v) {
face = f;
suit = s;
value = v;
switch(suit) {
case 0: image = new SuitImage("heart.jpg");;
break;
case 1: image = new SuitImage("spade.jpg");;
break;
case 2: image = new SuitImage("diamond.jpg");;
break;
case 3: image = new SuitImage("club.jpg");;
break;
}
p = new Point(this.getLocation());
x = p.x;
y = p.y;
}
public int getCardsValue() {
return value;
}
public String toString() {
String faces[] = {" Ace","Two","Three","Four","Five",
"Six","Seven","Eight","Nine","Ten",
"Jack","Queen","King"};
String suits[] = {"Hearts","Spades","Diamonds","Clubs"};
return faces[face]+" of "+suits[suit];
}
public void paint(Graphics g) {
String faces[] = {" A"," 2"," 3"," 4"," 5",
" 6"," 7"," 8"," 9","10",
" J"," Q"," K"};
g.setColor(Color.white);
g.fillRect(x,y,85,100);
if ((suit == 0) || (suit == 2))
g.setColor(Color.red);
else
g.setColor(Color.black);
g.drawString(faces[face],x,10+y);
g.drawString(faces[face],x+70,y+100);
switch(face) {
case 0: image.paint(g,x+30,y+40);
break;
case 1: image.paint(g,x+30,y+10);
image.paint(g,x+30,y+70);
break;
case 2: image.paint(g,x+30,y+10);
image.paint(g,x+30,y+40);
image.paint(g,x+30,y+70);
break;
case 3: image.paint(g,x+10,y+10);
image.paint(g,x+10,y+70);
image.paint(g,x+50,y+10);
image.paint(g,x+50,y+70);
break;
case 4: image.paint(g,x+10,y+10);
image.paint(g,x+10,y+70);
image.paint(g,x+30,y+40);
image.paint(g,x+50,y+10);
image.paint(g,x+50,y+70);
break;
case 5: image.paint(g,x+10,y+10);
image.paint(g,x+10,y+40);
image.paint(g,x+10,y+70);
image.paint(g,x+50,y+10);
image.paint(g,x+50,y+40);
image.paint(g,x+50,y+70);
break;
case 6: image.paint(g,x+10,y+10);
image.paint(g,x+10,y+40);
image.paint(g,x+10,y+70);
image.paint(g,x+50,y+10);
image.paint(g,x+50,y+40);
image.paint(g,x+50,y+70);
image.paint(g,x+30,y+30);
break;
case 7: image.paint(g,x+10,y+10);
image.paint(g,x+10,y+40);
image.paint(g,x+10,y+70);
image.paint(g,x+50,y+10);
image.paint(g,x+50,y+40);
image.paint(g,x+50,y+70);
image.paint(g,x+30,y+30);
image.paint(g,x+30,y+55);
break;
case 8: image.paint(g,x+10,y+10);
image.paint(g,x+10,y+30);
image.paint(g,x+10,y+50);
image.paint(g,x+10,y+70);
image.paint(g,x+50,y+10);
image.paint(g,x+50,y+30);
image.paint(g,x+50,y+50);
image.paint(g,x+50,y+70);
image.paint(g,x+30,y+40);
break;
case 9: image.paint(g,x+10,y+10);
image.paint(g,x+10,y+30);
image.paint(g,x+10,y+50);
image.paint(g,x+10,y+70);
image.paint(g,x+50,y+10);
image.paint(g,x+50,y+30);
image.paint(g,x+50,y+50);
image.paint(g,x+50,y+70);
image.paint(g,x+30,y+30);
image.paint(g,x+30,y+55);
break;
case 10: image.paint(g,x+30,y+40);
break;
case 11: image.paint(g,x+30,y+40);
break;
case 12: image.paint(g,x+30,y+40);
break;
}
}
public Dimension getPreferredSize() {
return new Dimension(85,100);
}
}
Technical Details
Message-ID:
<8l2lqn$8qm6@bornews.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,6a8993813838b9b9,start X-Google-Attributes: gid111d45,public From: "Randy" <slyfox65@netzero.net> Subject: help with repainting Date: 2000/07/18 Message-ID: <8l2lqn$8qm6@bornews.borland.com> X-Deja-AN: 647974652 X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Newsgroups: borland.public.jbuilder.java.swing