I am making a Sudoku Solver in a JavaFx Application. I have got the grid ready as shown below, but I don't know how to add the center border to distinguish between the smaller boxes.
Like in a normal sudoku, I want to get the border between the smaller boxes.
Here's is the code:-
private GridPane getGridPane() {
GridPane gridPane = new GridPane();
gridPane.setGridLinesVisible(true);
gridPane.setPadding(new Insets(10));
gridPane.setHgap(0);
gridPane.setHgap(0);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
buttons[i][j] = new Label("");
buttons[i][j].setStyle("-fx-font-weight: bold; -fx-font-size: 40");
buttons[i][j].setAlignment(Pos.CENTER);
buttons[i][j].setTextAlignment(TextAlignment.CENTER);
buttons[i][j].setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}
...
for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) GridPane.setConstraints(buttons[i][j], j, i);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) gridPane.getChildren().add(buttons[i][j]);
GridPane.setHalignment(gridPane, HPos.CENTER);
GridPane.setValignment(gridPane, VPos.CENTER);
gridPane.setAlignment(Pos.CENTER);
return gridPane;
}
Related
I am trying to make a little algorithm that colors a point a certain color based on what side of a line the point is on. This is what i have at the moment. The code doesnt give any errors, but the colors also arent correct for the dots.. Could someone point out to me what i am doing wrong?
See the code:
PVector[] points;
void setup() {
size(500, 500);
points = new PVector[10];
for (int i = 0; i < points.length; i++) {
points[i] = new PVector(random(0, width), random(0, height));
}
ExtremesLine(points);
}
void ExtremesLine(PVector[] pts) {
float maxx = 0, minx = width+1;
PVector min = new PVector(), max = new PVector();
ArrayList<PVector> groupA = new ArrayList<PVector>(), groupB = new ArrayList<PVector>();
for (int i = 0; i < pts.length; i++) {
if (pts[i].x > maxx) {
maxx = pts[i].x;
max = pts[i];
}
if (pts[i].x < minx) {
minx = pts[i].x;
min = pts[i];
}
}
PVector divisionLine = new PVector();
PVector.sub(max, min, divisionLine);
PVector normal = new PVector(-divisionLine.y, divisionLine.x).normalize();
for (int i = 0; i < pts.length; i++) {
float s = PVector.dot(normal, pts[i].copy().normalize());
if ( s < 0) groupA.add(pts[i]);
else if ( s > 0) groupB.add(pts[i]);
}
fill(0);
line(min.x, min.y, max.x, max.y);
for (int i = 0; i < groupA.size(); i++) {
fill(255, 0, 0);
ellipse(groupA.get(i).x, groupA.get(i).y, 10, 10);
}
for (int i = 0; i < groupB.size(); i++) {
fill(0, 0, 255);
ellipse(groupB.get(i).x, groupB.get(i).y, 10, 10);
}
}
As you can see from the images below sometimes it works but 90% of the time it doesnt. First image is the correct result, second image is the incorrect result
If there is anything unclear pls let me know so i can clarify!
You need to dot with vectors from min:
replace
float s = PVector.dot(normal, pts[i].copy().normalize());
by
float s = PVector.dot(normal, pts[i].copy().sub(min).normalize());
and it will work as expected:
Tangentially, since min and max are Processing built-ins, are you sure that you want to use them as variable names?
I have a QGraphicsScene. In that scene I have a rects and I want to set different colors in that rects.
My code is
QGraphicsScene* scene;
scene = new QGraphicsScene(this);
QGraphicsView* view;
view = new QGraphicsView(this);
view->setGeometry(x, y, mapColumns * min_height_size, mapRows * min_width_size);
view->setScene(scene);
for(int i=0; i<mapRows; ++i){
for(int j=0; j<mapColumns; ++j){
if (j == 0) {
y = 20;
}
if(map.getCell(i,j).getTerrain() == Cell::forest) {
scene->addRect(x,y,min_height_size,min_width_size);
scene->setPalette(Qt::green); //but it didn't work
y += min_width_size;
} else if(map.getCell(i,j).getTerrain() == Cell::mountain){
scene->addRect(x,y,min_height_size,min_width_size);
scene->setPalette(Qt::blue); //but it didn't work
y += min_width_size;
} else if(map.getCell(i,j).getTerrain() == Cell::plain) {
scene->addRect(x,y,min_height_size,min_width_size);
scene->setPalette(Qt::yellow); //but it didn't work
y += min_width_size;
}
view->setDragMode(QGraphicsView::ScrollHandDrag);
}
x += min_height_size;
}
}
You can use the setBrush() function to set the color.
QGraphicsRectItem *rect_item1 = scene->addRect(100, 100, 50, 50);
rect_item1->setBrush(Qt::yellow);
QGraphicsRectItem *rect_item2 = scene->addRect(200, 250, 50, 50);
rect_item2->setBrush(QColor(100,50,200));
I'm having a dilemma with this code and have no clue what to do. I'm pretty new to processing. This is a project from this link...
http://blog.makezine.com/2012/08/10/build-a-touchless-3d-tracking-interface-with-everyday-materials/
any help is massively appreciated... Thanks in advance
import processing.serial.*;
import processing.opengl.*;
Serial serial;
int serialPort = 1;
int sen = 3; // sensors
int div = 3; // board sub divisions
Normalize n[] = new Normalize[sen];
MomentumAverage cama[] = new MomentumAverage[sen];
MomentumAverage axyz[] = new MomentumAverage[sen];
float[] nxyz = new float[sen];
int[] ixyz = new int[sen];
float w = 256; // board size
boolean[] flip = {
false, true, false};
int player = 0;
boolean moves[][][][];
PFont font;
void setup() {
size(800, 600, P3D);
frameRate(25);
font = loadFont("TrebuchetMS-Italic-20.vlw");
textFont(font);
textMode(SCREEN);
println(Serial.list());
serial = new Serial(this, Serial.list()[serialPort], 115200);
for(int i = 0; i < sen; i++) {
n[i] = new Normalize();
cama[i] = new MomentumAverage(.01);
axyz[i] = new MomentumAverage(.15);
}
reset();
}
void draw() {
updateSerial();
drawBoard();
}
void updateSerial() {
String cur = serial.readStringUntil('\n');
if(cur != null) {
String[] parts = split(cur, " ");
if(parts.length == sensors) {
float[] xyz = new float[sen];
for(int i = 0; i < sen; i++)
xyz[i] = float(parts[i]);
if(mousePressed && mouseButton == LEFT)
for(int i = 0; i < sen; i++)
n[i].note(xyz[i]);
nxyz = new float[sen];
for(int i = 0; i < sen; i++) {
float raw = n[i].choose(xyz[i]);
nxyz[i] = flip[i] ? 1 - raw : raw;
cama[i].note(nxyz[i]);
axyz[i].note(nxyz[i]);
ixyz[i] = getPosition(axyz[i].avg);
}
}
}
}
float cutoff = .2;
int getPosition(float x) {
if(div == 3) {
if(x < cutoff)
return 0;
if(x < 1 - cutoff)
return 1;
else
return 2;
}
else {
return x == 1 ? div - 1 : (int) x * div;
}
}
void drawBoard() {
background(255);
float h = w / 2;
camera(
h + (cama[0].avg - cama[2].avg) * h,
h + (cama[1].avg - 1) * height / 2,
w * 2,
h, h, h,
0, 1, 0);
pushMatrix();
noStroke();
fill(0, 40);
translate(w/2, w/2, w/2);
rotateY(-HALF_PI/2);
box(w);
popMatrix();
float sw = w / div;
translate(h, sw / 2, 0);
rotateY(-HALF_PI/2);
pushMatrix();
float sd = sw * (div - 1);
translate(
axyz[0].avg * sd,
axyz[1].avg * sd,
axyz[2].avg * sd);
fill(255, 160, 0);
noStroke();
sphere(18);
popMatrix();
for(int z = 0; z < div; z++) {
for(int y = 0; y < div; y++) {
for(int x = 0; x < div; x++) {
pushMatrix();
translate(x * sw, y * sw, z * sw);
noStroke();
if(moves[0][x][y][z])
fill(255, 0, 0, 200);
else if(moves[1][x][y][z])
fill(0, 0, 255, 200);
else if(
x == ixyz[0] &&
y == ixyz[1] &&
z == ixyz[2])
if(player == 0)
fill(255, 0, 0, 200);
else
fill(0, 0, 255, 200);
else
fill(0, 100);
box(sw / 3);
popMatrix();
}
}
}
fill(0);
if(mousePressed && mouseButton == LEFT)
msg("defining boundaries");
}
void keyPressed() {
if(key == TAB) {
moves[player][ixyz[0]][ixyz[1]][ixyz[2]] = true;
player = player == 0 ? 1 : 0;
}
}
void mousePressed() {
if(mouseButton == RIGHT)
reset();
}
void reset() {
moves = new boolean[2][div][div][div];
for(int i = 0; i < sen; i++) {
n[i].reset();
cama[i].reset();
axyz[i].reset();
}
}
void msg(String msg) {
text(msg, 10, height - 10);
}
You are missing a class, in fact, more than one. Go back to the github and download, or copy and paste, all three codes, placing each one in a new tab named same name of the class (well this is not required, but is a good practice). The TicTacToe3D.pde is the main code. To make a new tab choose "new tab" from the arrow menu in Processing IDE (just below the standard button at the right). The code should run. WIll need an Arduino though to really get it working.
i have a greyscale image and i want to scan the pixels out of the Image and this is what i get :
var i:int;
var j:int;
for (i = 0; i < img.contentWidth ; i++)
{
for(j = 0; j < img.contentHeight; j++){
pixeldaten.addItem({x:i,y:j,pixel:bmd.getPixel(i,j)});
}
}
but the table doesn't look like RGB Values . (R , B , and G must be the same)
: example
getPixel should return the hex value value of the pixel, you could then do something like
// get the red value
bmd.getPixel(i,j) >> 16
//for Image processing
Bitmap myBitmap = new Bitmap(CurrentBitmap);
int imgH = myBitmap.Height;
int imgW = myBitmap.Width;
ARed = new double[imgH, imgW];
AGreen = new double[imgH, imgW];
ABlue = new double[imgH, imgW];
doubles = new double[imgH, imgW];
var max = new double[imgH, imgW];
var min = new double[0, 0];
//seperating each RGB components
for (int x = 0; x < imgH; x++)
{
for (int y = 0; y < imgW; y++)
{
Color color = myBitmap.GetPixel(x, y);
// things we do with pixelColor
//ARed[x][y] = myBitmap.GetPixel >> 16;
ARed[x, y] = color.R;
ABlue[x, y] = color.B;
AGreen[x, y] = color.G;
max[x, y] = ARed[x, y];
}
}
Bitmap bmp = new Bitmap(pictureBox1.Image);
bmp.getPixel(i,j).R
I am adding texboxes into a table (type Table) but I can't add them. I can't add more than one cell to each row, any idea?
TextBox[] tx = new TextBox[10];
TableCell[] tc = new TableCell[10];
TableRow[] tr = new TableRow[10];
for (int i = 0; i < 10; i++)
{
tx[i] = new TextBox();
tc[i] = new TableCell();
tc[i].Controls.Add(tx[i]);
}
for (int i = 0; i < 10; i++)
{
tr[i] = new TableRow();
tr[i].Cells.Add(tc[i]);
}
for (int i = 0; i < 10; i++)
Table1.Rows.Add(tr[i]);
It comes out like 10 rows each having only 1 cell
Because you need an inner loop on this:
for (int i = 0; i < 10; i++)
{
tr[i] = new TableRow();
tr[i].Cells.Add(tc[i]);
}
Try this:
for (int i = 0; i < 10; i++)
{
tr[i] = new TableRow();
for (int x = 0; x < 10; x++)
{
tr[i].Cells.Add(tc[x]);
}
}
Your loops are not set up to give you a 10x10 table
Table table = new Table();
TableRow tr = null;
TableCell tc = null;
for (int i = 0; i < 10; i++)
{
tr = new TableRow();
for (int j = 0; j < 10; j++)
{
tc = new TableCell();
tc.Controls.Add(new TextBox());
tr.Cells.Add(tc);
}
table.Rows.Add(tr);
}
The cells has to be distinct: I need to create 100 cells not only 10!
TextBox[] tx = new TextBox[100];
TableCell[] tc = new TableCell[100];
TableRow[] tr = new TableRow[10];
for (int i = 0; i < 100; i++)
{
tx[i] = new TextBox();
tc[i] = new TableCell();
tc[i].Controls.Add(tx[i]);
}
int x = 0;
for (int i = 0; i < 10; i++)
{
tr[i] = new TableRow();
for (int j=0; j < 10; j++)
{
tr[i].Cells.Add(tc[x++]);
}
}
for (int i = 0; i < 10; i++)
Table1.Rows.Add(tr[i]);