Reversi GUI question, A LOT of code in here!
okay, so I've been working on his code for a long time. A REAL long time, working on it bit by bit, and all I got left is the Reversi's GUI. Basically I want to use the Jbutton and have x represent black and o represent white. I'm getting stuck on how to go about it. Any ideas?
Here's alllll the code.
package com.Ozo.games.reversi;
* This utility class provides a number of static constants and
* methods that provide abstractions for player colors and moves.
* Colors and moves could be implemented as objects of separate
* classes, but instead we represent them as integers and provide
* the abstraction via these operations.
public class Reversi {
public static final int Black = 1;
public static final int White = 2;
public static final int Empty = 0;
* Gives the color of a player's opponent.
* @param color of this player.
* @return color of the opponent.
public static int playerOpposite(int color) {
if (color == Black) return White;
if (color == White) return Black;
throw new Error("Player must be Black or White");
* Compare two scores and determine whether the first is better than the second.
* Better for black means more positive. Better for white means more negative.
* @param colorAsking
* @param score1
* @param score2
* @return whether score1 is better than score2, as far as colorAsking is concerned.
public static boolean isBetterScore(int colorAsking, int score1, int score2) {
if (colorAsking == Black) return score1 > score2;
if (colorAsking == White) return score1 < score2;
throw new Error("Player must be Black or White");
* Encode a move from this position as an integer.
* A "pass" move is recorded as (-1,-1).
* @param pos the board this move is for.
* @param row the row at which the piece is placed.
* @param col the column at which the piece is placed.
* @return encoded move.
public static int newMove(ReversiPosition pos, int row, int col) {
return row * pos.ncols() + col;
* A "pass" move. I.e. no piece is placed.
public static int newMovePass(ReversiPosition pos) {
return newMove(pos, -1, -1);
* Find the row of an encoded move.
* @param pos the board this move is for.
* @param move
* @return the row.
public static int moveRow(ReversiPosition pos, int move) {
return move / pos.ncols();
* Find the column of an encoded move.
* @param pos the board this move is for.
* @param move
* @return the column.
public static int moveCol(ReversiPosition pos, int move) {
return move % pos.ncols();
package com.Ozo.games.reversi;
* Top-level driver for a Reversi game.
public class ReversiGame {
private ReversiPosition _pos;
private int _toMoveColor;
private ReversiTextIO _userInterface;
public ReversiGame(int nrows, int ncols) {
_pos = new ReversiPosition(nrows, ncols);
_toMoveColor = Reversi.Black;
_userInterface = null;
ReversiRules.setStartingPosition(_pos);
public void setUserInterface(ReversiTextIO ui) {
_userInterface = ui;
public ReversiPosition currentPosition() { return _pos; }
public int toMoveColor() { return _toMoveColor; }
public void getHumanMoveAndApplyIt() {
if (ReversiRules.countLegalMoves(_pos, _toMoveColor) != 0) {
int move = _userInterface.getMove(_pos, _toMoveColor);
_pos.applyMove(move, _toMoveColor);
else
_userInterface.message("You have no move. I get another turn.");
_toMoveColor = Reversi.playerOpposite(_toMoveColor);
public void getComputerMoveAndApplyIt() {
if (ReversiRules.countLegalMoves(_pos, _toMoveColor) != 0) {
int move = ReversiStrategy.findBestMove(_pos, _toMoveColor);
_pos.applyMove(move, _toMoveColor);
else
_userInterface.message("I have no move. You get another turn.");
_toMoveColor = Reversi.playerOpposite(_toMoveColor);
public void play() {
_userInterface.output(_pos);
for (;;) {
if (ReversiRules.isGameOver(_pos, _toMoveColor)) { gameOverMessage(); break; }
getHumanMoveAndApplyIt();
_userInterface.output(_pos);
if (ReversiRules.isGameOver(_pos, _toMoveColor)) { gameOverMessage(); break; }
getComputerMoveAndApplyIt();
_userInterface.output(_pos);
public void gameOverMessage() {
_userInterface.message("Game over...");
int winner = ReversiRules.winningColor(_pos, _toMoveColor);
if (winner == Reversi.Black) _userInterface.message("Black wins.");
else if (winner == Reversi.White) _userInterface.message("White wins.");
else _userInterface.message("Draw.");
package com.Ozo.games.reversi;
* The main class to start a Reversi game as an application.
public class ReversiMain {
public static void main(String[] args) {
ReversiGame game = new ReversiGame(8, 8);
ReversiTextIO tui = new ReversiTextIO(System.in, System.out);
game.setUserInterface(tui);
game.play();
}
package com.Ozo.games.reversi;
* This class provides all of the intelligence for a program to play Reversi.
* It contains all the knowledge of play strategy. (The knowledge of the rules
* is maintained by the ReversiRules class.)
* The principal method is findBestMove, which does an exhaustive search a number
* of plys deep, and then applies a hueristic position evaluation function.
public class ReversiStrategy {
private static int PlysToTry = 6;
* Find the best move for the player of the given color.
* If there is no move, then return "pass".
* @param pos the position to be evaluated.
* @param toMoveColor the color of player to move.
* @param plysRemaining the depth to examine (must be >= 1).
* @return the best move within the default horizon.
public static int findBestMove(ReversiPosition pos, int toMoveColor) {
return findBestMove(pos, toMoveColor, PlysToTry).move;
* This class is used to return a pair of values: the best move and the score it achieves
* after the number of plys remaining are played.
public static class BestMove {
public int move;
public int score;
public BestMove(int m, int s) { move = m; score = s; }
* Find the best move for the player of the given color within
* the given number of plys.
* @param pos the position to be evaluated.
* @param toMoveColor the color of player to move.
* @param plysRemaining the depth to examine (must be >= 1).
* @return a BestMove object returning the best move found and the score it achieves.
public static BestMove findBestMove(ReversiPosition pos, int toMoveColor, int plysRemaining) {
if (plysRemaining < 1) throw new Error("findBestMove needs plysRemaining >= 1");
// Generate the legal moves. If there are none, then pass.
int opponentColor = Reversi.playerOpposite(toMoveColor);
int[] moves = ReversiRules.generateMoves(pos, toMoveColor);
if (moves.length == 0) {
if (plysRemaining == 1)
return new BestMove(Reversi.newMovePass(pos), summaryScore(pos));
else
return findBestMove(pos, opponentColor, plysRemaining-1);
// Try all the moves. Re-use one position object to make the move.
ReversiPosition afterMove = pos.copy();
// Start with a hypothetical worst scenario and then look for what's better.
afterMove.fill(opponentColor);
int bestScore = 2*summaryScore(afterMove); // Worse that the worst possible real score.
int bestIndex = -1;
for (int i = 0; i < moves.length; i++) {
pos.copyInto(afterMove); // Re-use the position object.
afterMove.applyMove(moves, toMoveColor);
int thisScore = (plysRemaining == 1) ?
summaryScore(afterMove) :
findBestMove(afterMove, opponentColor, plysRemaining - 1).score;
if (Reversi.isBetterScore(toMoveColor, thisScore, bestScore)) {
bestScore = thisScore;
bestIndex = i;
if (bestIndex == -1) System.out.println("Number of moves " + moves.length + " plys " + plysRemaining);
return new BestMove(moves[bestIndex], bestScore);
* Examine contents of square and return 1 for Black, -1 for White, 0 for Empty.
* Useful in computing scores.
* @param r row number.
* @param c column number.
* @return +1/-1/0
private static int squareVal(ReversiPosition pos, int r, int c) {
if (pos.getSquare(r, c) == Reversi.White) return -1;
if (pos.getSquare(r, c) == Reversi.Black) return +1;
return 0;
* Count the number of black squares minus the number of white squares.
* @return difference in number of black and white squares.
public static int squareScore(ReversiPosition pos) {
int nBminusW = 0;
for (int r = 0; r < pos.nrows(); r++)
for (int c = 0; c < pos.ncols(); c++)
nBminusW += squareVal(pos, r, c);
return nBminusW;
* Count the number of black edge squares minus the number of white ones.
* @return difference in number of black and white squares.
public static int edgeScore(ReversiPosition pos) {
int nBminusW = 0;
// East and west edges.
for (int r = 1; r < pos.nrows()-1; r++)
nBminusW += squareVal(pos, r, 0) + squareVal(pos, r, pos.ncols()-1);
// North and south edges.
for (int c = 1; c < pos.ncols()-1; c++)
nBminusW += squareVal(pos, 0, c) + squareVal(pos, pos.nrows()-1, c);
return nBminusW;
* Count the number of black corner squares minus the number of white ones.
* @return difference in number of black and white squares.
public static int cornerScore(ReversiPosition pos) {
int rlast = pos.nrows()-1, clast = pos.ncols()-1;
return squareVal(pos, 0, 0) + squareVal(pos, 0, clast) +
squareVal(pos, rlast, 0) + squareVal(pos, rlast, clast);
* Compute a heuristic score for a given position. The more positive,
* the better for Black. Controlling corners is weighted most heavily,
* folowed by sides, then regular squares. In principle a different weight
* function should be used at the end of the game, since then it is really
* the total number of squares that counts.
* @param pos the position to be assessed.
* @return the numerical score, with positive being good for Black, negative good for White.
public static int summaryScore(ReversiPosition pos) {
return squareScore(pos) + 8*edgeScore(pos) + 20*cornerScore(pos);
}Edited by: Oozaro on Nov 19, 2009 12:41 PM
Similar Messages
-
i have 5 html pages that share a common header, footer, and sidebar. how do i use my nav bar to change the content of the body without duplicating a lot of code? thank you!
i inherited the website. It’s for a non-profit and is not very
sophisticated, and neither am I in webdesign. It currently has multiple
pages that are identical except for that body section, so whenever i change
the navigation (in the sidebar) I have to update every html page. I want
to have one basic page, and just call in the different body content based
on the link the user selects from the nav bar. How can i do that using a
script? i am using Dreamweaver.
~ in love and light ~
Jeannie
On Sat, Feb 7, 2015 at 4:07 AM, Ben Pleysier <[email protected]> -
Greetings,
I am a java developer, I have been so for the past 9 years. Whenever I have time on my hands, I develop applications just for fun. I have a lot of code written and would like to share all of it. Which place is the best to post all this code and application? It would be great, if a particular piece of code can be tagged with labels. For example, I developed this puzzle game... I would like to tag it with labels like "Swing, Images Manipulation" and so on and so forth. The tags essentially will make it easier for other developers to fetch code according to tags and look up stuff.
Thank you for any help.
Please excuse, If this is the wrong forum.rksharma wrote:
I have a lot of code written and would like to share all of it. Which place is the best to post all this code and application?There's an area of this site known as SDN Share. I'm sure you'll be able to find it.
db -
GUI crashes when calling native code..
Hi,
I have interfaced to an existing C program using the Java Native Interface and I am controlling the C program using a GUI. The problem is that everytime I click on the button to call the native method, the GUI crashes... The bizarre thing is that the C code seems to actually execute properly and no exceptions are thrown..
Anyone come across this problem?
/johnHi,
Thanks for the replies...
The GUI completely disappears. No error file is generated. No exceptions are thrown. Here it is in more detail..
The C code is invoked using the java native interface. The C code converts a MIDI file to a text file. It is normally run in DOS and accepts two parameters. You would run it in DOS normally as follows:
mf2t Example1.mid Example1.txt.
In the GUI I select the MIDI file and specify the output file (Example1.txt). I then pass these two parameters to the C code. The C code originally used argc and argv to accept the parameters. I replaced these with "fileArray" and "parameter"... "mf2t" replaces "main" in the native code...
On the java side the code looks like this:
static public native boolean mf2t(String s6, String s7);
public String infile; // Input MIDI file
public String textOut; // Output text file
private void MIDIButtonActionPerformed(java.awt.event.ActionEvent evt) {
Object target=evt.getSource();
if(target == MIDIButton) {
if(mf2t(infile,textOut)){
InfoLabel.setText("MIDI to text conversion complete");
The code is built on the C side into a DLL using MS Visual Studio.
On the C side the code looks something like this:
static char *fileArray[5];
static int argument=3;
static char midiArray[25];
static char txtArray[25];
void mf2t(int argument,char * fileArray[]);
// Entry point to native C code is here
JNIEXPORT jboolean JNICALL
Java_MainWindow_mf2t (JNIEnv *env, jobject j1, jstring midi, jstring txt)
const jbyte *midiFile;
const jbyte *txtFile;
midiFile=(*env)->GetStringUTFChars(env,midi, NULL);
txtFile=(*env)->GetStringUTFChars(env,txt, NULL);
// The lines above just convert the java string into a C equivalent..
strcpy(midiArray,midiFile);
strcpy(txtArray,txtFile);
fileArray[0]="mf2t.exe"; // Here I get fileArray to point to the converted strings
fileArray[1]=midiArray;
fileArray[2]=txtArray;
mf2t(argument,fileArray); // Then I pass this to a native method
(*env)->ReleaseStringUTFChars(env,midi, midiFile);
(*env)->ReleaseStringUTFChars(env,txt, txtFile);
return 1;
void mf2t(int argument,char * fileArray[]){
// native code in here
I think it may have something to do with threads.. I'm not sure though.. It's impossible to know whats going on in the C code when the GUI crashes. If anything looks strange it's because I left out some code here for the sake of brevity... Still if you see anything that stands out let me know..
Thanks,
John -
UWL HTML Gui is not launching T-Code
Helllo Experts,
We have a workiten in UWL that launches a transaction code in ECC when you click it.
When I set the SAP Gui type to Win Gui it launches the t-code. If I change it to HTML Gui it does not open the t-code, I don't get any error message and it goes straight to the navigation screen.
In our development environment, the html gui works perfect. We have the problem in our QA environment.
Thanks
IsmailHi,
did you check that ITS is working fine in your QA.
check webgui under ITS working fine or not under sicf.
Thank you
swapna -
I have my main class, which holds 5 JPanels on a container. There is an awful lot of code involved in setting all my components, especially since i use gridbagconstraints quite a bit. I will post the code, its quite a lot but its more for you to just glance over. What i wanted to do is neaten up my code, but i am not sure how. One thought was putting code related too each JPanel in its own method but i am ready to lesten to any suggestions, its problably really messy at the moment but who knows, the way i have it might be the standard way of doing it.....But as i say, any suggestions in making it better..
import java.awt.*; //the importing of required libraries
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.*;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.*;
public class MainPage extends JFrame implements ActionListener //extends JFrame so that we inherit JFrame's attributes and methods
{ //implements ActionLister on the class
MainPageMethods mpm = new MainPageMethods(); //creating a reference to class MainPageMethods (Linking it with this class)
private JPanel topPanel, leftPanel, centerPanel, rightPanel, bottomPanel; //declaring variables
private JLabel logUser, logPassword, regUser, regPassword, regName, mainTitle, loginTitle, regTitle, centerPanel1;
private JTextField logUser2, regUser2, regPassword2, regName2, logPassword2;
private JPasswordField logPassword12;
private JButton exitBt, logSubmitBt, regSubmitBt, regResetBt, logResetBt;
private Icon image = new ImageIcon("london_2012.png"); //assigning an image to an Icon
private JTextArea regTxt;
private String logUser3, regUser3, regPassword3, regName3, logPassword3, regTxt2;
private Container cPane; //container which sits in the JFrame
ImageIcon icon;
public MainPage() //constructor of the class
super(); //Superclassing the JFrame
createGUI(); //Calling up method cretaeGUI when this constructor is called
logUser3 = ""; //initialising the Strings so that they are empty
regUser3 = "";
regPassword3 ="";
regName3 ="";
logPassword3 = "";
private void createGUI(){ //createGUI method
cPane = getContentPane(); //assigning our container a ContentPane
arrangeCompenents(); //calling up arrangeComponents method
setTitle("Nrr"); //Setting title on JFrame
setLocation(new Point(0, 0)); //location
setVisible(true); //making it visible
setSize(800, 608); //setting the size
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //making it disappear if closed
private void arrangeCompenents(){ //arrangeComponents method
topPanel = new JPanel(); //initlising our variables
leftPanel = new JPanel();
centerPanel = new JPanel();
icon = new ImageIcon("london.jpg");
rightPanel = new JPanel(){
public void paintComponent(Graphics g)
Dimension d = getSize();
g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
setOpaque( false );
super.paintComponent(g);
bottomPanel = new JPanel();
logUser = new JLabel("Username");
logPassword = new JLabel("Password");
regUser = new JLabel("Username");
regPassword = new JLabel("Password");
regName = new JLabel("Full Name");
mainTitle = new JLabel("LONDON 2012");
loginTitle = new JLabel("<html><b><i>LOGIN</i></b></html>"); //using html within a JLabel
regTitle = new JLabel("<html><b><i>REGISTER</i></b></html>");
regTxt2 = ("This applicationis designed" + "\n" + "to be used by people autorised by "); //text for JTextArea
regTxt = new JTextArea(regTxt2, 10, 20); //initialising a JTextArea
regTxt.setEditable(false); //making sure it cannot be edited
regTxt.setBorder(BorderFactory.createLoweredBevelBorder()); //giving it a border
logUser2 = new JTextField(10);
logPassword2 = new JTextField(10);
regUser2 = new JTextField(10);
regPassword2 = new JTextField(10);
regName2 = new JTextField(10);
logPassword12 = new JPasswordField(10);
exitBt = new JButton("EXIT");
logSubmitBt = new JButton("SUBMIT");
regSubmitBt = new JButton("SUBMIT");
regResetBt = new JButton("RESET");
logResetBt = new JButton("RESET");
mainTitle.setFont(mainTitle.getFont().deriveFont(Font.BOLD, 48));
mainTitle.setForeground(Color.white);
centerPanel1 = new JLabel(image);
regTitle.setFont(new Font("Serif", Font.BOLD, 35));
regTitle.setForeground(Color.white);
loginTitle.setFont(new Font("Serif", Font.BOLD, 35));
loginTitle.setForeground(Color.white);
regTxt.setBorder(BorderFactory.createLoweredBevelBorder());
regTxt.setBackground(new Color(0, 153, 254));
cPane.setLayout(new BorderLayout()); //setting our ContenetPane to have a BorderLayout
cPane.add(topPanel,BorderLayout.PAGE_START); //Setting the JPanels in a location using BorderLayout on our ContentPane
cPane.add(centerPanel,BorderLayout.CENTER);
cPane.add(bottomPanel, BorderLayout.PAGE_END);
cPane.add(leftPanel, BorderLayout.LINE_START);
cPane.add(rightPanel, BorderLayout.LINE_END);
topPanel.setLayout(new FlowLayout()); //setting topPanel to use FlowLayout
topPanel.setBackground(new Color(223, 0, 148)); //giving the panel a background colour
topPanel.setBorder(BorderFactory.createRaisedBevelBorder()); //giving this panel a border
topPanel.add(mainTitle, 0); //adding variable mainTitle to panel
centerPanel.setLayout(new FlowLayout());
centerPanel.setBackground(new Color(0, 153, 254));
centerPanel.setBorder(BorderFactory.createRaisedBevelBorder());
centerPanel.add(centerPanel1, 0);
bottomPanel.setLayout(new FlowLayout());
bottomPanel.setBorder(BorderFactory.createRaisedBevelBorder());
bottomPanel.setBackground(new Color(223, 0, 148));
bottomPanel.setPreferredSize(new Dimension(608, 65));
bottomPanel.add(exitBt, 0);
leftPanel.setLayout(new GridBagLayout()); //setting the leftPanel to use GridbagLayout
leftPanel.setBackground(new Color(0, 153, 254)); //panel background colour
leftPanel.setBorder(BorderFactory.createLoweredBevelBorder()); //giving panel a border
GridBagConstraints gbc01 = new GridBagConstraints(); //creating the constraints of the panel
gbc01.gridx = 0; //location on the x axis
gbc01.gridy = 0; //location on the y axis
gbc01.insets = new Insets(35, 0, 15, 5); //size of the insets (Spacing around the component)
gbc01.gridwidth = GridBagConstraints.REMAINDER; //setting the gridwidth
gbc01.anchor = GridBagConstraints.PAGE_START; //setting an anchor
gbc01.fill = GridBagConstraints.NONE; //creating a fill
leftPanel.add(regTitle, gbc01); //adding variable regTitle to the panel with these constraints applied on it
GridBagConstraints gbc02 = new GridBagConstraints();
gbc02.gridx = 0;
gbc02.gridy = 1;
gbc02.insets = new Insets(5, 5, 5, 5);
gbc02.gridwidth = GridBagConstraints.REMAINDER;
gbc02.anchor = GridBagConstraints.LINE_START;
gbc02.fill = GridBagConstraints.NONE;
leftPanel.add(regTxt, gbc02);
GridBagConstraints gbc03 = new GridBagConstraints();
gbc03.gridx = 0;
gbc03.gridy = 2;
gbc03.insets = new Insets(15, 5, 5, 5);
gbc03.fill = GridBagConstraints.NONE;
leftPanel.add(regName, gbc03);
GridBagConstraints gbc04 = new GridBagConstraints();
gbc04.gridx = 1;
gbc04.insets = new Insets(15, 5, 5, 5);
gbc04.gridy = 2;
gbc04.gridwidth = 2;
gbc04.fill = GridBagConstraints.NONE;
leftPanel.add(regName2, gbc04);
GridBagConstraints gbc05 = new GridBagConstraints();
gbc05.gridx = 0;
gbc05.gridy = 3;
gbc05.insets = new Insets(5, 5, 5, 5);
gbc05.fill = GridBagConstraints.NONE;
leftPanel.add(regUser, gbc05);
GridBagConstraints gbc06 = new GridBagConstraints();
gbc06.gridx = 1;
gbc06.insets = new Insets(5, 5, 5, 5);
gbc06.gridy = 3;
gbc06.gridwidth = 2;
gbc06.fill = GridBagConstraints.NONE;
leftPanel.add(regUser2, gbc06);
GridBagConstraints gbc07 = new GridBagConstraints();
gbc07.gridx = 0;
gbc07.gridy = 4;
gbc07.insets = new Insets(5, 5, 5, 5);
//gbc07.anchor = GridBagConstraints.NORTH;
gbc07.fill = GridBagConstraints.NONE;
leftPanel.add(regPassword, gbc07);
GridBagConstraints gbc08 = new GridBagConstraints();
gbc08.gridx = 1;
gbc08.insets = new Insets(5, 5, 5, 5);
gbc08.gridy = 4;
gbc08.gridwidth = 2;
//gbc08.anchor = GridBagConstraints.NORTH;
gbc08.fill = GridBagConstraints.NONE;
leftPanel.add(regPassword2, gbc08);
GridBagConstraints gbc09 = new GridBagConstraints();
gbc09.gridx = 0;
gbc09.gridy = 5;
gbc09.insets = new Insets(5, 15, 5, 15);
gbc09.weighty = 1.0;
gbc09.anchor = GridBagConstraints.NORTH;
gbc09.fill = GridBagConstraints.NONE;
leftPanel.add(regResetBt, gbc09);
GridBagConstraints gbc10 = new GridBagConstraints();
gbc10.gridx = 2;
gbc10.gridy = 5;
gbc10.insets = new Insets(5, 15, 5, 15);
gbc10.weighty = 1.0;
gbc10.anchor = GridBagConstraints.NORTH;
gbc10.fill = GridBagConstraints.HORIZONTAL;
leftPanel.add(regSubmitBt, gbc10);
rightPanel.setLayout(new GridBagLayout());
rightPanel.setBackground(new Color(0, 153, 254));
rightPanel.setBorder(BorderFactory.createLoweredBevelBorder());
GridBagConstraints gbcR01 = new GridBagConstraints();
gbcR01.gridx = 0;
gbcR01.gridy = 0;
gbcR01.insets = new Insets(35, 0, 15, 5);
gbcR01.gridwidth = GridBagConstraints.REMAINDER;
gbcR01.anchor = GridBagConstraints.PAGE_START;
gbcR01.fill = GridBagConstraints.NONE;
rightPanel.add(loginTitle, gbcR01);
GridBagConstraints gbcR02 = new GridBagConstraints();
gbcR02.gridx = 0;
gbcR02.gridy = 1;
gbcR02.insets = new Insets(35, 5, 5, 5);
gbcR02.fill = GridBagConstraints.NONE;
rightPanel.add(logUser, gbcR02);
GridBagConstraints gbcR03 = new GridBagConstraints();
gbcR03.gridx = 1;
gbcR03.insets = new Insets(35, 5, 5, 5);
gbcR03.gridy = 1;
gbcR03.gridwidth = 2;
gbc01.fill = GridBagConstraints.NONE;
rightPanel.add(logUser2, gbcR03);
GridBagConstraints gbcR04 = new GridBagConstraints();
gbcR04.gridx = 0;
gbcR04.gridy = 2;
gbcR04.insets = new Insets(5, 5, 5, 5);
gbcR04.fill = GridBagConstraints.NONE;
rightPanel.add(logPassword, gbcR04);
GridBagConstraints gbcR05 = new GridBagConstraints();
gbcR05.gridx = 1;
gbcR05.insets = new Insets(5, 5, 5, 5);
gbcR05.gridy = 2;
gbcR05.gridwidth = 2;
gbcR05.fill = GridBagConstraints.NONE;
rightPanel.add(logPassword12, gbcR05);
GridBagConstraints gbcR06 = new GridBagConstraints();
gbcR06.gridx = 0;
gbcR06.gridy = 3;
gbcR06.insets = new Insets(5, 15, 5, 15);
gbcR06.weighty = 1.0;
gbcR06.anchor = GridBagConstraints.NORTH;
gbcR06.fill = GridBagConstraints.NONE;
rightPanel.add(logResetBt, gbcR06);
GridBagConstraints gbcR07 = new GridBagConstraints();
gbcR07.gridx = 2;
gbcR07.gridy = 3;
gbcR07.insets = new Insets(5, 15, 5, 15);
gbcR07.weighty = 1.0;
gbcR07.anchor = GridBagConstraints.NORTH;
gbcR07.fill = GridBagConstraints.HORIZONTAL;
rightPanel.add(logSubmitBt, gbcR07);
exitBt.addActionListener(this); //adding an ActionListener on all JButtons
logSubmitBt.addActionListener(this);
regSubmitBt.addActionListener(this);
regResetBt.addActionListener(this);
logResetBt.addActionListener(this);
public void actionPerformed(ActionEvent e) //creating the ActionEvents
if (e.getSource() == exitBt) //if the ActionEvent is exitBt
this.dispose(); //do this
else if (e.getSource() == logSubmitBt) //if the ActionEvent is ....
logUser3 = getLogUser(); //call up the getLogUser method and assign value to variable logUser3
logPassword3= getlogPassword(); //call up the getLogPassword method and assign value to variable logPassword3
mpm.LogSubmit(logUser3, logPassword3); //send these two variables to the LogSubmit method in mpm class (MainPageMethods), referenced at the top
logClear();
else if (e.getSource() == regSubmitBt)
regName3 = getRegName(); //call up the getRegName method and assign value to variable regName3
regUser3 = getRegUser(); //call up the getRegUser method and assign value to variable regUser3
regPassword3= getRegPassword(); //call up the getRegPassword method and assign value to variable regPassword3
mpm.RegSubmit(regName3, regUser3, regPassword3); //send these three variables to the RegSubmit method in mpm class (MainPageMethods)
regClear();
else if (e.getSource() == regResetBt)
regClear(); //calling up the regClear method below
else if (e.getSource() == logResetBt)
logClear();
String getRegName() //methods to retrieve the values entered in the TextFields
return regName2.getText();
String getRegUser()
return regUser2.getText();
String getRegPassword()
return regPassword2.getText();
String getlogUser()
return logUser2.getText();
String getLogUser()
return logUser2.getText();
String getlogPassword()
return new String(logPassword12.getPassword());
public void regClear() //methods to clear the TextFields
regName2.setText(""); //by setting the text of the fields to an empty string
regUser2.setText("");
regPassword2.setText("");
public void logClear()
logUser2.setText("");
logPassword12.setText("");
public static void main(String[] args){ //main method to execute the class
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new MainPage();
}Woo, 5 hour conference call.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class MainView {
private static final Font PANEL_TITLE_FONT =
new Font("Serif", Font.BOLD + Font.ITALIC, 35);
private final MainPageMethods mpm = new MainPageMethods();
private final JTextField loginUsername = new JTextField(10);
private final JPasswordField loginPassword = new JPasswordField(10);
private final JTextField registerFullName = new JTextField(10);
private final JTextField registerUsername = new JTextField(10);
private final JTextField registerPassword = new JPasswordField(10);
public MainView(final Container container, final Action exitAction) {
super();
container.setLayout(new BorderLayout());
container.add(this.buildTitlePanel(), BorderLayout.PAGE_START);
container.add(this.buildImagePanel(),BorderLayout.CENTER);
container.add(this.buildExitPanel(exitAction), BorderLayout.PAGE_END);
container.add(this.buildRegistrationPanel(), BorderLayout.LINE_START);
container.add(this.buildLoginPanel(), BorderLayout.LINE_END);
private final JPanel buildLoginPanel() {
final ImageIcon background = new ImageIcon("london.jpg");
final JPanel loginPanel = new JPanel(new GridBagLayout()) {
public void paintComponent(final Graphics g) {
super.paintComponent(g);
final Dimension d = this.getSize();
g.drawImage(background.getImage(), 0, 0, d.width, d.height, this);
this.setOpaque(false);
loginPanel.setBackground(new Color(0, 153, 254));
loginPanel.setBorder(BorderFactory.createLoweredBevelBorder());
final JLabel title = new JLabel("LOGIN");
title.setFont(PANEL_TITLE_FONT);
title.setForeground(Color.white);
final GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(35, 0, 15, 5);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.fill = GridBagConstraints.NONE;
loginPanel.add(title, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.insets = new Insets(35, 5, 5, 5);
constraints.gridwidth = 1;
loginPanel.add(new JLabel("Username"), constraints);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.insets = new Insets(5, 5, 5, 5);
loginPanel.add(new JLabel("Password"), constraints);
constraints.gridx = 1;
constraints.gridy = 1;
constraints.insets = new Insets(35, 5, 5, 5);
constraints.gridwidth = 2;
loginPanel.add(this.loginUsername, constraints);
constraints.gridx = 1;
constraints.gridy = 2;
constraints.insets = new Insets(5, 5, 5, 5);
loginPanel.add(this.loginPassword, constraints);
final JButton reset = new JButton(new ClearLoginAction());
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 1;
constraints.insets = new Insets(5, 15, 5, 15);
constraints.weighty = 1.0;
constraints.anchor = GridBagConstraints.NORTH;
constraints.fill = GridBagConstraints.NONE;
loginPanel.add(reset, constraints);
final JButton submit = new JButton(new LoginAction());
constraints.gridx = 2;
constraints.gridy = 3;
constraints.weighty = 1.0;
constraints.fill = GridBagConstraints.HORIZONTAL;
loginPanel.add(submit, constraints);
return loginPanel;
private final JPanel buildRegistrationPanel() {
final JPanel registrationPanel = new JPanel();
registrationPanel.setLayout(new GridBagLayout());
registrationPanel.setBackground(new Color(0, 153, 254));
registrationPanel.setBorder(BorderFactory.createLoweredBevelBorder());
final JLabel regTitle = new JLabel("REGISTER");
regTitle.setFont(PANEL_TITLE_FONT);
regTitle.setForeground(Color.white);
final GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(35, 0, 15, 5);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.fill = GridBagConstraints.NONE;
registrationPanel.add(regTitle, constraints);
final JTextArea authMessage = new JTextArea(10, 20);
authMessage.setText("This application is designed "
+ "to be used by people authorised by ");
authMessage.setLineWrap(true);
authMessage.setEditable(false);
authMessage.setBorder(BorderFactory.createLoweredBevelBorder());
authMessage.setBackground(new Color(0, 153, 254));
constraints.gridx = 0;
constraints.gridy = 1;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.LINE_START;
constraints.fill = GridBagConstraints.NONE;
registrationPanel.add(authMessage, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.insets = new Insets(15, 5, 5, 5);
registrationPanel.add(new JLabel("Full Name"), constraints);
constraints.gridx = 0;
constraints.gridy = 3;
constraints.insets = new Insets(5, 5, 5, 5);
registrationPanel.add(new JLabel("Username"), constraints);
constraints.gridx = 0;
constraints.gridy = 4;
constraints.insets = new Insets(5, 5, 5, 5);
registrationPanel.add(new JLabel("Password"), constraints);
constraints.gridx = 1;
constraints.gridy = 2;
constraints.insets = new Insets(15, 5, 5, 5);
constraints.gridwidth = 2;
registrationPanel.add(this.registerFullName, constraints);
constraints.gridx = 1;
constraints.gridy = 3;
constraints.insets = new Insets(5, 5, 5, 5);
registrationPanel.add(this.registerUsername, constraints);
constraints.gridx = 1;
constraints.gridy = 4;
registrationPanel.add(this.registerPassword, constraints);
final JButton reset = new JButton(new ClearRegistrationAction());
constraints.gridx = 0;
constraints.gridy = 5;
constraints.insets = new Insets(5, 15, 5, 15);
constraints.weighty = 1.0;
constraints.anchor = GridBagConstraints.NORTH;
registrationPanel.add(reset, constraints);
final JButton submit = new JButton(new LoginAction());
constraints.gridx = 2;
constraints.gridy = 5;
constraints.fill = GridBagConstraints.HORIZONTAL;
registrationPanel.add(submit, constraints);
return registrationPanel;
public JPanel buildTitlePanel() {
final JPanel titlePanel = new JPanel();
titlePanel.setBackground(new Color(223, 0, 148));
titlePanel.setBorder(BorderFactory.createRaisedBevelBorder());
final JLabel mainTitle = new JLabel("LONDON 2012");
mainTitle.setFont(mainTitle.getFont().deriveFont(Font.BOLD, 48));
mainTitle.setForeground(Color.white);
titlePanel.add(mainTitle);
return titlePanel;
public JPanel buildExitPanel(final Action exitAction) {
final JPanel exitPanel = new JPanel();
exitPanel.setBorder(BorderFactory.createRaisedBevelBorder());
exitPanel.setBackground(new Color(223, 0, 148));
exitPanel.setPreferredSize(new Dimension(608, 65));
final JButton exit = new JButton(exitAction);
exitPanel.add(exit);
return exitPanel;
public JPanel buildImagePanel() {
final JPanel imagePanel = new JPanel();
imagePanel.setLayout(new FlowLayout());
imagePanel.setBackground(new Color(0, 153, 254));
imagePanel.setBorder(BorderFactory.createRaisedBevelBorder());
final Icon image = new ImageIcon("london_2012.png");
final JLabel centerPanel1 = new JLabel(image);
imagePanel.add(centerPanel1);
return imagePanel;
private static class ExitAction extends AbstractAction {
private final JFrame frame;
public ExitAction(final JFrame frame) {
super("EXIT");
this.frame = frame;
public void actionPerformed(final ActionEvent e) {
this.frame.dispose();
private class LoginAction extends AbstractAction {
public LoginAction() {
super("SUBMIT");
public void actionPerformed(final ActionEvent e) {
MainView.this.mpm.LogSubmit(
MainView.this.loginUsername.getText(),
MainView.this.loginPassword.getPassword());
private class RegisterAction extends AbstractAction {
public RegisterAction() {
super("SUBMIT");
public void actionPerformed(final ActionEvent e) {
MainView.this.mpm.RegSubmit(
MainView.this.registerFullName.getText(),
MainView.this.registerUsername.getText(),
MainView.this.registerPassword.getText());
private class ClearLoginAction extends AbstractAction {
public ClearLoginAction() {
super("RESET");
public void actionPerformed(final ActionEvent e) {
MainView.this.loginUsername.setText("");
MainView.this.loginPassword.setText("");
private class ClearRegistrationAction extends AbstractAction {
public ClearRegistrationAction() {
super("RESET");
public void actionPerformed(final ActionEvent e) {
MainView.this.registerFullName.setText("");
MainView.this.registerUsername.setText("");
MainView.this.registerPassword.setText("");
public static void main(String[] args) {
final JFrame frame = new JFrame();
new MainView(frame.getContentPane(), new ExitAction(frame));
frame.setTitle("Nrr");
frame.setLocation(new Point(0, 0));
frame.setSize(800, 608);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
} -
Easy Java GUI Question Please Help!
Please help! whenever I try to run this code:
package here;
import java.awt.Graphics;
import java.awt.Color;
mport javax.swing.JFrame;
public class Window extends JFrame {
public Window(){
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(500,500);
public void paint(Graphics g){
g.drawString("Hello!", 75, 75);
public static void main(String[] args) {
new Window();
this keeps happening http://www.youtube.com/watch?v=k7htCX6a4BI&feature=watch_response
(I didn't post this video but it looks like I'm not the only one this has happened to)
its like I get a weird screen capture :/
I tried setting the bacgkround color and I tried using netbeans instead of eclipse. neither worked.
Any help would be really great! I don't want to get discouraged from learning java!First of all it contains Syntax error .
Call the super paint method when trying to override the paint method .
package here;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
public class Window extends JFrame {
public Window(){
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(500,500);
public void paint(Graphics g){
// call the base paint method first
super.paint(g);
g.drawString("Hello!", 75, 75);
public static void main(String[] args) {
new Window();
Edited by: VID on Mar 31, 2012 5:05 AM -
My daughter put a restriction code on her ipod touch and i also cannot get onto her itunes account. I need to know how to reset her device as a new device?
See Here > http://support.apple.com/kb/HT1808
You may need to try this More than Once... Be sure to Follow ALL the Steps...
Take your time... Pay particular attention to Steps 3 and 4. -
Why do so many code samples here use Arrays?
I've seen many code samples here use Arrays. Why? I much prefer to use ArrayLists, and Vectors if I need synchronization or HashSets if I require uniqueness. I don't understand why people are using Arrays when the Collection classes are so useful. You can change the underlying datastructure without changing any of the access code.
I guess they are faster...
public class Test1 {
public static void main(String[] args) {
Integer zero = new Integer(0);
long start = System.currentTimeMillis();
for (int j = 0; j < 1000; j++) {
Integer[] array = new Integer[10000];
for (int i = 0; i < 10000; i++) {
array[i] = zero;
long finish = System.currentTimeMillis();
System.out.println(finish - start);
start = System.currentTimeMillis();
for (int j = 0; j < 1000; j++) {
ArrayList arrayList = new ArrayList();
for (int i = 0; i < 10000; i++) {
arrayList.add(zero);
finish = System.currentTimeMillis();
System.out.println(finish - start);
}~460 ms for array
~1600 for ArrayList
Still, the benefit of the Collections interface and the better OO design of code that uses these classes is probably worth it in most cases, IMO. -
Revision: 13388
Revision: 13388
Author: [email protected]
Date: 2010-01-08 14:35:37 -0800 (Fri, 08 Jan 2010)
Log Message:
No code changes here - addressing several FIXMEs for 4.0.0.
QE notes: N/A
Doc notes: N/A
Bugs: N/A
Reviewer: Alex
Tests run: checkintests
Is noteworthy for integration: No
Modified Paths:
flex/sdk/branches/4.0.0/frameworks/projects/framework/src/mx/styles/StyleManager.as
flex/sdk/branches/4.0.0/frameworks/projects/framework/src/mx/styles/StyleProtoChain.as
flex/sdk/branches/4.0.0/frameworks/projects/framework/src/mx/utils/ObjectUtil.as -
my daughter put a pass code on her i pod and she cant remember it how can i resolve this and i can not read the serial number its to faded and scratched
If you cannot remember the passcode, you will need to Restore your device...
Connect to iTunes on the computer you usually Sync with and Restore...
http://support.apple.com/kb/HT1414
If necessary Place the Device into Recovery mode...
http://support.apple.com/kb/HT1808 -
My granddaughter put a pass code in her ipad and forgot it. how do you fix this?
My granddaughter put a pass code in her ipad2 and forgot what it was. How do you fix this?
If she's forgotten the passcode to unlock the iPad then she will need to connect the iPad to the computer that she normally syncs to and she should then be able to reset the iPad and restore/re-sync her content to it (http://support.apple.com/kb/HT1212) - she may need to put the iPad into recovery mode in order to be able to reset it : http://support.apple.com/kb/ht1808
If you do it via a different computer then :
If you restore on a different computer that was never synced with the device, you will be able to unlock the device for use and remove the passcode, but your data will not be present. -
HT201210 My daughter put a pass code on her ipod &she forgot what it was how do unlock it
My 6 yr old daughter put a pass code on her Ipod & she forgot it now she can unlock it does anyone know how
hi try this
Options for when an iOS device gets locked because of forgotten password:
Restore (and reset password) on your device by connecting it to the last computer to which it was connected:
iTunes: Backing up, updating, and restoring iOS software -http://support.apple.com/kb/HT1414
If you cannot connect it to the computer to which the device was last connected you will have to use recovery mode to completely reset the device, losing all data:
iOS: Unable to update or restore - http://support.apple.com/kb/HT1808 - recovery mode (e.g., cannot connect to computer last used to sync device, iTunes still asks for a password) -
My 8 year old put a pass code on her iPod touch and forgot it! Is there any way to get it to unlock?
This is asked and answered many times every day.
Look under More Like This on the right side of this page or use the forum search bar above it -
Question about compiled byte code
Howdy folks,
I have a client who wants to run an application in both
on-line and off-line modes. The user could run the application
locally on their laptop making changes and such which would get
stored to local database files (probably FoxPro free tables just to
make it easier on me). Then when the user got back to their
internet connection they could run the application and it would
sync with the online tables (probably MySql tables at that point).
So the question is, if I compile Cold Fusion code into Java
byte code, will it be able to execute independantly of the Cold
Fusion Server? I realize that I could load ColdFusion on the user's
laptop, but I don't think I want to do that. I'm assuming that the
answer to my question will be "No. You can't do that. Cold Fusion
isn't meant to work like that." To which my next question would be,
"Well, what language would be best for the type of application I
have described above? Action Script, maybe?"
Any thoughts are welcome, especially if you've written an
application like the one I've described.
Thanks very much,
ChrisWell, rats.
I wrote a nice reply to your message BKBK, but lost it
because, apparently, my session timed out.
The basic jist, was that I've been working on AJAX, and have
been implementing some AJAX-like techniques at some other clients
(using hidden iframes combined with innerHTML -- I know not a
standard, but darn handy otherwise), but I couldn't see how that
would solve my on-line/off-line problem (unless I stuck with the
cookies idea).
I also did some reading on cookies last night (obviously, I
don't use cookies very often if at all in my daily coding), and I'm
a bit put off by the different browser limitations. I'd hate my
client to be chugging along, entering appointments into the
"database" (read: data being stored as cookies to be sync'd later
when the user goes online), and then suddenly run into the cookie
limitation. On top of that, if I'm reading right, IE (my client's
most likely choise of browser), will not let you know that you've
reached this limit, but will just begin dropping the older cookies
in favor of the newer ones. If I could programmatically sense this
limitation and then write the cookies to some file before
continuing that'd be geat, but since JavaScript can't write files
(that I know of) this isn't feasable. Also, if I could write a file
like that, I wouldn't bother with the cookies.
I think I'm going to end up writing it in FoxPro since my
company has a bunch of copies of it (and it's licenced per
developer and not per copy), and there are lots of folks in my
company who can help me get up to speed. That also means that I'll
probably need to write a web version of the code for when my
client's client's (does that make sense? :-) ) connect to the app
via the internet.
Anyway, I'm really enjoying everyones comments on the
subject. Can anybody think of a technique for a way around the
cookie limitations? Or perhaps another language that this whole
thing could be written in?
I really wish that I could compile my ColdFusion code for use
independant of the CF server. I know, that's not the way it works
and typically not what scripting languages like this are used for.
I suppose I could always install the developer's version of CF on
the user's local machine, write the code in CF and then just detect
whether or not the user is online and behave accordingly.
Maybe you are looking for
-
Dear Friends / Experts We have around 10 HR (BW/BO) reports. All are woking fine. Now the users wants to restrict the report based on cost center. Hence, I have created one DSO with relevent information and extracted the data from ECC ( Used id, Cost
-
I bought an iPhone 5 off of craigslist, which unbeknownstome was stolen
Before I start, I just want to let everyone know that I know exactly what most of you will say, but I beg you to read this with an open-mind and understand where I'm coming from and my position. I am also a victim in this situation, just like the gir
-
Multi mapping for Synchronous Interface (RFC)
Is it posssible to define synch multimapping (RFC)? For exmaple my structure is request <items> <item> </item> <item> </item> </items> I want to invoke RFC for each <item> and to get response like <responseitems> <item> </item> <item> </item> </resp
-
Dynamic Stream not playing f4v files
In my .smil file, if I put this line it works: <video src="sample.flv" system-bitrate="150000"/> But if I put the following line it doesn't work: <video src="mp4:sample1_150kbps.f4v" system-bitrate="150000"/> Both video files are in the webroot\vod f
-
How do I get evaluation license key for linux NW04s installation.
Can anyone help please. I am going through this site on linux , but there is no link to get the evaluation license key for NW04s. I downloaded all the binaries. Thanks. TK