HELP WITH .JAR FILE WITH Two PARAMETER How do i take a cde liek the one given be
ID: 3681975 • Letter: H
Question
HELP WITH .JAR FILE WITH Two PARAMETER
How do i take a cde liek the one given below, to run in a.jar file with two parameters,
Please tell me the steps you did .
CODE IS FOR A CONEECT 4 GAME
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Connect4 {
public static void main(String[] args) {
Connect4JFrame newF = new Connect4JFrame();
newF.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
newF.setVisible(true);
}
}
class Connect4JFrame extends JFrame implements ActionListener {
private static final long SV1 = 1L;
private Button buttotn1, buttotn2, buttotn3, buttotn4, buttotn5, buttotn6, buttotn7;
private Label lSpacer;
MenuItem neww, exitt, blackk, bluee;
int[][] theeArrayArray;
boolean end=false;
boolean startTHEGAME;
public static final int BLANK = 0;
public static final int BLACK = 1;
public static final int BROWN = 2;
public static final int theROW = 6; // 6 rows
public static final int theColumn = 6; // 7 columns
public static final String theSpace = " ";
int activeColour = BLACK;
public Connect4JFrame() {
setTitle("CS 151 CONNECT 4 HOMEWORK 3");
MenuBar mbar = new MenuBar();
Menu fileMenu = new Menu("File");
exitt = new MenuItem("Exit");
exitt.addActionListener(this);
fileMenu.add(exitt);
mbar.add(fileMenu);
Menu optMenu = new Menu("Options");
mbar.add(optMenu);
setMenuBar(mbar);
Panel panel = new Panel();
buttotn1 = new Button("one");
buttotn1.addActionListener(this);
panel.add(buttotn1);
lSpacer = new Label(theSpace);
panel.add(lSpacer);
buttotn2 = new Button("two");
buttotn2.addActionListener(this);
panel.add(buttotn2);
lSpacer = new Label(theSpace);
panel.add(lSpacer);
buttotn3 = new Button("three");
buttotn3.addActionListener(this);
panel.add(buttotn3);
lSpacer = new Label(theSpace);
panel.add(lSpacer);
buttotn4 = new Button("four");
buttotn4.addActionListener(this);
panel.add(buttotn4);
lSpacer = new Label(theSpace);
panel.add(lSpacer);
buttotn5 = new Button("five");
buttotn5.addActionListener(this);
panel.add(buttotn5);
lSpacer = new Label(theSpace);
panel.add(lSpacer);
buttotn6 = new Button("six");
buttotn6.addActionListener(this);
panel.add(buttotn6);
lSpacer = new Label(theSpace);
panel.add(lSpacer);
add(panel, BorderLayout.NORTH);
initialize();
// Set to a reasonable size.
setSize(1024, 768);
} // Connect4
public void paint(Graphics g) {
g.setColor(Color.ORANGE);
g.fillRect(110, 50, 100+100*theColumn, 100+100*theROW);
for (int row=0; row<theROW; row++)
for (int col=0; col<theColumn; col++) {
if (theeArrayArray[row][col]==BLANK) g.setColor(Color.RED);
if (theeArrayArray[row][col]==BLACK) g.setColor(Color.BLACK);
if (theeArrayArray[row][col]==BROWN) g.setColor(Color.BLUE);
g.fillOval(160+100*col, 100+100*row, 100, 100);
}
check4(g);
} // paint
public void initialize() {
theeArrayArray = new int [theROW][theColumn] ;
for (int row = 0; row<theROW; row++)
for (int col = 0; col<theColumn; col++)
theeArrayArray [row] [col] = BLANK;
startTHEGAME = false;
} // initialize
public void PutCircle(int z) {
// put a disk on top of column n
// if game is won, do nothing
if (end) return;
startTHEGAME = true;
int row;
z -- ;
for (row=0; row<theROW; row++ )
if (theeArrayArray [row] [z ] > 0 ) break;
if (row>0) {
theeArrayArray [ -- row] [z] = activeColour;
if (activeColour == BLACK)
activeColour = BROWN;
else
activeColour = BLACK;
repaint();
}
}
public void theWinnerIS(Graphics f, int z) {
f.setColor(Color.BLACK);
f.setFont(new Font("Courier", Font.ITALIC, 50));
if ( z == BLACK )
f.drawString("BLACK Is The Winner", 2, 200);
else
f.drawString("BLUE Is The Winner!", 2, 200);
end=true;
}
public void check4(Graphics g) {
// see if there are 4 disks in a row: horizontal, vertical or diagonal
// horizontal rows
for (int row=0; row<theROW; row++) {
for (int col=0; col<theColumn-3; col++) {
int curr = theeArrayArray[row][col];
if (curr>0
&& curr == theeArrayArray[row][col+1]
&& curr == theeArrayArray[row][col+2]
&& curr == theeArrayArray[row][col+3]) {
theWinnerIS(g, theeArrayArray[row][col]);
}
}
}
// vertical columns
for (int col=0; col<theColumn; col++) {
for (int row=0; row<theROW-3; row++) {
int curr = theeArrayArray[row][col];
if (curr>0
&& curr == theeArrayArray[row+1][col]
&& curr == theeArrayArray[row+2][col]
&& curr == theeArrayArray[row+3][col])
theWinnerIS(g, theeArrayArray[row][col]);
}
}
// diagonal lower left to upper right
for (int row=0; row<theROW-3; row++) {
for (int col=0; col<theColumn-3; col++) {
int curr = theeArrayArray[row][col];
if (curr>0
&& curr == theeArrayArray[row+1][col+1]
&& curr == theeArrayArray[row+2][col+2]
&& curr == theeArrayArray[row+3][col+3])
theWinnerIS(g, theeArrayArray[row][col]);
}
}
// diagonal upper left to lower right
for (int row=theROW-1; row>=3; row--) {
for (int col=0; col<theColumn-3; col++) {
int curr = theeArrayArray[row][col];
if (curr>0
&& curr == theeArrayArray[row-1][col+1]
&& curr == theeArrayArray[row-2][col+2]
&& curr == theeArrayArray[row-3][col+3])
theWinnerIS(g, theeArrayArray[row][col]);
}
}
} // end check4
public void actionPerformed(ActionEvent z) {
if (z.getSource() == buttotn1)
PutCircle(1);
else if (z.getSource() == buttotn2)
PutCircle(2);
else if (z.getSource() == buttotn3)
PutCircle(3);
else if (z.getSource() == buttotn4)
PutCircle(4);
else if (z.getSource() == buttotn5)
PutCircle(5);
else if (z.getSource() == buttotn6)
PutCircle(6);
else if (z.getSource() == neww) {
end=false;
initialize();
repaint();
}
else if (z.getSource() == exitt) {
System.exit(0);
}
else if (z.getSource() == blackk) {
// don't change colour to play in middle of game
if (!startTHEGAME) activeColour=BLACK;
}
else if (z.getSource() == bluee) {
if (!startTHEGAME) activeColour=BROWN;
}
} // end ActionPerformed
} // class
Explanation / Answer
Parameters can be taken into the Main class (Connect4.class) straight away using the parameters
example: java -jar Connect4.jar 6 6
These values can be accessed into the main function using the args variable of main function.
If those parameters are to be used int the class Connect4JFrame, then they have to be passed as parameters to the Connect4JFrame class through a parameterized constructor
The constructor has to be redefined as a parameterized constructor like -> Connect4JFrame(int x, int y). From there, those values can be used wherever u needed them in the class Connect4Jframe. The corrected code snippets are given as follows.
The corrected code is shown below.
public class Connect4 {
public static void main(String[] args) {
int s1 = Integer.parseInt(args[0]);
int s2 = Integer.parseInt(args[1]);
System.out.println("FIRST STRING : " + s1 + " SECOND STRING : " + s2);
Connect4JFrame newF = new Connect4JFrame(s1,s2);
newF.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
newF.setVisible(true);
}
}
class Connect4JFrame extends JFrame implements ActionListener {
private static final long SV1 = 1L;
private Button buttotn1, buttotn2, buttotn3, buttotn4, buttotn5, buttotn6, buttotn7;
private Label lSpacer;
MenuItem neww, exitt, blackk, bluee;
int[][] theeArrayArray;
boolean end=false;
boolean startTHEGAME;
public static final int BLANK = 0;
public static final int BLACK = 1;
public static final int BROWN = 2;
public static final int theROW = 6; // 6 rows
public static final int theColumn = 6; // 7 columns
public static final String theSpace = " ";
int activeColour = BLACK;
public Connect4JFrame(int x, int y) {
setTitle("CS 151 CONNECT 4 HOMEWORK 3");
MenuBar mbar = new MenuBar();
---
....
....
...
....
..
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.