Skip to content

Commit 427d455

Browse files
authoredOct 29, 2024··
Merge pull request #599 from PranikaBaby/qrgen
Added QR Generator using given string
2 parents 5c20acb + 133892f commit 427d455

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
 

‎multiplayerchess.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
public class ChessGame {
7+
private JFrame frame;
8+
private JButton[][] boardButtons;
9+
private String currentPlayer = "White";
10+
11+
public ChessGame() {
12+
frame = new JFrame("Multiplayer Chess Game");
13+
boardButtons = new JButton[8][8];
14+
frame.setLayout(new GridLayout(8, 8));
15+
initializeBoard();
16+
frame.setSize(600, 600);
17+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18+
frame.setVisible(true);
19+
}
20+
21+
// Initialize the board with buttons
22+
private void initializeBoard() {
23+
for (int row = 0; row < 8; row++) {
24+
for (int col = 0; col < 8; col++) {
25+
JButton button = new JButton();
26+
button.setBackground((row + col) % 2 == 0 ? Color.WHITE : Color.GRAY);
27+
button.setOpaque(true);
28+
button.setBorderPainted(false);
29+
boardButtons[row][col] = button;
30+
button.addActionListener(new ButtonClickListener(row, col));
31+
frame.add(button);
32+
}
33+
}
34+
setupInitialPieces();
35+
}
36+
37+
// Set up the initial positions of the pieces
38+
private void setupInitialPieces() {
39+
// For simplicity, setting up only pawns. Add other pieces as needed.
40+
for (int col = 0; col < 8; col++) {
41+
boardButtons[1][col].setText("P"); // Black pawns
42+
boardButtons[6][col].setText("P"); // White pawns
43+
}
44+
// Add other pieces (King, Queen, Rook, Knight, Bishop) here
45+
}
46+
47+
// Handle button click events
48+
private class ButtonClickListener implements ActionListener {
49+
private int row;
50+
private int col;
51+
52+
public ButtonClickListener(int row, int col) {
53+
this.row = row;
54+
this.col = col;
55+
}
56+
57+
@Override
58+
public void actionPerformed(ActionEvent e) {
59+
JButton clickedButton = boardButtons[row][col];
60+
if (clickedButton.getText().isEmpty()) {
61+
System.out.println(currentPlayer + " clicked on an empty square at (" + row + ", " + col + ")");
62+
} else {
63+
System.out.println(currentPlayer + " clicked on a piece at (" + row + ", " + col + ")");
64+
}
65+
switchPlayer();
66+
}
67+
}
68+
69+
// Switch the current player
70+
private void switchPlayer() {
71+
currentPlayer = currentPlayer.equals("White") ? "Black" : "White";
72+
System.out.println("Current player: " + currentPlayer);
73+
}
74+
75+
public static void main(String[] args) {
76+
SwingUtilities.invokeLater(ChessGame::new);
77+
}
78+
}

‎qrgenerator.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import com.google.zxing.BarcodeFormat;
2+
import com.google.zxing.EncodeHintType;
3+
import com.google.zxing.qrcode.QRCodeWriter;
4+
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
5+
import com.google.zxing.common.BitMatrix;
6+
import com.google.zxing.client.j2se.MatrixToImageWriter;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.FileSystems;
11+
import java.nio.file.Path;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
public class QRCodeGenerator {
16+
public static void main(String[] args) {
17+
String text = "Hello, QR Code!"; // Text to encode in QR code
18+
String filePath = "QRCode.png"; // Output file path
19+
int width = 300; // QR code image width
20+
int height = 300; // QR code image height
21+
22+
// Create a map for encoding hints
23+
Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
24+
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
25+
26+
try {
27+
// Generate the QR code matrix
28+
QRCodeWriter qrCodeWriter = new QRCodeWriter();
29+
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
30+
31+
// Save the QR code image
32+
Path path = FileSystems.getDefault().getPath(filePath);
33+
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
34+
35+
System.out.println("QR Code generated successfully: " + filePath);
36+
} catch (Exception e) {
37+
System.out.println("Error while generating QR Code: " + e.getMessage());
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)
Please sign in to comment.