swing - Java Code - Solitaire -
i've created solitaire game in java.
my question is: how can fill objects (hearts, spades, diamonds, club) on every card black or red color?
here code have now:
// draw card public void draw (graphics g, int x, int y) { // clear rectangle, draw border g.clearrect(x, y, width, height); g.setcolor(color.black); g.drawrect(x, y, width, height); // draw body of card if (faceup()) { if (color() == red) g.setcolor(color.red); else g.setcolor(color.black); g.drawstring(names[rank()], x+3, y+15); if (suit() == heart) { g.drawline(x+25, y+30, x+35, y+20); g.drawline(x+35, y+20, x+45, y+30); g.drawline(x+45, y+30, x+25, y+60); g.drawline(x+25, y+60, x+5, y+30); g.drawline(x+5, y+30, x+15, y+20); g.drawline(x+15, y+20, x+25, y+30); // g.fill(color.red); } else if (suit() == spade) { g.drawline(x+25, y+20, x+40, y+50); g.drawline(x+40, y+50, x+10, y+50); g.drawline(x+10, y+50, x+25, y+20); g.drawline(x+23, y+45, x+20, y+60); g.drawline(x+20, y+60, x+30, y+60); g.drawline(x+30, y+60, x+27, y+45); } else if (suit() == diamond) { g.drawline(x+25, y+20, x+40, y+40); g.drawline(x+40, y+40, x+25, y+60); g.drawline(x+25, y+60, x+10, y+40); g.drawline(x+10, y+40, x+25, y+20); } else if (suit() == club) { g.drawoval(x+20, y+25, 10, 10); g.drawoval(x+25, y+35, 10, 10); g.drawoval(x+15, y+35, 10, 10); g.drawline(x+23, y+45, x+20, y+55); g.drawline(x+20, y+55, x+30, y+55); g.drawline(x+30, y+55, x+27, y+45); } } else // face down { g.setcolor(color.black); g.drawline(x+15, y+5, x+15, y+65); g.drawline(x+35, y+5, x+35, y+65); g.drawline(x+5, y+20, x+45, y+20); g.drawline(x+5, y+35, x+45, y+35); g.drawline(x+5, y+50, x+45, y+50); } } }
i took snippet, , made snippet of own. snippet fill of heart suit, using graphics.fillpolygon
. i've commented out old drawing of lines in snippet, can compare did. other cards i'll leave you.
import java.awt.*; import javax.swing.*; public class cardframe { enum cardcolor{red,black}; enum cardsuit{heart,diamond,spade,club} public static void main( string[] args ) { swingutilities.invokelater(new runnable() { @override public void run() { jpanel carddisplay = new jpanel() { @override public dimension getpreferredsize() { return new dimension(50,100); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); draw(g,0,0); } private int width = 50; private int height = 80; private boolean faceup() { return true; } private cardcolor color() { return cardcolor.red; } private cardsuit suit() { return cardsuit.heart; } private int rank() { return 0; } private string[] names = {"1","2","3","4","5","6","7","8","9","10","j","q","k","a"}; private void draw(graphics g, int x, int y) { // clear rectangle, draw border g.clearrect(x, y, width, height); g.setcolor(color.black); g.drawrect(x, y, width, height); // draw body of card if (faceup()) { if (color() == cardcolor.red) g.setcolor(color.red); else g.setcolor(color.black); g.drawstring(names[rank()], x + 3, y + 15); if (suit() == cardsuit.heart) { // g.drawline(x + 25, y + 30, x + 35, y + 20); // g.drawline(x + 35, y + 20, x + 45, y + 30); // g.drawline(x + 45, y + 30, x + 25, y + 60); // g.drawline(x + 25, y + 60, x + 5, y + 30); // g.drawline(x + 5, y + 30, x + 15, y + 20); // g.drawline(x + 15, y + 20, x + 25, y + 30); int[] xpoints = new int[]{x + 5,x + 15,x + 25,x + 35,x + 45,x + 25}; int[] ypoints = new int[]{y + 30,y + 20,y + 30,y + 20,y + 30,y + 60}; g.fillpolygon(xpoints, ypoints, 6); } else if (suit() == cardsuit.spade) { // ... } else if (suit() == cardsuit.diamond) { // ... } else if (suit() == cardsuit.club) { // } } else // face down { // ... } } }; jframe frm = new jframe(); frm.setcontentpane(carddisplay); frm.pack(); frm.setvisible(true); } }); } }
result:
Comments
Post a Comment