java - Uno card draw game cant print the card to the panel -
im working on program draws card collection(deck) of uno cards. should display first card on startup, every time "next card" button pressed draws card. i'm new gui's , can't figure out i'm doing wrong. "uno" in box should display text representation of card.
package uno; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class gameframe extends jframe { private playingcardpanel panel; private jpanel field; private jpanel menu; public gameframe(){ this.setlayout(new flowlayout()); this.setsize( 300, 150); this.settitle("uno"); this.createfieldpanel(); this.createmenupanel(); this.add(field); this.add(menu); } private void createmenupanel(){ menu = new jpanel(); menu.setbounds(0,300,250,100); jbutton drawbutton = new jbutton("next card"); drawbutton.setsize(300,200); drawbutton.addactionlistener(new drawlistener()); menu.add(drawbutton); } private void createfieldpanel() { field = new jpanel(); field.setbounds(0,0,250,200); panel = new playingcardpanel(); field.add(panel); } private class drawlistener implements actionlistener{ public void actionperformed(actionevent e){ panel.drawcard(); } } } package uno; import java.awt.*; import javax.swing.*; public class playingcardpanel extends jpanel{ private standarddeck playingdeck; private playingcard card; public playingcardpanel(){ this.setpreferredsize(new dimension(100,100)); playingdeck = new standarddeck(); playingdeck.shuffle(); playingdeck.drawtopcard(); } public void drawcard(){ playingdeck.drawtopcard(); } public void paintcomponent(graphics g){ super.paintcomponent(g); int panelwidth = this.getwidth(); int panelheight = this.getheight(); g.drawroundrect(0, 0, panelwidth-1, panelheight-1, 10, 10); card = playingdeck.drawtopcard(); g.drawstring(card.tostring(), 75, 75); } } package uno; import uno.gameframe; public class gameapp{ /** * @param args */ public static void main(string[] args) { gameframe aboard = new gameframe(); aboard.setvisible(true); } } package uno; public class playingcard { private int number; private string color; public int getnumber() { return number; } public string getcolor(){ return color; } public playingcard(playingcard card){ number = card.getnumber(); color = new string(card.getcolor()); } public playingcard(int cardnumber, string cardcolor){ number = cardnumber; color = cardcolor; } } package uno; public class playingcard { private int number; private string color; public int getnumber() { return number; } public string getcolor(){ return color; } public playingcard(playingcard card){ number = card.getnumber(); color = new string(card.getcolor()); } public playingcard(int cardnumber, string cardcolor){ number = cardnumber; color = cardcolor; } } package uno; import java.util.collections; import java.util.vector; public class standarddeck { protected vector<playingcard> cardsvector; public standarddeck(){ cardsvector = new vector<playingcard>(); string[] colors = {"blue","red","yellow","green"}; for(string color : colors){ for(int i=1;i<=9;i++){ cardsvector.add(new playingcard(i,color)); cardsvector.add(new playingcard(i,color)); } cardsvector.add(new playingcard(0,color)); for(int i=-3;i<0;i++){ cardsvector.add(new playingcard(i,color)); cardsvector.add(new playingcard(i,color)); } } for(int i=0;i<4;i++){ cardsvector.add(new playingcard(-4,"black")); cardsvector.add(new playingcard(-5,"black")); } } public int getsize(){ return cardsvector.size(); } public standarddeck reset(){ standarddeck resetdeck = new standarddeck(); return resetdeck; } public void shuffle(){ collections.shuffle(cardsvector); } public playingcard drawtopcard(){ return new playingcard(cardsvector.remove(cardsvector.size()-1)); } } package uno; import uno.iplayingcard; public interface ideck { public void reset(); public void shuffle(); public iplayingcard drawtopcard(playingcard card); } package uno; import uno.playingcard; public interface iplayingcard extends comparable<playingcard>{ public string getcolor(); public string getnumber(); }
thanks help!
it's printing uno.playingcard@56609959
-- last part of being truncated though, you're seeing "uno" @ edge of panel.
you missing tostring()
implementation in playingcard class.
public void paintcomponent(graphics g) { super.paintcomponent(g); int panelwidth = this.getwidth(); int panelheight = this.getheight(); g.drawroundrect(0, 0, panelwidth - 1, panelheight - 1, 10, 10); card = playingdeck.drawtopcard(); g.drawstring(card.tostring(), 75, 75); // you're calling tostring() here... }
since don't have implemented, it's calling default object.tostring() method. meant add playingcard class?
public string tostring() { return ""+number; }
Comments
Post a Comment