Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the find() method to find the path from starting point and the destination (update the data array) Paint Brush import java.awt.event.MouseEvent; import java.util.Scanner; import

Complete the find() method to find the path from starting point and the destination (update the data array)

Paint Brush

import java.awt.event.MouseEvent; import java.util.Scanner;

import javax.swing.SwingUtilities;

public class Brush { Matrix matrix; int[] data; int h, w; public Brush(int[] data, int h, int w) { this.data = data; this.h = h; this.w = w; // create a Matrix instance and override its mousePressed() method matrix = new Matrix(data, h, w) { @Override public void mousePressed(MouseEvent e) { Brick b = (Brick) e.getSource(); if (b == bkColor) { int c = (b.getColor() + 1) % colors.length; SwingUtilities.invokeLater(() -> b.setColor(c)); } else SwingUtilities.invokeLater(() -> setColor(b.getIndex(), bkColor.getColor())); } }; matrix.enableMouseClick(matrix); } public void setColor(int idx, int newColor) { if (data[idx] != newColor) setColor(idx, newColor, data[idx]); } private void setColor(int idx, int newColor, int oriColor) { // TODO: // 1. Use recursion // 2. Update values of the data array // 3. Call matrix.refresh(idx) to refresh the color of the brick

}

public static void main(String[] args) { // A single-dimension array is used for storing the data of a matrix. int[] data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 7, 0, 0, 7, 0, 0, 4, 4, 4, 0, 0, 0, 0, 0, 7, 7, 0, 0, 7, 7, 0, 0, 4, 4, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 2, 7, 7, 8, 7, 0, 11, 11, 11, 0, 0, 0, 3, 3, 7, 7, 7, 7, 7, 7, 11, 11, 11, 11, 0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7, 11, 11, 11, 11, 3, 3, 3, 3, 3, 3, 3, 7, 7, 11, 11, 11, 11, 11, 11, 3, 3, 7, 7, 3, 3, 7, 7, 7, 7, 11, 11, 11, 11, 11, 3, 7, 7, 3, 3, 3, 7, 7, 7, 7, 11, 11, 11, 11, 11, 3, 7, 3, 3, 3, 7, 7, 7, 7, 7, 7, 11, 11, 11, 11, 3, 7, 7, 3, 3, 7, 7, 7, 7, 7, 7, 11, 11, 11, 11, 3, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 11, 11, 11, 11 }; // A Brush instance is created with the 15x15 matrix data. Brush brush = new Brush(data, 15, 15); // You can type "print" in the console to print the data array. Scanner scanner = new Scanner(System.in); while(true) { if (scanner.nextLine().equals("print")) brush.matrix.print(); } } }

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

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 1 Lncs 8055

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013 Edition

3642402844, 978-3642402845

More Books

Students also viewed these Databases questions