Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.Applet;
- import java.awt.event.*;
- import java.awt.*;
- public class MyFirstApplet extends Applet implements KeyListener, MouseListener {
- private Deck myDeck;
- public void init() {
- addKeyListener(this);
- addMouseListener(this);
- //Do any initialization here instead of using a constructor
- myDeck = new Deck();
- }
- public void update(Graphics g) {
- paint(g);
- }
- public void paint(Graphics g) {
- int w = getSize().width; //the width of the window
- int h = getSize().height; //the height of the window
- g.setColor(new Color(0,0,255)); //set the color to blue
- g.fillRect(0, 0, w, h); //draw a filled rectangle for the whole window
- g.setColor(new Color(255,255,0)); //set the color to yellow
- g.fillRect(5, 5, w-10, h-10); //fill the window with yellow, except the edges
- for(int i = 0; i < 52; i++) {
- g.setColor(new Color(128,128,255)); //light blue
- int x = i * 40 + 10;
- int y = i * 40 + 10;
- g.fillRect(x, y, 120, 60); //light blue rectangle for the i^th card
- g.setColor(new Color(0,0,0)); //black
- g.drawRect(x, y, 120, 60); //a border for the card
- Card c = myDeck.cardAt(i); //get the i^th card in the deck
- g.drawString(c.toString(), x+5, y+30); //print out the name of the card
- }
- //draw a big red X through everything
- g.setColor(Color.RED);
- g.drawLine(0, 0, w, h);
- g.drawLine(1, 0, w+1, h);
- g.drawLine(0, h, w, 0);
- g.drawLine(1, h, w+1, 0);
- }
- public void keyPressed(KeyEvent k) {
- //this method is called by Java when a key is pressed
- }
- public void keyReleased(KeyEvent k) {
- //this method is called by Java when a key is released
- }
- public void keyTyped(KeyEvent k) {
- //this method is called by Java when a key is typed
- System.out.println("Key was pressed: "+k.getKeyChar());
- }
- public void mouseClicked(MouseEvent k) {
- //this method is called by Java when the mouse is clicked
- System.out.println("Mouse clicked at: (x,y) = "+k.getX()+" "+k.getY());
- }
- public void mouseEntered(MouseEvent k) {
- //this method is called by Java when the mouse enters the applet window
- }
- public void mouseExited(MouseEvent k) {
- //this method is called by Java when the mouse exits the applet window
- }
- public void mousePressed(MouseEvent k) {
- //this method is called by Java when a mouse button is pressed down
- }
- public void mouseReleased(MouseEvent k) {
- //this method is called by Java when a mouse button is released
- }
- }
Add Comment
Please, Sign In to add comment