problem with calling the method after pressing the key - java

I would like to implement KeyListiner so that when pressing Space key program would generate new object of Function class and draw new image representing this function. For now program is working but pressing space key does not allow to generate a new image. Every advice will be greatly appreciated :)
Main class:
public class Main {
public static void main(String[] args) {
JFrame window = new JFrame(SOFTWARE_TITLE);
MainView mainView = new MainView();
Key key = new Key();
Controller controller = new Controller(mainView);
key.setController(controller);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(790, 580);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.add(mainView);
window.setVisible(true);
}
}
MainView class:
public class MainView extends JPanel {
private Function function;
public MainView(){
super();
setBackground(Color.GRAY);
setLayout(null);
setSize(200, 200);
function = new Function();
DrawBoard drawBoard = new DrawBoard(function);
drawBoard.setSize(500, 500);
drawBoard.setBackground(Color.lightGray);
drawBoard.setLocation(250, 20);
add(drawBoard);
setVisible(true);
}
}
DrawBoard class:
public class DrawBoard extends Canvas {
short CUSTOM_WIDTH = 5 * 100;
private Function function;
public DrawBoard(Function function) {
this.function = function;
}
public void paint(Graphics g) {
double difference = function.getzMax() - function.getzMin();
for (int indexX = 0; indexX < 100; indexX++) {
for (int indexY = 0; indexY < 100; indexY++) {
double value = function.getPoints()[indexX][indexY].getZ() - function.getzMin();
double corrected = setFloatInRGBRange(difference, value);
g.setColor(intToColor((int) corrected));
g.fillRect(indexX * 5, CUSTOM_WIDTH - ((indexY+1) * 5), 5, 5);
}
}
}
public Color intToColor(int colNum){
return new Color(colNum, colNum, colNum);
}
private int setFloatInRGBRange(double difference, double value){
return (int) ((255 * value)/difference);
}
}
Key class:
public class Key implements KeyListener {
Controller controller;
public void setController(Controller controller) {
this.controller = controller;
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_SPACE){
controller.generateFormula();
}
}
}
Controller class:
public class Controller {
MainView mainView;
public Controller(MainView mainView) {
this.mainView = mainView;
}
public void generateFormula() {
this.mainView = new MainView();
}
}

There is some other things that need be reviewed in code you posted. But for your main concern i guess you donc check Key strokes properly.
I would use e.getKeyCode() instead of e.getKeyChar() to check the event.
So your condition would be : e.getKeyCode() == KeyEvent.VK_SPACE
it is out of topic, but :
Your use of Function in multiples classes is unnecessary. It could be used only in DrawBoard
Don't mix Swing and AWT components as other already advised

Related

JPanel doesn't react to MouseEvents?

I'm trying to create a "Tic Tac Toe" game. I've chosen to create a variation of JPanel to represent each square. The class beneath represents one of 9 panels that together make up my game board.
Now the problem I'm having is that when I click the panel a 'X' should be displayed inside of the panel, but nothing happens. I'd very much appreciate it if someone steered me in the right direction.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToePanel extends JPanel implements MouseListener {
private boolean isPlayer1Turn = true;
private boolean isUsed = false;
private JLabel ticTacLbl = new JLabel();
public TicTacToePanel() {
setBorder(BorderFactory.createLineBorder(Color.BLACK));
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
if (!isUsed) {
if (isPlayer1Turn) {
ticTacLbl.setForeground(Color.red);
ticTacLbl.setText("X");
add(ticTacLbl, 0);
isUsed = true;
} else {
ticTacLbl.setForeground(Color.blue);
ticTacLbl.setText("O");
add(ticTacLbl, 0);
isUsed = true;
}
} else {
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, new TicTacToePanel());
}
}
EDIT:
I simply added my label component in the constructor of my TicTacToePanel so that I no longer have to call revalidate() and I'm not adding components during runtime.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToePanel extends JPanel implements MouseListener{
private boolean isPlayer1Turn = true;
private boolean isUsed = false;
private JLabel ticTacLbl = new JLabel();
public TicTacToePanel(){
add(ticTacLbl, 0);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
}
public void mousePressed(MouseEvent e){
if (!isUsed) {
if (isPlayer1Turn) {
ticTacLbl.setForeground(Color.red);
ticTacLbl.setText("X");
isUsed = true;
} else {
ticTacLbl.setForeground(Color.blue);
ticTacLbl.setText("O");
isUsed = true;
}
}
else{
}
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public static void main(String[] args){
JOptionPane.showMessageDialog(null, new TicTacToePanel());
}
}
The GUI constructor:
public TicTacToeGUI(int gameMode){
if(gameMode == 0){
amountOfPanels = 9;
TicTacToePanel[] panelArr = new TicTacToePanel[amountOfPanels];
add(gamePanel, new GridLayout(3, 3));
setPreferredSize(new Dimension(100, 100));
for(int i = 0; i < amountOfPanels; i++){
panelArr[i] = new TicTacToePanel();
gamePanel.add(panelArr[i]);
}
}
else if(gameMode == 1){
amountOfPanels = 225;
TicTacToePanel[] panelArr = new TicTacToePanel[amountOfPanels];
add(gamePanel, new GridLayout(15, 15));
setPreferredSize(new Dimension(500, 500));
for(int i = 0; i < amountOfPanels; i++){
panelArr[i] = new TicTacToePanel();
gamePanel.add(panelArr[i]);
}
}
}
public static void main(String[] args){
JOptionPane.showMessageDialog(null, new TicTacToeGUI(0));
}
}
When you add/remove components at runtime, always call revalidate() afterwards. revalidate() makes the component refresh/relayout.
So just call revalidate() after you add the label and it will work.
If you're goal is to create a Tic Tac Toe game, then you may wish to re-think your current strategy of adding components to the GUI on the fly. Much better would be to create a grid of components, say of JLabel, and place them on the JPanel at program start up. This way you can change the pressed JLabel's text and color, and even its Icon if you want to be fancy during program run without having to add or remove components. For example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
#SuppressWarnings("serial")
public class TicTacToePanel extends JPanel {
private static final int ROWS = 3;
private static final int MY_C = 240;
private static final Color BG = new Color(MY_C, MY_C, MY_C);
private static final int PTS = 60;
private static final Font FONT = new Font(Font.SANS_SERIF, Font.BOLD, PTS);
public static final Color X_COLOR = Color.BLUE;
public static final Color O_COLOR = Color.RED;
private JLabel[][] labels = new JLabel[ROWS][ROWS];
private boolean xTurn = true;
public TicTacToePanel() {
setLayout(new GridLayout(ROWS, ROWS, 2, 2));
setBackground(Color.black);
MyMouse myMouse = new MyMouse();
for (int row = 0; row < labels.length; row++) {
for (int col = 0; col < labels[row].length; col++) {
JLabel label = new JLabel(" ", SwingConstants.CENTER);
label.setOpaque(true);
label.setBackground(BG);
label.setFont(FONT);
add(label);
label.addMouseListener(myMouse);
}
}
}
private class MyMouse extends MouseAdapter {
#Override // override mousePressed not mouseClicked
public void mousePressed(MouseEvent e) {
JLabel label = (JLabel) e.getSource();
String text = label.getText().trim();
if (!text.isEmpty()) {
return;
}
if (xTurn) {
label.setForeground(X_COLOR);
label.setText("X");
} else {
label.setForeground(O_COLOR);
label.setText("O");
}
// information to help check for win
int chosenX = -1;
int chosenY = -1;
for (int x = 0; x < labels.length; x++) {
for (int y = 0; y < labels[x].length; y++) {
if (labels[x][y] == label) {
chosenX = x;
chosenY = y;
}
}
}
// TODO: check for win here
xTurn = !xTurn;
}
}
private static void createAndShowGui() {
TicTacToePanel mainPanel = new TicTacToePanel();
JFrame frame = new JFrame("Tic Tac Toe");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Java.awt.event classes management

I started writing a game (also taking code parts online) in Java all in one class and now I'm trying to break it into multiple classes but awt.event is giving me problems.
I want the class MainGUI to implement awt.event classes but methods keyPressed and actionPerformed must only contain a call to a method that is in another class that contains the code to be executed in correspondence with the occurrence of the event .
I am doing this because I want the class mainGUI to only realize the game window , leaving the management of controls to another class .
I tried to transfer the code and implement the methods to call and everything works except the method requestFocus .
When I call it indirectly from the class used for the control, asking him to move the focus to the game window, it does not work .
So, the class I want to break is MainGUI and the class I want to manage the controls is Logic:
public class MainGUI extends JFrame implements ComponentListener, ActionListener, FocusListener, KeyListener {
//STATIC ATTRIBUTES
private final static int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3, STOP = 4;
private final static int NUMBER_OF_ROWS = 70;
private final static int NUMBER_OF_COLUMNS = 70;
private static int direction;
private static int currentX, currentY;
//INSTANCE ATTRIBUTES
private BoardPanel drawPanel;
private JPanel bottomPanel;
private JLabel message;
private Timer timer;
//CONSTRUCTOR
public MainGUI() {
super("Grid");
createGUI();
}
//PUBLIC INSTANCE METHODS
public void createbottomPanel(){
this.bottomPanel=new JPanel();
this.message = new JLabel("To START, Press \"ENTER\"", JLabel.CENTER);
this.message.setBackground(Color.LIGHT_GRAY);
this.message.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
this.bottomPanel.add(this.message);
}
public void createGUI(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(800, 600));
this.drawPanel = new BoardPanel();
createbottomPanel();
addComponentListener(this);
addKeyListener(this);
addFocusListener(this);
this.message.addKeyListener(this);
Container contPane = getContentPane();
contPane.setLayout(new BorderLayout());
contPane.add(this.drawPanel,BorderLayout.CENTER);
contPane.add(this.bottomPanel, BorderLayout.SOUTH);
this.currentX = 50;
this.currentY = 30;
this.direction = STOP;
this.message.requestFocus();
pack();
}
public void componentResized(ComponentEvent e) {
this.drawPanel.setGridUnit();
}
public void componentHidden(ComponentEvent e) {
//do-nothing
}
public void componentMoved(ComponentEvent e) {
//do-nothing
}
public void componentShown(ComponentEvent e) {
//do-nothing
}
public void actionPerformed(ActionEvent e) {
switch (direction) {
case UP:
if (currentY > 0)
currentY--;
BoardPanel.fillCell(currentX,currentY);
this.drawPanel.repaint();
break;
case DOWN:
if (currentY < NUMBER_OF_ROWS-1)
currentY++;
BoardPanel.fillCell(currentX,currentY);
this.drawPanel.repaint();
break;
case RIGHT:
if (currentX < NUMBER_OF_COLUMNS-1)
currentX++;
BoardPanel.fillCell(currentX,currentY);
this.drawPanel.repaint();
break;
case LEFT:
if (currentX > 0)
currentX--;
BoardPanel.fillCell(currentX,currentY);
this.drawPanel.repaint();
break;
}
}
public void focusGained(FocusEvent e) {
direction=STOP;
message.setText("To PAUSE, Press \"P\"");
timer = new Timer(50,this);
timer.start();
}
public void focusLost(FocusEvent e) {
if (timer != null)
timer.stop();
timer = null;
message.setText("To START, Press \"ENTER\"");
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_LEFT)
direction = LEFT;
else if (code == KeyEvent.VK_RIGHT)
direction = RIGHT;
else if (code == KeyEvent.VK_UP)
direction = UP;
else if (code == KeyEvent.VK_DOWN)
direction = DOWN;
else if (code == KeyEvent.VK_P)
message.requestFocus();
else if (code == KeyEvent.VK_ENTER)
this.requestFocus();
}
public void keyReleased(KeyEvent e) {
//do-nothing
}
public void keyTyped(KeyEvent e) {
//do-nothing
}
//STATIC METHODS
public static void openWindow() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run () {
new MainGUI().setVisible(true);
}
});
}
} // end class
The class Logic:
public class Logic {
//STATIC ATTRIBUTES
private static MainGUI test = new MainGUI();
//STATIC METHODS
public static void initGame(){
test.openWindow();
}
//This method must be called from actionPerformed in MainGUI and
//contain the code to be executed
public static void logicPerforms(){
//empty-for-now
}
//This method must be called from keyPressed in MainGUI and
//contain the code to be executed
public static void logicPress(){
//empty-for-now
}
} //end-class
The classes are in different packages but I omitted the various imports.
The class Boardpanel draws a grid and colors the cell coordinates (x, y) through its method fillCells.
The main class is:
public class Main {
public static void main(String[] args) {
Logic.initGame();
}
} //end-class
I transferred the necessary variables in the class Logic, copied the code from the methods actionPerformed and keyPressed of MainGUI and created static methods ( for example, to run the repaint ) to be called from logicPerforms and logicPress to solve call problems but these lines are not working:
else if (code == KeyEvent.VK_P)
test.askMessageFocus();
else if (code == KeyEvent.VK_ENTER)
test.askGUIFocus();
where askMessageFocus e askGUIFocus are two of those static methods mentioned earlier (they are in the class MainGUI) whose code is:
this.message.requestFocus();
for askMessageFocus and
this.requestFocus();
for the other one.
I apologize for my English and other mistakes I may have made ​​in the code, I hope someone can help me!
Almost any advice will be welcome , even aimed at optimizing other parts of code!
Thank you!!

Windows alignment in SWING-based GUI

My goal is to display several views of one object. For each view I create a thread. Also, I have a class which controls those views, e.g. send a command to align them. However, it is not always I get correct alignment. So there is a data races, and I cannot understand what I am doing wrong.
Here there is a piece of code showing the problem I have. It has a simple idea: create a main view window, and then align the second window of the same size near its right border.
First, I have an abstract class to create a thread:
public abstract class ViewWindow implements Runnable{
private Thread thread;
private boolean terminate = false ;
private Controller controller;
private UpdateTask currentUpdateTask = null;
private class UpdateTask {
boolean alignWindows = true;
}
public ViewWindow(Controller controller, String title) {
this.title = title;
this.controller = controller;
}
public void startThread() {
thread = new Thread(this);
thread.start();
}
#Override
public void run() {
UpdateTask updateTask = null;
synchronized (thread) {
while (terminate == false) {
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
updateTask = currentUpdateTask;
currentUpdateTask = null;
if(updateTask.alignWindows) {
controller.getLock().lock();
setLocationRelativeTo(controller.getMainWindow());
controller.getLock().unlock();
}
}
}
}
public void alignWindowsUsingThread() {
synchronized (thread) {
currentUpdateTask = new UpdateTask();
thread.notify();
}
}
public abstract void setLocationRelativeTo(ImageViewWindow imageWindow);
}
Then I extend it to create an abstraction for the window views:
public abstract class ImageViewWindow extends ViewWindow {
private JFrame frame;
public ImageViewWindow(Controller controller, String title) {
super(controller, title);
frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel(title);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(300,500));
frame.pack();
frame.setVisible(true);
}
public JFrame getFrame() {
return frame;
}
synchronized public void setLocation(int x, int y) {
frame.setLocation(x, y);
}
synchronized public Point getLocation() {
return frame.getLocation();
}
}
Finally, I override a function to set relative location for each window:
public class FirstWindow extends ImageViewWindow {
public FirstWindow(Controller controller, String title) {
super(controller, title);
this.setLocation(50, 50);
this.startThread();
}
#Override
public void setLocationRelativeTo(ImageViewWindow imageWindow) { }
}
public class SecondWindow extends ImageViewWindow {
public SecondWindow(Controller controller, String title) {
super(controller, title);
this.startThread();
}
#Override
public void setLocationRelativeTo(ImageViewWindow imageWindow) {
Point location = imageWindow.getLocation();
int xOffSet = imageWindow.getFrame().getWidth();
int yOffSet = 0;
this.setLocation(xOffSet + location.x, yOffSet + location.y);
}
}
Here there is a class which is responsible for the control:
public class Controller {
private Lock controlLock;
private List<ImageViewWindow> windows = new ArrayList<ImageViewWindow>();
private ImageViewWindow mainWindow;
public Controller() {
controlLock = new ReentrantLock();
}
public ImageViewWindow getMainWindow() {
return mainWindow;
}
public Lock getLock() {
return controlLock;
}
public void addMainWindow(ImageViewWindow mainViewWindow) {
this.mainWindow = mainViewWindow;
this.addWindow(mainViewWindow);
}
public void addWindow(ImageViewWindow imageWindow) {
windows.add(imageWindow);
}
public void updateWindowPositions() {
for(ImageViewWindow window : windows) {
window.alignWindowsUsingThread();
}
}
}
And do run everything:
public class Start {
public static void main(String[] args) {
Controller controller = new Controller();
ImageViewWindow window1 = new FirstWindow(controller, "FirstWindow");
controller.addMainWindow(window1);
ImageViewWindow window2 = new SecondWindow(controller, "SecondWindow");
controller.addWindow(window2);
controller.updateWindowPositions();
}
}
UPD: I updated the code based on the answer below, but the problem still remains!
You don’t specify how you want to “align” your windows but from your code
public void setLocationRelativeTo(ImageViewWindow imageWindow) {
Point location = imageWindow.getLocation();
int xOffSet = this.getFrame().getWidth();
int yOffSet = 0;
this.setLocation(xOffSet + location.x, yOffSet + location.y);
}
I suppose you want this to be placed to the right of imageWindow. In this case you have to use imageWindow.x + imageWindow.width rather than imageWindow.x + this.width:
| imageWindow | this
x ← width → x ← width →
↳=imageWindow.x+imageWindow.width
So the correct method would be:
public void setLocationRelativeTo(ImageViewWindow imageWindow) {
Point location = imageWindow.getLocation();
int xOffSet = imageWindow.getFrame().getWidth();
int yOffSet = 0;
this.setLocation(xOffSet + location.x, yOffSet + location.y);
}
By the way, I don’t get why you are making such a simple task that complicated and even multi-threaded. There’s no benefit from multi-threading here, only a complication that obstructs the view on the simplest things…

timer t = new timer(50, b); t.stop(); is not working

My t.stop(); method is not working. I am going crazy trying to figure out why my stop method is not working.
I'm using the a timer in my code and I can't get it to stop. Can anyone take a look at it and tell me what's going on?:
/*Gilberto Rose*/
package homework2;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MultipleBalls extends JFrame implements ActionListener
{
int dx = 2;
int dy = 2;
int x = 1;
int y = 1;
int i = 0;
public static void main(String[] args)
{
Runnable balls = new Ball2();
Thread thread1 = new Thread(balls);
thread1.run();
}
#Override
public void actionPerformed(ActionEvent arg0)
{
repaint();
System.out.println(i++);
}
}// End of Ball class
class Ball2 extends JPanel implements Runnable
{
MultipleBalls b = new MultipleBalls();
JButton g = new JButton("resume");
JButton f = new JButton("suspend");
JButton e = new JButton("-1");
JButton d = new JButton("+1");
List<Ball2> L = new ArrayList<Ball2>();
Timer t = new Timer(50, b);
public int x = 6;
public void loopstop()
{
t.stop();
}// end of loopstop method
Ball2()
{
controller4();
controller3();
controller2();
controller();
add(d);
add(e);
add(f);
add(g);
}// End of Ball2 constructor
public void run()
{
Ball2 c = new Ball2();
b.setSize(500, 500);
b.setVisible(true);
b.add(c);
t.start();
} // End of run method
public void controller()
{
d.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
L.add(new Ball2());
}
});
}// End of controller
public void controller2()
{
e.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("subtracter");
L.remove(L.size()-1);
}
});
}// End of controller2
public void controller3()
{
f.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
loopstop();
}
});
}// End of controller3
public void controller4()
{
g.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Resume");
}
});
}// End of controller4
public void paintComponent(Graphics g)
{
if(L.size() > 0)
{
int i = 0;
do
{
g.fillOval(L.get(i).ballx(), L.get(i).bally(), 90, 90);
i++;
}while(i < L.size() && true ); // End of Do while loop
}// End of if statement
}// End of paintComponent
MultipleBalls bb = new MultipleBalls();
public int ballx()
{
if (bb.x == 0 || bb.x == 500)
{
bb.dx *= -1;
} // End of if statement
bb.x += bb.dx;
return bb.x;
}
public int bally()
{
if (bb.y == 0 || bb.y == 500 )
{
bb.dy *= -1;
}// end of if statement
bb.y += bb.dy;
return bb.y;
}// End of bally
}// End of Ball2 class
Your code is extremely convoluted, I believe that it's suffering from something called cyclomatic complexity, so much so, it is difficult for you or us to see what object is creating what other object, and what is running what. And this is your problem. You have at least two MultipleBall objects, two Ball2 objects, and you're starting the Timer for one of the Ball2 objects and stopping it for the other.
The solution: simplify this code greatly.
Create one MultipleBalls object, just one.
Don't have MultipleBalls implement ActionListener. Rather use an anonymous inner class for your ActionListener and create it on the spot where you need it.
Create just one Ball2 object, just one.
Also note that you almost never call run() on a Thread object but rather start(), but having said that, I'm not even sure that you should be using a Thread object where you're using it.
Edit
My main class would be simple, and would simply have a main method and supporting method that gets things started. Something like:
public class MultipleBalls {
private static void createAndShowGui() {
BallsPanel mainPanel = new BallsPanel();
JFrame frame = new JFrame("Multiple Balls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit
For an example of a separation of concerns:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
public class MultipleBallsZ {
private static void createAndShowGui() {
BallsPanelZ ballsPanel = new BallsPanelZ();
new Control(ballsPanel);
JFrame frame = new JFrame("Multiple Balls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(ballsPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class BallsPanelZ extends JPanel {
private static final int TIMER_DELAY = 200;
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private Timer timer = new Timer(TIMER_DELAY, new TimerListener());
private int counter = 0;
private Control control = null;
public BallsPanelZ() {
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public Timer getTimer() {
return timer;
}
private class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
counter++;
System.out.printf("Count: %03d%n", counter);
}
}
public void setControl(Control control) {
this.control = control;
for (Action action : control) {
add(new JButton(action));
}
}
}
#SuppressWarnings("serial")
class Control implements Iterable<Action> {
private List<Action> actionList = new ArrayList<>();
private BallsPanelZ ballsPanel;
public Control(BallsPanelZ ballsPanel) {
actionList.add(new PauseAction());
actionList.add(new ResumeAction());
this.ballsPanel = ballsPanel;
ballsPanel.setControl(this);
}
private class PauseAction extends AbstractAction {
public PauseAction() {
super ("Timer Pause");
putValue(MNEMONIC_KEY, KeyEvent.VK_P);
}
#Override
public void actionPerformed(ActionEvent e) {
ballsPanel.getTimer().stop();
}
}
private class ResumeAction extends AbstractAction {
public ResumeAction() {
super("Timer Resume");
putValue(MNEMONIC_KEY, KeyEvent.VK_R);
putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 6);
}
#Override
public void actionPerformed(ActionEvent e) {
ballsPanel.getTimer().restart();
}
}
#Override
public Iterator<Action> iterator() {
return actionList.iterator();
}
}

Clearing a Sudoku table

I'm working on this piece of code which can be found at
http://pastebin.com/7bCFtUHL
Basically, I want to add a clear method (button) which clears the sudoku after having it solved.
I've tried making a loop that goes through every cell and puts it to null but I'm not completely sure how to connect it exactly. Nor am I sure in which class I'd have to create it so it can be connected to the GUI where I have the other button.
EDIT:
This is the clear method I currently got
public void clearCells(){
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
cells[y][x] = null;
cells[y][x].setText("");
}
}
}
Now I need to attach that to the JButton in another class, how would that be possible?
My clear button looks like this
JButton clear = new JButton("Clear");
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Code
}
}
);
What code would I need to add in the actionPerformed method to connect it with my clearCells method?
Again, I would put the "meat" of the clear method in the model itself. The general form of a solution would be to do this:
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myModel.clearCells();
}
});
Where the Model class would have a public void clearCells() method that iterates through the cells and clears them.
Edit 1
Note: yeah I did look at your pastebin code link and one big problem I see is that your SwingSudokuBoard class extends the SudokuBoard class, and this is misuse of inheritance where you should be using composition instead. The SwingSudokuBoard class should hold an instance of a SudokuBoard object and call methods on it.
Edit 2
You ask:
I'm not sure that I can completely understand you. You want me to have the clear method in the same class as I got the button, but then I cant call the cells. I added x.clearCells(); while x being what? My main class like, SwingSudokuBoard.clearCells(); ? Eitherway, if I add what you say the program complaints that it want the clearCells method and cells to be static. But if I put them to static, I get a NullPointerException.
I think that you need to use the Model-View-Control (MVC) pattern or an abbreviated version of it, perhaps one where you combine the view with the control since your program is small. I suggest that you have a separate model class, here this would likely be the SudokuBoard class, and then a view class, here probably the SwingSudokuBoard class. Your view's control methods (the ActionListeners) would call the model's clearCells() method. And don't use static anything here.
Edit 3
You ask:
I assume something along with these lines. Model: SudokuBoard; View: SwingSudokuBoard; Control: SwingSudoKiller. How would that go about? I'd have the actionListener posted above in the control. How would the other classes look like? Since I assume the clear method lays in the Model which you want to be in SudokuBoard but it cant connect with the cells there.
I'm not a professional, nor have I received formal programming training, so theory is one of my weak points, but my interpretation of MVC is that the view listens to the model and updates itself when the model notifies it of changes and that the control listens to the view and responds to view changes by notifying the model. This precise pattern has variations and does not need to be followed exactly to the letter, but the key in all of this is to separate out in your code the separate concerns as much as possible so that "coupling" (the number of direct connections between classes) is low or "loose" and "cohesion" (code that deals with the same concerns) is high or "tight".
In your program, again I'd combine the view and control by using anonymous inner listeners just as you're doing. I'd have the view/control, which is the SwingSudokuBoard class, hold an instance of the SudokuBoard class as a class field, and have the view/control's anonymous listeners call methods on the SudokuBoard field. When I've done this sort of thing before, I've given the model support for being observed by giving it a SwingPropertyChangeSupport object as well as public addPropertyChangeListener(...) and removePropertyChangeListener(...) methods. Then the view could respond easily to changes in the model.
You state:
Since I assume the clear method lays in the Model which you want to be in SudokuBoard but it cant connect with the cells there.
I'm not sure what you mean by this. The model holds the cells. Perhaps you don't mean the logical cells held by the model but rather the displayed cells held by the view. The view would add a listener to the model, and when notified of changes to the model, would ask the model for its data and use that to update the visualized cells.
Edit 4
For example:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
public class OverlySimpleModelView {
private static void createAndShowGui() {
Model model = new Model();
ViewControl viewControl = new ViewControl(model);
JFrame frame = new JFrame("OverlySimpleModelView");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(viewControl.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ViewControl {
private JPanel mainPanel = new JPanel();
private JTextField number1Field = new JTextField(5);
private JTextField number2Field = new JTextField(5);
private JTextField productField = new JTextField(5);
private Model model;
public ViewControl(Model model) {
this.model = model;
model.addPropertyChangeListener(new MyPropChngListener());
productField.setEditable(false);
productField.setFocusable(false);
mainPanel.add(number1Field);
mainPanel.add(new JLabel(" * "));
mainPanel.add(number2Field);
mainPanel.add(new JLabel(" = "));
mainPanel.add(productField);
CalculateAction calculateAction = new CalculateAction("Calculate", KeyEvent.VK_C);
mainPanel.add(new JButton(calculateAction));
number1Field.addActionListener(calculateAction);
number2Field.addActionListener(calculateAction);
mainPanel.add(new JButton(new ClearAction("Clear", KeyEvent.VK_L)));
}
public JComponent getMainComponent() {
return mainPanel;
}
private class MyPropChngListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
number1Field.setText(String.valueOf(model.getNumber1()));
number2Field.setText(String.valueOf(model.getNumber2()));
productField.setText(String.valueOf(model.calculateProduct()));
}
}
private class CalculateAction extends AbstractAction {
public CalculateAction(String text, int keyCode) {
super(text);
putValue(MNEMONIC_KEY, keyCode);
}
#Override
public void actionPerformed(ActionEvent evt) {
try {
double number1 = Double.parseDouble(number1Field.getText());
double number2 = Double.parseDouble(number2Field.getText());
model.setNumber1(number1);
model.setNumber2(number2);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
private class ClearAction extends AbstractAction {
public ClearAction(String text, int keyCode) {
super(text);
putValue(MNEMONIC_KEY, keyCode); // to allow buttons a mnemonic letter
}
#Override
public void actionPerformed(ActionEvent evt) {
model.clear();
}
}
}
class Model {
public static final String NUMBERS_CHANGED = "numbers changed";
private double number1 = 0.0;
private double number2 = 0.0;
private SwingPropertyChangeSupport propChngSupport =
new SwingPropertyChangeSupport(this);
public double getNumber1() {
return number1;
}
public double getNumber2() {
return number2;
}
public void clear() {
setNumber1(0.0);
setNumber2(0.0);
}
// make number1 field a "bound" property, one that notifies listeners if it is changed.
public void setNumber1(double number1) {
Double oldValue = this.number1;
Double newValue = number1;
this.number1 = number1;
propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);
}
// ditto for the number2 field
public void setNumber2(double number2) {
Double oldValue = this.number2;
Double newValue = number2;
this.number2 = number2;
propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);
}
public double calculateProduct() {
return number1 * number2;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propChngSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propChngSupport.removePropertyChangeListener(listener);
}
}
Or maybe better since it uses an array of numbers:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
public class OverlySimpleModelView {
private static void createAndShowGui() {
Model model = new Model(5);
ViewControl viewControl = new ViewControl(model);
JFrame frame = new JFrame("OverlySimpleModelView");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(viewControl.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ViewControl {
private JPanel mainPanel = new JPanel();
private JTextField[] numberFields;
private JTextField productField = new JTextField(5);
private Model model;
public ViewControl(Model model) {
this.model = model;
model.addPropertyChangeListener(new MyPropChngListener());
productField.setEditable(false);
productField.setFocusable(false);
CalculateAction calculateAction = new CalculateAction("Calculate", KeyEvent.VK_C);
numberFields = new JTextField[model.getNumberFieldsLength()];
for (int i = 0; i < numberFields.length; i++) {
numberFields[i] = new JTextField("0.0", 5);
mainPanel.add(numberFields[i]);
numberFields[i].addActionListener(calculateAction);
if (i < numberFields.length - 1) {
mainPanel.add(new JLabel(" + "));
} else {
mainPanel.add(new JLabel(" = "));
}
}
mainPanel.add(productField);
mainPanel.add(new JButton(calculateAction));
mainPanel.add(new JButton(new ClearAction("Clear", KeyEvent.VK_L)));
}
public JComponent getMainComponent() {
return mainPanel;
}
private class MyPropChngListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
for (int i = 0; i < numberFields.length; i++) {
numberFields[i].setText(String.valueOf(model.getNumber(i)));
}
productField.setText(String.valueOf(model.calculateSum()));
}
}
private class CalculateAction extends AbstractAction {
public CalculateAction(String text, int keyCode) {
super(text);
putValue(MNEMONIC_KEY, keyCode);
}
#Override
public void actionPerformed(ActionEvent evt) {
try {
double[] numbers = new double[numberFields.length];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = Double.parseDouble(numberFields[i].getText());
}
model.setNumbers(numbers);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
private class ClearAction extends AbstractAction {
public ClearAction(String text, int keyCode) {
super(text);
putValue(MNEMONIC_KEY, keyCode); // to allow buttons a mnemonic letter
}
#Override
public void actionPerformed(ActionEvent evt) {
model.clear();
}
}
}
class Model {
public static final String NUMBERS_CHANGED = "numbers changed";
private double[] numbers;
private SwingPropertyChangeSupport propChngSupport =
new SwingPropertyChangeSupport(this);
public Model(int length) {
numbers = new double[length];
}
public void setNumbers(double[] numbers) {
double[] oldValue = this.numbers;
double[] newValue = numbers;
this.numbers = numbers;
propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);
}
public double calculateSum() {
double sum = 0.0;
for (double number : numbers) {
sum += number;
}
return sum;
}
public double getNumber(int i) {
return numbers[i];
}
public int getNumberFieldsLength() {
return numbers.length;
}
public void clear() {
double[] newNumbers = new double[numbers.length];
setNumbers(newNumbers);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propChngSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propChngSupport.removePropertyChangeListener(listener);
}
}

Categories