Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package next;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.EventQueue;
  7. import java.awt.Graphics;
  8. import java.awt.event.KeyAdapter;
  9. import java.awt.event.KeyEvent;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.border.EmptyBorder;
  14. import javax.swing.JButton;
  15.  
  16. import labyyy.labyyy;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ActionEvent;
  19.  
  20. public class test extends JFrame {
  21.  
  22.     private JPanel contentPane;
  23.     public static final int CANVAS_WIDTH = 200;
  24.     public static final int CANVAS_HEIGHT = 200;
  25.     public static final Color LINE_COLOR = Color.black;
  26.       public static final Color CANVAS_BACKGROUND = Color.white;
  27.       private int x1 =0;
  28.        private int y1 = 0;
  29.      
  30.     private DrawCanvas canvas;
  31.  
  32.     /**
  33.      * Launch the application.
  34.      */
  35.    
  36.        class DrawCanvas extends JPanel {
  37.               @Override
  38.               public void paintComponent(Graphics g) {
  39.                  super.paintComponent(g);
  40.                  setBackground(CANVAS_BACKGROUND);
  41.                  g.setColor(LINE_COLOR);
  42.                      
  43.                      if(x1>=180)
  44.                      { x1=180;
  45.                         }
  46.                      if(x1<0)
  47.                          x1=0;
  48.                      if(y1>=180){
  49.                          y1=180;
  50.                         }
  51.                      if(y1<0)
  52.                          y1=0;
  53.                      g.fillOval(x1, y1, 20, 20);
  54.                  
  55.  
  56.                  
  57.               }
  58.        }
  59.     public static void main(String[] args) {
  60.         EventQueue.invokeLater(new Runnable() {
  61.             public void run() {
  62.                 try {
  63.                     test frame = new test();
  64.                     frame.setVisible(true);
  65.                 } catch (Exception e) {
  66.                     e.printStackTrace();
  67.                 }
  68.             }
  69.         });
  70.     }
  71.  
  72.     /**
  73.      * Create the frame.
  74.      */
  75.     public test() {
  76.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  77.         setBounds(100, 100, 450, 300);
  78.         contentPane = new JPanel();
  79.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  80.         setContentPane(contentPane);
  81.         contentPane.setLayout(null);
  82.        
  83.         JButton btnNewButton = new JButton("L");
  84.         btnNewButton.addActionListener(new ActionListener() {
  85.             public void actionPerformed(ActionEvent arg0) {
  86.                 x1 -= 5;
  87.                 repaint();
  88.             }
  89.         });
  90.         btnNewButton.setBounds(10, 227, 89, 23);
  91.         contentPane.add(btnNewButton);
  92.        
  93.         JButton btnNewButton_1 = new JButton("R");
  94.         btnNewButton_1.addActionListener(new ActionListener() {
  95.             public void actionPerformed(ActionEvent arg0) {
  96.                 x1 += 5;
  97.                 repaint();
  98.             }
  99.         });
  100.         btnNewButton_1.setBounds(264, 227, 89, 23);
  101.         contentPane.add(btnNewButton_1);
  102.        
  103.          DrawCanvas canvas = new DrawCanvas();
  104.          canvas.setBounds(10, 10, 200, 200);
  105.         contentPane.add(canvas);
  106.          
  107.        
  108. }
  109.    
  110.    
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement