Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package next;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JButton;
- import labyyy.labyyy;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class test extends JFrame {
- private JPanel contentPane;
- public static final int CANVAS_WIDTH = 200;
- public static final int CANVAS_HEIGHT = 200;
- public static final Color LINE_COLOR = Color.black;
- public static final Color CANVAS_BACKGROUND = Color.white;
- private int x1 =0;
- private int y1 = 0;
- private DrawCanvas canvas;
- /**
- * Launch the application.
- */
- class DrawCanvas extends JPanel {
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- setBackground(CANVAS_BACKGROUND);
- g.setColor(LINE_COLOR);
- if(x1>=180)
- { x1=180;
- }
- if(x1<0)
- x1=0;
- if(y1>=180){
- y1=180;
- }
- if(y1<0)
- y1=0;
- g.fillOval(x1, y1, 20, 20);
- }
- }
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- test frame = new test();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public test() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JButton btnNewButton = new JButton("L");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- x1 -= 5;
- repaint();
- }
- });
- btnNewButton.setBounds(10, 227, 89, 23);
- contentPane.add(btnNewButton);
- JButton btnNewButton_1 = new JButton("R");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- x1 += 5;
- repaint();
- }
- });
- btnNewButton_1.setBounds(264, 227, 89, 23);
- contentPane.add(btnNewButton_1);
- DrawCanvas canvas = new DrawCanvas();
- canvas.setBounds(10, 10, 200, 200);
- contentPane.add(canvas);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement