Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java, I'm creating a word Search program I'm trying to get the word Search to print the words in reverse so dragon, nogard, I belive

Java, I'm creating a word Search program I'm trying to get the word Search to print the words in reverse so dragon, nogard, I belive it is working, but the words won't appear on my word Search here is all of my code fillArrayWithWords is the method to look at, it's in a GUI

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package wordsearch;

import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Image; import java.util.Random; import javax.swing.ImageIcon;

/** * * @author Josh */ public class wordSearchFrame extends javax.swing.JFrame {

Graphics draw; Font wordFont = new Font("Tahoma", 0, 24); String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int letHeight, letWidth; FontMetrics fontMetrics; //letter info Image graph; ImageIcon lblGraph; int row = 0, col = 0, ind = 0; String let; int letSize = 15; String[][] placed = new String[letSize][letSize]; int wordToPlace = 0; Dimension board; Random rnd = new Random();

/** * Creates new form wordSearchFrame */ public wordSearchFrame() { initComponents(); // board = new Dimension(500, 500); // graph = createImage(board.width, board.height); // draw = graph.getGraphics(); // draw.setFont(wordFont); // fontMetrics = draw.getFontMetrics(wordFont); // letHeight = fontMetrics.getHeight(); // letWidth = fontMetrics.charWidth('W'); // // draw.clearRect(0, 0, board.width, board.width); // draw.setColor(Color.white); // draw.fillRect(0, 0, board.width, board.width); // // draw.setColor(Color.RED); // drawWordsOnImage(); // // //draw.drawString("It works", 100, 100); // lblGraph = new ImageIcon(graph); // lblGraphout.setIcon(lblGraph); }

public void blankArray() { for (int i = 0; i

public void drawWordsOnImage() { row = letHeight * 2; col = letWidth / 2; draw.clearRect(0, 0, board.width, board.width); draw.setColor(Color.white); draw.fillRect(0, 0, board.width, board.width);

draw.setColor(Color.RED); blankArray();

fillArrayWithWords(); //fillArrayWithJunk(); for (int i = 0; i

} col = letWidth / 2; row += letHeight; } }

public void fillArrayWithWords() { int up, row, col, start = 0, diff = 0, used = 0; int random = (int) Math.random() * placed.length; for (int x = 0; x

this.listPanel.setSelectedIndex(wordToPlace); String word = this.listPanel.getSelectedValue().toString(); up = rnd.nextInt(2); switch (3) { case 0: do { used = 0; row = rnd.nextInt(letSize); diff = letSize - word.length(); start = rnd.nextInt(diff); for (int i = 0; i 0); for (int i = 0; i 0); for (int i = 0; i

String[] backwards = reverse.split(""); String s = ""; for (int i = backwards.length - 1; i >= 0; i--) { s += backwards[i]; } placed[random][random] = s; do { used = 0; col = rnd.nextInt(letSize); diff = letSize - word.length(); start = rnd.nextInt(diff); for (int i = 0; i 0); for (int i = 0; i

public void fillArrayWithJunk() { for (int i = 0; i

if (placed[i][j] == "-") { ind = rnd.nextInt(letters.length()); placed[i][j] = letters.substring(ind, ind + 1); } } } }

/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane(); listPanel = new javax.swing.JList(); lblGraphout = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); enterWordButton = new javax.swing.JButton(); wordBox = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

listPanel.setModel(new javax.swing.AbstractListModel() { String[] strings = { "DRAGON", "WOLF", "BEAR", "TURTLE", "DOLPHIN", "CAT", "SHARK", "WHALE", "STUDENT", "FUNNY" }; public int getSize() { return strings.length; } public String getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(listPanel);

jButton1.setText("Build Word Search"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });

enterWordButton.setText("Enter Word"); enterWordButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { enterWordButtonActionPerformed(evt); } });

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1) .addGroup(layout.createSequentialGroup() .addComponent(enterWordButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(wordBox))) .addGap(165, 165, 165) .addComponent(lblGraphout, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblGraphout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(enterWordButton) .addComponent(wordBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 21, Short.MAX_VALUE))) .addContainerGap()) );

pack(); }//

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: board = new Dimension(500, 500); graph = createImage(board.width, board.height); draw = graph.getGraphics(); draw.setFont(wordFont); fontMetrics = draw.getFontMetrics(wordFont); letHeight = fontMetrics.getHeight(); letWidth = fontMetrics.charWidth('W');

draw.clearRect(0, 0, board.width, board.width); draw.setColor(Color.white); draw.fillRect(0, 0, board.width, board.width);

draw.setColor(Color.RED); drawWordsOnImage();

//draw.drawString("It works", 100, 100); lblGraph = new ImageIcon(graph); lblGraphout.setIcon(lblGraph); }

private void enterWordButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //this.listPanel }

image text in transcribedHere is my output

Repeat DRAGON BEAR TURTLE DOLPHIN CAT ES S A CTH R A U A STUDENT FUNNY T D R WH A L E Build Word Search F UN N Y Enter Word Repeat DRAGON BEAR TURTLE DOLPHIN CAT ES S A CTH R A U A STUDENT FUNNY T D R WH A L E Build Word Search F UN N Y Enter Word

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions