0% found this document useful (0 votes)
5 views

Java program for shapes (BCA)

The ShapeMover class is a Java Swing application that allows users to move a rectangle and a circle on the screen using keyboard inputs. The rectangle is controlled with the arrow keys, while the circle is moved using the 'W', 'A', 'S', and 'D' keys. The application sets up a JFrame and listens for key events to update the positions of the shapes accordingly.

Uploaded by

wayseaofficial
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java program for shapes (BCA)

The ShapeMover class is a Java Swing application that allows users to move a rectangle and a circle on the screen using keyboard inputs. The rectangle is controlled with the arrow keys, while the circle is moved using the 'W', 'A', 'S', and 'D' keys. The application sets up a JFrame and listens for key events to update the positions of the shapes accordingly.

Uploaded by

wayseaofficial
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;

public class ShapeMover extends JPanel implements KeyListener {

// Initial position of the shapes


private int x = 150, y = 150; // Starting position for the rectangle
private int circleX = 250, circleY = 150; // Starting position for the circle

// Shape size
private final int RECT_WIDTH = 50, RECT_HEIGHT = 30;
private final int CIRCLE_RADIUS = 20;

public ShapeMover() {
// Set focusable so that it can listen to key events
setFocusable(true);
addKeyListener(this);
}

// Paint method to draw the shapes


@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

// Draw a rectangle
g.setColor(Color.RED);
g.fillRect(x, y, RECT_WIDTH, RECT_HEIGHT);

// Draw a circle
g.setColor(Color.BLUE);
g.fillOval(circleX, circleY, CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2);
}

// Handle key events (arrow keys)


@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

// Move the rectangle with arrow keys


if (key == KeyEvent.VK_LEFT) {
x -= 10; // Move left
} else if (key == KeyEvent.VK_RIGHT) {
x += 10; // Move right
} else if (key == KeyEvent.VK_UP) {
y -= 10; // Move up
} else if (key == KeyEvent.VK_DOWN) {
y += 10; // Move down
}

// Move the circle with arrow keys


if (key == KeyEvent.VK_A) { // 'A' for moving the circle left
circleX -= 10;
} else if (key == KeyEvent.VK_D) { // 'D' for moving the circle right
circleX += 10;
} else if (key == KeyEvent.VK_W) { // 'W' for moving the circle up
circleY -= 10;
} else if (key == KeyEvent.VK_S) { // 'S' for moving the circle down
circleY += 10;
}

// Repaint the panel to reflect the new positions of the shapes


repaint();
}

@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}

// Main method to create the frame


public static void main(String[] args) {
JFrame frame = new JFrame("Move Shapes with Arrow Keys");
ShapeMover panel = new ShapeMover();

frame.setSize(600, 400); // Set the size of the window


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel); // Add the panel to the frame
frame.setVisible(true); // Make the frame visible
}
}

You might also like