Computer Science, asked by krimajohnShbarbi, 1 year ago

Steps to Make Game in JAVA
How we can make game in Java?

Answers

Answered by pranav2122
0
import javax.swing. JFrame; import javax.swing.Window Constants; public class FrameDemo{ public static void main(String args[]){ JFrame myFrame = new JFrame("Sample Frame"); myFrame.setSize(300,400); myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); myFrame.setVisible(true); } }

With this code, you will be able to control the window of your game. You can minimize, maximize, and change the size settings to your preference. Apart from keeping things together, the window serves as a command center for the code that will manage your application's user interface.

Animation

Of course, a game without animation would be pretty boring. Here’s an example of how you can code a simple moving object for your game. If you’re thinking about doing a sports game like tennis or a simple poker table game with card animation, this will get you started.

First, you need to determine the starting position of the ball (x and y). Then, to make it move, you have to modify the position with each shift.

package com.edu4java.samplegame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") public class Game extends JPanel { int x = 0; int y = 0; private void moveBall() { x = x + 1; y = y + 1; } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.fillOval(x, y, 30, 30); } public static void main(String[] args) throws InterruptedException { JFrame frame = new JFrame("Sample Frame"); Game game = new Game(); frame.add(game); frame.setSize(300, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); while (true) { game.moveBall(); game.repaint(); Thread.sleep(10); } } }

If you already know the basics of coding in JAVA U CAN MAKE IT EASILY

Similar questions