Guest User

Untitled

a guest
Feb 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4.  
  5. public class MyFirstApplet extends Applet implements KeyListener, MouseListener {
  6. private Deck myDeck;
  7.  
  8. public void init() {
  9. addKeyListener(this);
  10. addMouseListener(this);
  11. //Do any initialization here instead of using a constructor
  12. myDeck = new Deck();
  13. }
  14.  
  15. public void update(Graphics g) {
  16. paint(g);
  17. }
  18.  
  19. public void paint(Graphics g) {
  20. int w = getSize().width; //the width of the window
  21. int h = getSize().height; //the height of the window
  22.  
  23. g.setColor(new Color(0,0,255)); //set the color to blue
  24. g.fillRect(0, 0, w, h); //draw a filled rectangle for the whole window
  25. g.setColor(new Color(255,255,0)); //set the color to yellow
  26. g.fillRect(5, 5, w-10, h-10); //fill the window with yellow, except the edges
  27.  
  28. for(int i = 0; i < 52; i++) {
  29. g.setColor(new Color(128,128,255)); //light blue
  30. int x = i * 40 + 10;
  31. int y = i * 40 + 10;
  32. g.fillRect(x, y, 120, 60); //light blue rectangle for the i^th card
  33. g.setColor(new Color(0,0,0)); //black
  34. g.drawRect(x, y, 120, 60); //a border for the card
  35.  
  36. Card c = myDeck.cardAt(i); //get the i^th card in the deck
  37. g.drawString(c.toString(), x+5, y+30); //print out the name of the card
  38. }
  39.  
  40. //draw a big red X through everything
  41. g.setColor(Color.RED);
  42. g.drawLine(0, 0, w, h);
  43. g.drawLine(1, 0, w+1, h);
  44. g.drawLine(0, h, w, 0);
  45. g.drawLine(1, h, w+1, 0);
  46. }
  47.  
  48. public void keyPressed(KeyEvent k) {
  49. //this method is called by Java when a key is pressed
  50. }
  51.  
  52. public void keyReleased(KeyEvent k) {
  53. //this method is called by Java when a key is released
  54. }
  55.  
  56. public void keyTyped(KeyEvent k) {
  57. //this method is called by Java when a key is typed
  58. System.out.println("Key was pressed: "+k.getKeyChar());
  59. }
  60.  
  61. public void mouseClicked(MouseEvent k) {
  62. //this method is called by Java when the mouse is clicked
  63. System.out.println("Mouse clicked at: (x,y) = "+k.getX()+" "+k.getY());
  64. }
  65.  
  66. public void mouseEntered(MouseEvent k) {
  67. //this method is called by Java when the mouse enters the applet window
  68. }
  69.  
  70. public void mouseExited(MouseEvent k) {
  71. //this method is called by Java when the mouse exits the applet window
  72. }
  73.  
  74. public void mousePressed(MouseEvent k) {
  75. //this method is called by Java when a mouse button is pressed down
  76. }
  77.  
  78. public void mouseReleased(MouseEvent k) {
  79. //this method is called by Java when a mouse button is released
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment