Yehonatan

JFrame and JPanel example

Dec 10th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. Create file MainWindow.java
  3. //////////////////////////////////////////////////////////////////////////////////////
  4. public class MainWindow
  5. {
  6.  
  7.     import javax.swing.JFrame;
  8.    
  9.     public static void main(String args[])
  10.     {
  11.    
  12.         JFrame f = new JFrame("This is a basic app");
  13.         //Adding the JPanel to the JFrame
  14.         f.add(new PaintSheet());
  15.         //Resize JFrame relative to JPanel size
  16.         f.frame.pack();
  17.         //Set position in the middle
  18.         f.frame.setLocationRelativeTo(null);
  19.         //Close if pressing X
  20.         f.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  21.         //Show the window
  22.         f.frame.setVisible(true);
  23.        
  24.     }
  25.    
  26. }
  27.  
  28. ///////////////////////////////////////////////////////////////////////////////////////
  29. Create file PaintSheet.java
  30. //////////////////////////////////////////////////////////////////////////////////////
  31.  
  32. import java.awt.Graphics;
  33. import java.awt.Graphics2D;
  34. import java.awt.event.KeyEvent;
  35. import java.awt.event.KeyListener;
  36. import java.awt.Color;
  37. import javax.swing.JPanel;
  38.  
  39. public class PaintSheet extends JPanel implements KeyListener{
  40.    
  41.     //Set the JPanel's size to 400x400
  42.     private double width = 400, height = 400;
  43.     //First x && y position
  44.     private double x = 200,y = 200;
  45.    
  46.     private double dx, dy;
  47.    
  48.     public PaintSheet()
  49.     {
  50.         //Set the JPanel size
  51.         this.setSize((int)this.width, (int)this.height);
  52.         //Add keyListener
  53.         this.addKeyListener(this);
  54.         //IMPORTANT - THIS LINE ALLOWS JPANEL TO GET KEY PRESS
  55.         this.setFocusable(true);
  56.     }
  57.    
  58.     @Override
  59.     public void paintComponent(Graphics g)
  60.     {
  61.         //Request focus to allow key pressing
  62.         super.requestFocus();
  63.         super.paintComponent(g);
  64.         Graphics2D g2d = (Graphics2D) g;
  65.        
  66.         g2d.setColor(Color.black);
  67.         g2d.fillOval((int)this.x, (int)this.y, 40, 40);
  68.        
  69.         //Sleep the thread to avoid super-moving
  70.         try {
  71.             Thread.sleep(10);
  72.         } catch (InterruptedException ex) { }
  73.        
  74.         repaint();
  75.     }
  76.    
  77.     public void keyReleased(KeyEvent e){}
  78.     public void keyTyped(KeyEvent e){}        
  79.            
  80.     public void keyPressed(KeyEvent e)
  81.     {
  82.         if(e.getKeyCode() == KeyEvent.VK_RIGHT)
  83.             this.dx = 1;
  84.         if(e.getKeyCode() == KeyEvent.VK_LEFT)
  85.             this.dx = -1;
  86.         if(e.getKeyCode() == KeyEvent.VK_DOWN)
  87.             this.dy = 1;
  88.         if(e.getKeyCode() == KeyEvent.VK_UP)  
  89.             this.dy = -1;
  90.        
  91.         this.x += this.dx;
  92.         this.y += this.dy;
  93.        
  94.         this.dx = 0;
  95.         this.dy = 0;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment