Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java prog (use JavaFx) Design and implement a class UpDownPane where the pane co

ID: 3678484 • Letter: J

Question

Java prog (use JavaFx)

Design and implement a class UpDownPane where the pane contains a pattern of 7x7 smaller panes. The small pane in the middle of the big pane will, once the program is started, contain a small icon (or image). When an arrow key is pressed, the icon will move a step up/down or right/left, to a neighboring small pane. If the icon is moved out of the bigger pane, then the icon will appear on the other side of the bigger pane (i.e. if the icon is moved upwards when in an uppermost small pane, then it will appear on the corresponding small panel in the bottom row). Also write a test program UpDownMain.java, starting a window containing an UpDownPanel

Explanation / Answer

package p1caccia.test;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class SampleGUI {

   private final JPanel gui = new JPanel(new BorderLayout(3, 3));
   private static JButton[][] simpleSquare = new JButton[7][7];
   private JPanel chessBoard;
   final static int[] currPos = { 3, 3 };
   SampleGUI() {
       initializeGui();
   }

   public final void initializeGui() {
       // set up the main GUI
       gui.setBorder(new EmptyBorder(5, 5, 5, 5));
       JToolBar tools = new JToolBar();
       tools.setFloatable(false);
       gui.add(tools, BorderLayout.PAGE_START);
      
       chessBoard = new JPanel(new GridLayout(0, 7)) {

           private static final long serialVersionUID = 1L;

           /**
           * Override the preferred size to return the largest it can, in a
           * square shape. Must (must, must) be added to a GridBagLayout as
           * the only component (it uses the parent as a guide to size) with
           * no GridBagConstaint (so it is centered).
           */
           @Override
           public final Dimension getPreferredSize() {
               Dimension d = super.getPreferredSize();
               Dimension prefSize = null;
               Component c = getParent();
               if (c == null) {
                   prefSize = new Dimension((int) d.getWidth(), (int) d.getHeight());
               } else if (c != null && c.getWidth() > d.getWidth() && c.getHeight() > d.getHeight()) {
                   prefSize = c.getSize();
               } else {
                   prefSize = d;
               }
               int w = (int) prefSize.getWidth();
               int h = (int) prefSize.getHeight();
               // the smaller of the two sizes
               int s = (w > h ? h : w);
               return new Dimension(s, s);
           }
       };
       chessBoard.setBorder(new CompoundBorder(new EmptyBorder(7, 7, 7, 7), new LineBorder(Color.BLACK)));
       // Set the BG to be ochre
       Color ochre = new Color(204, 119, 34);
       chessBoard.setBackground(ochre);
       JPanel boardConstrain = new JPanel(new GridBagLayout());
       boardConstrain.setBackground(ochre);
       boardConstrain.add(chessBoard);
       gui.add(boardConstrain);
       // create the chess board squares
       Insets buttonMargin = new Insets(0, 0, 0, 0);
       for (int ii = 0; ii < simpleSquare.length; ii++) {
           for (int jj = 0; jj < simpleSquare[ii].length; jj++) {
               JButton b = new JButton();
               b.setMargin(buttonMargin);
               // our chess pieces are 64x64 px in size, so we'll
               // 'fill this in' using a transparent icon..
               ImageIcon icon = new ImageIcon(new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
               b.setIcon(icon);
               b.setBackground(Color.WHITE);
               if (ii == jj && ii == 3) {
                   b.setBackground(Color.BLACK);
               }
               simpleSquare[jj][ii] = b;
           }
       }
       // fill the black non-pawn piece row
       for (int ii = 0; ii < 7; ii++) {
           for (int jj = 0; jj < 7; jj++) {
               chessBoard.add(simpleSquare[jj][ii]);
           }
       }
   }

   public final JComponent getGui() {
       return gui;
   }

   public static void main(String[] args) {
       Runnable r = new Runnable() {

           @Override
           public void run() {
               SampleGUI cg = new SampleGUI();

               JFrame f = new JFrame("ChessChamp");
               f.add(cg.getGui());
               // Ensures JVM closes after frame(s) closed and
               // all non-daemon threads are finished
               f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
               // See http://stackoverflow.com/a/7143398/418556 for demo.
               f.setLocationByPlatform(true);
               // ensures the frame is the minimum size it needs to be
               // in order display the components within it
               f.pack();
               // ensures the minimum size is enforced.
               f.setMinimumSize(f.getSize());
               f.setVisible(true);
           }
       };
       // Swing GUIs should be created and updated on the EDT
       // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
       SwingUtilities.invokeLater(r);
      
       Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
            public void eventDispatched(AWTEvent event) {
                if(event.getID() == KeyEvent.KEY_PRESSED) {
                   KeyEvent e = (KeyEvent) event;
                   for (int ii = 0; ii < 7; ii++) {
                       for (int jj = 0; jj < 7; jj++) {
                           simpleSquare[jj][ii].setBackground(Color.white);
                       }
                   }
                   int keyCode = e.getKeyCode();
                   switch (keyCode) {
                   case KeyEvent.VK_UP:
                       if (currPos[1] != 0) {
                           currPos[0] = currPos[0];
                           currPos[1] = currPos[1] - 1;
                       }
                       break;
                   case KeyEvent.VK_RIGHT:
                       if (currPos[0] != 6) {
                           currPos[0] = currPos[0]+1;
                           currPos[1] = currPos[1] ;
                       }
                       break;
                   case KeyEvent.VK_LEFT:
                       if (currPos[0] != 0) {
                           currPos[0] = currPos[0]-1;
                           currPos[1] = currPos[1];
                       }
                       break;
                   case KeyEvent.VK_DOWN:
                       if (currPos[1] != 6) {
                           currPos[0] = currPos[0];
                           currPos[1] = currPos[1] + 1;
                       }
                       break;
                   }
                   simpleSquare[currPos[0]][currPos[1]].setBackground(Color.BLACK);
                }
            }
        }, AWTEvent.KEY_EVENT_MASK);
   }

}

BLack box can be considered as image. Let me know if you require modifications.

package p1caccia.test;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

import javax.script.ScriptEngine;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class SampleGUI extends Application {

   int currPos[] = { 3, 3 };

   @SuppressWarnings("restriction")
   @Override
   public void start(Stage primaryStage) {
       final int size = 7;
       javafx.scene.control.Button b[][] = new javafx.scene.control.Button[7][7];
       for (int row = 0; row < size; row++) {
           for (int col = 0; col < size; col++) {
               b[row][col] = new javafx.scene.control.Button();
               b[row][col].setStyle("-fx-background-color: white;");
           }
       }
       GridPane root = new GridPane();
       for (int row = 0; row < size; row++) {
           for (int col = 0; col < size; col++) {
               if (row == col && row == 3) {
                   b[row][col].setStyle("-fx-background-color: black;");
               }
               root.add(b[row][col], col, row);
           }
       }
       for (int i = 0; i < size; i++) {
           root.getColumnConstraints().add(new ColumnConstraints(5, Control.USE_COMPUTED_SIZE,
                   Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));
           root.getRowConstraints().add(new RowConstraints(5, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY,
                   Priority.ALWAYS, VPos.CENTER, true));
       }
       Scene scene = new Scene(root, 400, 400);
       primaryStage.setScene(scene);
       primaryStage.show();

       scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
           @Override
           public void handle(KeyEvent event) {
               for (int row = 0; row < size; row++) {
                   for (int col = 0; col < size; col++) {
                       b[row][col] = new javafx.scene.control.Button();
                       b[row][col].setStyle("-fx-background-color: black;");
                       b[row][col].setBackground(javafx.scene.layout.Background.EMPTY);
                   }
               }
               switch (event.getCode()) {
               case UP:
                   if (currPos[1] != 0) {
                       currPos[0] = currPos[0];
                       currPos[1] = currPos[1] - 1;
                   }
                   break;
               case DOWN:
                   if (currPos[1] != 6) {
                       currPos[0] = currPos[0];
                       currPos[1] = currPos[1] + 1;
                   }
                   break;
               case LEFT:
                   if (currPos[0] != 0) {
                       currPos[0] = currPos[0] - 1;
                       currPos[1] = currPos[1];
                   }
                   break;
               case RIGHT:
                   if (currPos[0] != 6) {
                       currPos[0] = currPos[0] + 1;
                       currPos[1] = currPos[1];
                   }
                   break;
               }
               b[currPos[0]][currPos[1]].setStyle("-fx-background-color: black;");
           }
       });

   }

   public static void main(String[] args) {
       launch(args);
   }
}

Code is somehow not working. I will fix this.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote