I would like to make in JavaFX a 9x9 sudoku grid like in this image
Any idea how to do it in a nice way? Thanks
Edit:
I managed to do it, but the code doesn't look so good.
private void addBackground(StackPane cell, int row, int col) {
String[] colors = {"#b0cbe1", "#cbe183", "#e18398","#b0e183", "#b8778a", "#e198b0", "#b08398", "#cb98b0", "#e1b0cb"};
if(row < 3) {
if(col < 3) {
cell.setStyle("-fx-background-color: " + colors[0] + ";");
} else if (col >= 3 && col < 6 ) {
cell.setStyle("-fx-background-color: " + colors[1] + ";");
} else{
cell.setStyle("-fx-background-color: " + colors[2] + ";");
}
} else if (row >= 3 && row <6) {
if(col < 3) {
cell.setStyle("-fx-background-color: " + colors[3] + ";");
} else if (col >= 3 && col < 6 ) {
cell.setStyle("-fx-background-color: " + colors[4] + ";");
} else {
cell.setStyle("-fx-background-color: " + colors[5] + ";");
}
} else {
if(col < 3) {
cell.setStyle("-fx-background-color: " + colors[6] + ";");
} else if (col >= 3 && col < 6 ) {
cell.setStyle("-fx-background-color: " + colors[7] + ";");
} else{
cell.setStyle("-fx-background-color: " + colors[8] + ";");
}
}
}
Instead of all those if-else constructs you can use
int colorIndex = 3 * (row / 3) + (col / 3);
cell.setStyle("-fx-background-color: " + colors[colorIndex] + ";");
Note that row / 3 is calculated using integer division, so row / 3 is 0 when row is in {0, 1, 2}, 1 when row is in {3, 4, 5}, and 2 when row is in {6, 7, 8}. (So 3 * (row / 3) is not equal to row. )
Related
Hi I am confused as to what is wrong with my code related to the "edges" filter portion of the problem.
I am able to apply a filter that detects edges. For some reason I fail the check50. I am only able to apply the filter to middle pixels. Any guidance would be much appreciated. I am wondering if I am approaching this problem the incorrect way.
With this code I am just ignoring the calculations for the "black pixels" or the pixels outside of the range of height/width.
Here is my code:
void edges(int height, int width, RGBTRIPLE image[height][width])
{
//create temporary array
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i ++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}
//initialize sobel arrays
int gxarray[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int gyarray[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
//loop through each ith pixel in jth column
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j ++)
{
float gx_red = 0;
float gx_blue = 0;
float gx_green = 0;
float gy_red = 0;
float gy_blue = 0;
float gy_green = 0;
//use the temporary array grid to calculate each gx value
//check if it is a corner or side pixel - and treat that pixel as black pixel
for (int k = -1; k < 2; k ++)
{
for (int l = -1; l < 2; l ++)
{
//calculate the gx and gy for each color by multiply each of
//check if they are corner or sidepixels
if (i + k < 0 || i + k >= height)
{
continue;
}
if (j + l < 0 || j + l >= width)
{
continue;
}
//otherwise calculate each color value
gx_red += temp[i + k][j + l].rgbtRed * gxarray[k + 1][l + 1];
gx_blue += temp[i + k][j + l].rgbtBlue * gxarray[k + 1][l + 1];
gx_green += temp[i + k][j + l].rgbtGreen * gxarray[k + 1][l + 1];
gy_red += temp[i + k][j + l].rgbtRed * gyarray[k + 1][l + 1];
gy_blue += temp[i + k][j + l].rgbtBlue * gyarray[k + 1][l + 1];
gy_green += temp[i + k][j + l].rgbtGreen * gyarray[k + 1][l + 1];
}
}
//times each number by itself then, add them, then square root them
int red = 0 + round(sqrt(gx_red * gx_red + gy_red * gy_red));
int blue = 0 + round(sqrt(gx_blue * gx_blue + gy_blue * gy_blue));
int green = 0 + round(sqrt(gx_green * gx_green + gy_green * gy_green));
image[i][j].rgbtRed = red;
image[i][j].rgbtBlue = blue;
image[i][j].rgbtGreen = green;
//cap it by 255
if (image[i][j].rgbtRed > 255)
{
image[i][j].rgbtRed = 255;
}
if (image[i][j].rgbtBlue > 255)
{
image[i][j].rgbtBlue = 255;
}
if (image[i][j].rgbtGreen > 255)
{
image[i][j].rgbtGreen = 255;
}
}
}
return;
}
```[enter image description here][1]
[1]: https://i.stack.imgur.com/3bExI.png
Hey guys i have been trying to figure out the problem with my code of the pset4 filter (more) about edge detection. My code compiles but the result image looks strange, with a lot of bright/white pixels and very little color pixels mostly at the boundaries of the image. I think i am close to the right result but i just can't figure it out by myself. Could someone plz check it out? appreciate any inputs!
I have treated the edges or corner pixels of the original image by creating copied image with extra lines and columns, which contain only black pixels (rgb values = 0). And i have also used copied image to store the temporary calculation results from the loop.
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// sobel filters
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy[3][3] = {{-1, -2, -1}, {0, 0 ,0}, {1, 2, 1}};
// creates copied image to story the original image and extra lines/columes
// of black pixels to loop through
RGBTRIPLE temp[height + 2][width + 2];
for (int i = 0; i < height + 2; i++)
{
for (int j = 0; j < width + 2; j++)
{
if (i == 0 || j == 0 || i == height + 1 || j == width + 1)
{
temp[i][j].rgbtRed = 0;
temp[i][j].rgbtGreen = 0;
temp[i][j].rgbtBlue = 0;
}
else
{
temp[i][j] = image[i - 1][j - 1];
}
}
}
// second copied image with extra black pixels to store the result
RGBTRIPLE temp2[height + 2][width + 2];
for (int i = 0; i < height + 2; i++)
{
for (int j = 0; j < width + 2; j++)
{
temp2[i][j] = temp[i][j];
}
}
//calculation based on the copied image
for (int i = 1; i < height + 1; i++)
{
// varibles to stroy Gx Gy for each color channels
float blueGx = 0.0, redGx = 0.0, greenGx = 0.0;
float blueGy = 0.0, redGy = 0.0, greenGy = 0.0;
for (int j = 1; j < width + 1; j++)
{
for (int k = -1; k < 2; k++)
{
for (int h = -1; h < 2; h++)
{
//calculate the Gx for each R G B channel by using temp
blueGx += temp[i + k][j + h].rgbtBlue * Gx[k + 1][h + 1];
redGx += temp[i + k][j + h].rgbtRed * Gx[k + 1][h + 1];
greenGx += temp[i + k][j + h].rgbtGreen * Gx[k + 1][h + 1];
//calculate the Gy for each R G B channel by using temp
blueGy += temp[i + k][j + h].rgbtBlue * Gy[k + 1][h + 1];
redGy += temp[i + k][j + h].rgbtRed * Gy[k + 1][h + 1];
greenGy += temp[i + k][j + h].rgbtGreen * Gy[k + 1][h + 1];
}
}
//store result for each pixels (i, j) in temp2
temp2[i][j].rgbtRed = maxCheck(round(sqrt(redGx * redGx + redGy * redGy)));
temp2[i][j].rgbtBlue = maxCheck(round(sqrt(blueGx * blueGx + blueGy * blueGy)));
temp2[i][j].rgbtGreen = maxCheck(round(sqrt(greenGx * greenGx + greenGy * greenGy)));
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = temp2[i + 1][j + 1];
}
}
}
// cap rgb value to 255
int maxCheck(int a)
{
if (a > 255)
{
a = 255;
}
return a;
}
by using check50 i got four errors:
:( edges correctly filters middle pixel
expected "210 150 60\n", not "255 250 255\n"
:( edges correctly filters pixel on edge
expected "213 228 255\n", not "255 255 255\n"
:( edges correctly filters 3x3 image
expected "76 117 255\n21...", not "76 117 255\n25..."
:( edges correctly filters 4x4 image
expected "76 117 255\n21...", not "76 117 255\n25..."
seeing from the results a lot of pixels have been calculated up to 255. That explains all the white pixels in the result image but i don't understand why. Something wrong with the math? i think i did pay attention to the number range of rgb values and the difference between int and float number.
check50
just found out the problem: the reset of blueGx, redGx etc. is at the wrong position, which doesn't reset them properly at the beginning of each loop for a new pixel.
I am getting system out println of variable "j" up to ten times in a row, but I don't see any reason for it.
Here is my code:
copyFolderButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
String dateStart = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
j = 0.00;
ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.schedule(new Runnable() {
#Override
public void run() {
List<String> finallist = copyList.getItems();
totalComputersCopy = copyList.getItems().size();
successRateCopy = 0;
double a = 1.00;
double i = (1.00 / finallist.size());
ArrayList<String> selectedIDs = new ArrayList<String>();
try {
for (String row : finallist) {
a++;
j = j + i;
if (a % 10 == 0) {
Thread.sleep(1500);
}
System.out.println(a);
copythread = new Thread("" + a) {
public void run() {
try {
System.out.println(j);
javafx.application.Platform.runLater(() -> copyGauge.setValue(j * 100));
} catch (Exception ex) {
}
Process p;
try {
String pathRaw = copyFolderPath.getText();
pathRaw = pathRaw.replace(":", "$");
if (pathRaw != null && pathRaw.length() > 0 && pathRaw.charAt(pathRaw.length() - 1) == '\\') {
pathRaw = pathRaw.substring(0, pathRaw.length() - 1);
}
String source = copyFolderSource.getText();
if (source != null && source.length() > 0 && source.charAt(source.length() - 1) == '\\') {
source = source.substring(0, source.length() - 1);
}
InetAddress ia = InetAddress.getByName("172.217.23.206");
boolean b = ia.isReachable(2000);
if (b) {
//System.out.println(j*100);
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "powershell -Command \"Copy-Item '" + source + "' -Destination '" + "\\\\" + row + "\\" + pathRaw + "' -Recurse -Verbose\"");
p = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuilder builders = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builders.append(line);
builders.append("\n");
}
String DeleteLog = builders.toString();
if (DeleteLog.trim().isEmpty()) {
successRateCopy++;
javafx.application.Platform.runLater(() -> copyFolderLog.appendText(row + ": " + "Succesfully copied" + "\n"));
} else {
javafx.application.Platform.runLater(() -> copyFolderLog.appendText(row + ": " + DeleteLog + "\n"));
}
p.waitFor(15, TimeUnit.SECONDS);
p.destroy();
} else {
javafx.application.Platform.runLater(() -> copyFolderLog.appendText("Cannot reach " + row + "\n"));
System.out.println("Cannot reach " + row + "\n");
}
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.toString());
} catch (InterruptedException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
Thread.currentThread().interrupt();
}
}
};
copythread.start();
}
} catch (Exception e) {
} finally {
while (copythread.getState() != Thread.State.TERMINATED) {
}
String successRateLog = String.valueOf(successRateCopy);
String failedRateLog = String.valueOf(totalComputersCopy - successRateCopy);
String dateStop = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
if ((diff / (60 * 60 * 1000) % 24) != 0) {
deleteTime = "Total time: " + String.valueOf(diffHours) + " hours, " + String.valueOf(diffMinutes) + " minutes, " + String.valueOf(diffSeconds) + " seconds";
} else {
if ((diff / (60 * 1000) % 60) != 0) {
deleteTime = "Total time: " + String.valueOf(diffMinutes) + " minutes, " + String.valueOf(diffSeconds) + " seconds";
} else {
if ((diff / 1000 % 60) != 0) {
deleteTime = "Total time: " + String.valueOf(diffSeconds) + " seconds";
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
Platform.runLater(() -> {
VBox container = new VBox();
container.setPadding(new Insets(10, 10, 10, 10));
Label icon = new Label();
Image image = new Image(getClass().getResourceAsStream("img/copyicon.png"));
icon.setGraphic(new ImageView(image));
container.getChildren().add(icon);
Notifications.create()
.darkStyle()
.title("Copy Folder")
.text("Workcomplete!" + "\n" + deleteTime + "\n" + "Success: " + successRateLog + "\n" + "Failed: " + failedRateLog)
.hideAfter(Duration.seconds(30))
.graphic(container)
.show();
}
);
}
}
}, 1, TimeUnit.SECONDS);
}
});
I am not sure that this is the best way on how to play with threads. It's working good, but I am aware what's wrong with printing variables in my code, it looks like this:
2.0
3.0
4.0
5.0
6.0
0.03333333333333333
7.0
0.04
0.04
0.04
0.04666666666666667
8.0
9.0
0.060000000000000005
0.060000000000000005
0.060000000000000005
10.0
11.0
12.0
13.0
14.0
15.0
16.0
17.0
18.0
19.0
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
0.12666666666666668
"A" is printing ok, but "j" grows to ten lines and keeps on them.
I have been trying to use Markov Chain to improve my model and get trouble when computing transition matrix. It appears missing values. Someone know why my code is wrong? Many thanks
I already defined all the variables to be 0 at first.
mresiduals is residuals of my model. len is the length of vector(residuals).
Error message is:
Error in if (mresiduals[ele + 1] < lim5) { :
missing value where TRUE/FALSE needed
for (ele in 1:len) {
if (mresiduals[ele] < lim5)
{
p1 = p1 + 1
if (mresiduals[ele + 1] < lim5)
{
p1I = p1I + 1
} else if (mresiduals[ele + 1] > lim5 & mresiduals[ele + 1] < lim4)
{
p1II = p1II + 1
} else if (mresiduals[ele + 1] > lim4 & mresiduals[ele + 1] < lim3)
{
p1III = p1III + 1
} else if (mresiduals[ele + 1] > lim3 & mresiduals[ele + 1] < lim2)
{
p1IV = p1IV + 1
} else{
p1V = p1V + 1
}
} else if (ele > lim5 & ele < lim4)
{
p2 = p2 + 1
if (mresiduals[ele + 1] < lim5)
{
p2I = p2I + 1
} else if (mresiduals[ele + 1] > lim5 & mresiduals[ele + 1] < lim4)
{
p2II = p2II + 1
} else if (mresiduals[ele + 1] > lim4 & mresiduals[ele + 1] < lim3)
{
p2III = p2III + 1
} else if (mresiduals[ele + 1] > lim3 & mresiduals[ele + 1] < lim2)
{
p2IV = p2IV + 1
} else {
p2V = p2V + 1
}
} else if (ele > lim4 & ele < lim3)
{
p3 = p3 + 1
if (mresiduals[ele + 1] < lim5)
{
p3I = p3I + 1
} else if (mresiduals[ele + 1] > lim5 & mresiduals[ele + 1] < lim4)
{
p3II = p3II + 1
} else if (mresiduals[ele + 1] > lim4 & mresiduals[ele + 1] < lim3)
{
p3III = p3III + 1
} else if (mresiduals[ele + 1] > lim3 & mresiduals[ele + 1] < lim2)
{
p3IV = p3IV + 1
} else{
p3V = p3V + 1
}
} else if (ele > lim4 & ele < lim3)
{
p4 = p4 + 1
if (mresiduals[ele + 1] < lim5)
{
p4I = p4I + 1
} else if (mresiduals[ele + 1] > lim5 & mresiduals[ele + 1] < lim4)
{
p4II = p4II + 1
} else if (mresiduals[ele + 1] > lim4 & mresiduals[ele + 1] < lim3)
{
p4III = p4III + 1
} else if (mresiduals[ele + 1] > lim3 & mresiduals[ele + 1] < lim2)
{
p4IV = p4IV + 1
} else{
p4V = p4V + 1
}
} else{
p5 = p5 + 1
if (mresiduals[ele + 1] < lim5)
{
p5I = p5I + 1
} else if (mresiduals[ele + 1] > lim5 & mresiduals[ele + 1] < lim4)
{
p5II = p5II + 1
} else if (mresiduals[ele + 1] > lim4 & mresiduals[ele + 1] < lim3)
{
p5III = p5III + 1
} else if (mresiduals[ele + 1] > lim3 & mresiduals[ele + 1] < lim2)
{
p5IV = p5IV + 1
} else{
p5V = p5V + 1
}
}
}
When R finds NA during its execution and try to compare that with some other element results in this error. In current case mresiduals is of length len so in line 5 mresiduals[ele + 1] when ele loop reaches len; ele+1 becomes len+1 outside the boundary of mresiduals and hence the error.
when user uncheckitems items.how we get that uncheckitems
for (int i = 0; i < ChkLstUserRoles.Items.Count; i++)
{
if (ChkLstUserRoles.Items[i].Selected == true)
{
URoles = URoles + "|" + ChkLstUserRoles.Items[i].Value;
ActivityLog = ActivityLog + "Grant the " + ChkLstUserRoles.Items[i].Text + " to user [" + CboUserName.Text + "]çGrantRole|";
}
}
Use this condition,
ChkLstUserRoles.Items[i].Selected == false