-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #599 from PranikaBaby/qrgen
Added QR Generator using given string
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
|
||
public class ChessGame { | ||
private JFrame frame; | ||
private JButton[][] boardButtons; | ||
private String currentPlayer = "White"; | ||
|
||
public ChessGame() { | ||
frame = new JFrame("Multiplayer Chess Game"); | ||
boardButtons = new JButton[8][8]; | ||
frame.setLayout(new GridLayout(8, 8)); | ||
initializeBoard(); | ||
frame.setSize(600, 600); | ||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
frame.setVisible(true); | ||
} | ||
|
||
// Initialize the board with buttons | ||
private void initializeBoard() { | ||
for (int row = 0; row < 8; row++) { | ||
for (int col = 0; col < 8; col++) { | ||
JButton button = new JButton(); | ||
button.setBackground((row + col) % 2 == 0 ? Color.WHITE : Color.GRAY); | ||
button.setOpaque(true); | ||
button.setBorderPainted(false); | ||
boardButtons[row][col] = button; | ||
button.addActionListener(new ButtonClickListener(row, col)); | ||
frame.add(button); | ||
} | ||
} | ||
setupInitialPieces(); | ||
} | ||
|
||
// Set up the initial positions of the pieces | ||
private void setupInitialPieces() { | ||
// For simplicity, setting up only pawns. Add other pieces as needed. | ||
for (int col = 0; col < 8; col++) { | ||
boardButtons[1][col].setText("P"); // Black pawns | ||
boardButtons[6][col].setText("P"); // White pawns | ||
} | ||
// Add other pieces (King, Queen, Rook, Knight, Bishop) here | ||
} | ||
|
||
// Handle button click events | ||
private class ButtonClickListener implements ActionListener { | ||
private int row; | ||
private int col; | ||
|
||
public ButtonClickListener(int row, int col) { | ||
this.row = row; | ||
this.col = col; | ||
} | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
JButton clickedButton = boardButtons[row][col]; | ||
if (clickedButton.getText().isEmpty()) { | ||
System.out.println(currentPlayer + " clicked on an empty square at (" + row + ", " + col + ")"); | ||
} else { | ||
System.out.println(currentPlayer + " clicked on a piece at (" + row + ", " + col + ")"); | ||
} | ||
switchPlayer(); | ||
} | ||
} | ||
|
||
// Switch the current player | ||
private void switchPlayer() { | ||
currentPlayer = currentPlayer.equals("White") ? "Black" : "White"; | ||
System.out.println("Current player: " + currentPlayer); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SwingUtilities.invokeLater(ChessGame::new); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import com.google.zxing.BarcodeFormat; | ||
import com.google.zxing.EncodeHintType; | ||
import com.google.zxing.qrcode.QRCodeWriter; | ||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; | ||
import com.google.zxing.common.BitMatrix; | ||
import com.google.zxing.client.j2se.MatrixToImageWriter; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class QRCodeGenerator { | ||
public static void main(String[] args) { | ||
String text = "Hello, QR Code!"; // Text to encode in QR code | ||
String filePath = "QRCode.png"; // Output file path | ||
int width = 300; // QR code image width | ||
int height = 300; // QR code image height | ||
|
||
// Create a map for encoding hints | ||
Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>(); | ||
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); | ||
|
||
try { | ||
// Generate the QR code matrix | ||
QRCodeWriter qrCodeWriter = new QRCodeWriter(); | ||
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints); | ||
|
||
// Save the QR code image | ||
Path path = FileSystems.getDefault().getPath(filePath); | ||
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); | ||
|
||
System.out.println("QR Code generated successfully: " + filePath); | ||
} catch (Exception e) { | ||
System.out.println("Error while generating QR Code: " + e.getMessage()); | ||
} | ||
} | ||
} |