Bézier curve map x,y points to screen - java
hi recently i implemented the bezier curve it works fine but my problem is i dont know how to map x,y points to the screen because it gives me x,y in form of decimal points i will appreciate any help
this is my code which works i think .
import java.util.ArrayList;
public class Bezier {
public static void main(String[] args) {
ArrayList<Point> CP = new ArrayList<Point>();
CP.add(new Point(-5, 0));
CP.add(new Point(0, 5));
CP.add(new Point(5, 0));
CP.add(new Point(0,-5));
Bezier curve = new Bezier(3, 0, 0.01, CP);
curve.DefineBezierCurve();
ArrayList<Point>Results = curve.getCurvePoints();
for(int i = 0; i<Results.size() ; i++){
Point x = Results.get(i);
System.out.println(x.Y);
}
}
// class definitions
private ArrayList<Point> controlPoints;// the control points
private double t;//the value of t indicates the location of the point on the line sigment
private double step;// the increment value that the t increments
private int N;//the Beziar Curve Order
private ArrayList<Point>CurvePoints;// genrated (x,y) values of the curve
// constructor
public Bezier(int n , double t , double step ,ArrayList<Point> CP ){
this.N = n ;
this.t = t;
this.step = step;
this.controlPoints = CP;
CurvePoints = new ArrayList<Point>();
}
private int factorial(int x){
int result =1;
for(int i= x ; x>0 ; x--){
result*=x;
}
return result;
}
private int BinomialCoefficient(int i){
// we get the order form the global variable
int factN = factorial(this.N);
int factI = factorial(i);
int factN_I = factorial(this.N-i);
int theCoefficient = (factN/(factI*factN_I));
return theCoefficient ;
}
private Point BI_N_P(int i){
int coefficient = BinomialCoefficient(i);
Point CurrentControlPoint = this.controlPoints.get(i);
double X = coefficient* Math.pow(t,i)* Math.pow((1-t),(this.N-i))*CurrentControlPoint.X ;
double Y = coefficient* Math.pow(t,i)* Math.pow((1-t),(this.N-i))*CurrentControlPoint.Y ;
Point Tmp = new Point(X, Y);
return Tmp;
// this.CurvePoints.add(PointOnCrve);
}
private void DefineBezierCurve(){
while(t<=1){
Point PointOnCurve = new Point(0, 0);
for(int i = 0 ; i<=this.N ; i++){
Point tmp = BI_N_P(i);
PointOnCurve.X+=tmp.X;
PointOnCurve.Y+=tmp.Y;
}
this.CurvePoints.add(PointOnCurve);
this.t+=this.step;
}
}
public ArrayList<Point> getCurvePoints(){
return this.CurvePoints;
}
}
class Point{
public double X;
public double Y;
public Point(double x,double y){
X=x;
Y=y;
}
}
i used the genrated points to draw them in excel and this was my result
example of cubic bezier curve initial t=0,step = 0.01 , with control points
(-5, 0),(0, 5),(5, 0),(0,-5)
generated X points
-5.0
-4.85001
-4.700079999999999
-4.55027
-4.400639999999999
-4.25125
-4.102159999999999
-3.9534299999999996
-3.80512
-3.6572900000000006
-3.5100000000000007
-3.3633100000000002
-3.2172800000000006
-3.07197
-2.92744
-2.7837499999999995
-2.6409599999999993
-2.4991299999999996
-2.3583199999999995
-2.2185899999999994
-2.0799999999999996
-1.9426099999999988
-1.8064799999999992
-1.6716699999999989
-1.5382399999999987
-1.4062499999999998
-1.2757599999999993
-1.1468299999999993
-1.0195199999999995
-0.8938899999999995
-0.7699999999999991
-0.6479099999999991
-0.527679999999999
-0.4093699999999989
-0.29303999999999886
-0.17874999999999885
-0.06655999999999862
0.043470000000001674
0.15128000000000164
0.256810000000002
0.3600000000000019
0.4607900000000018
0.5591200000000022
0.6549300000000019
0.7481600000000019
0.838750000000002
0.9266400000000021
1.011770000000002
1.094080000000002
1.173510000000002
1.2500000000000018
1.3234900000000016
1.3939200000000018
1.4612300000000018
1.5253600000000018
1.5862500000000017
1.6438400000000017
1.6980700000000017
1.7488800000000018
1.7962100000000016
1.8400000000000019
1.8801900000000014
1.916720000000001
1.9495300000000007
1.9785600000000012
2.0037500000000006
2.0250400000000006
2.042370000000001
2.05568
2.0649100000000002
2.0700000000000003
2.07089
2.06752
2.0598299999999994
2.0477599999999994
2.031249999999999
2.010239999999999
1.984669999999999
1.9544799999999984
1.9196099999999983
1.8799999999999981
1.8355899999999976
1.7863199999999972
1.7321299999999973
1.672959999999997
1.6087499999999963
1.539439999999996
1.4649699999999957
1.3852799999999954
1.3003099999999952
1.2099999999999946
1.1142899999999942
1.0131199999999938
0.9064299999999934
0.7941599999999929
0.6762499999999925
0.552639999999992
0.4232699999999916
0.28807999999999107
0.14700999999999054
generated Y points
0.0
0.14701
0.28808
0.42327
0.55264
0.6762500000000001
0.79416
0.90643
1.0131200000000002
1.1142900000000002
1.21
1.3003099999999999
1.3852799999999996
1.4649699999999997
1.5394399999999997
1.6087499999999997
1.6729599999999996
1.7321299999999997
1.78632
1.83559
1.88
1.91961
1.95448
1.98467
2.0102400000000005
2.0312500000000004
2.0477600000000002
2.0598300000000003
2.0675200000000005
2.0708900000000003
2.0700000000000003
2.0649100000000002
2.0556800000000006
2.0423700000000005
2.0250400000000006
2.00375
1.9785599999999997
1.9495300000000002
1.91672
1.8801899999999998
1.8399999999999999
1.7962099999999999
1.748879999999999
1.6980699999999993
1.6438399999999993
1.5862499999999993
1.5253599999999992
1.461229999999999
1.3939199999999987
1.3234899999999983
1.2499999999999982
1.173509999999998
1.094079999999998
1.011769999999998
0.9266399999999977
0.8387499999999977
0.7481599999999973
0.6549299999999975
0.5591199999999971
0.46078999999999715
0.359999999999997
0.2568099999999969
0.15127999999999653
0.043469999999996345
-0.06656000000000395
-0.17875000000000396
-0.2930400000000042
-0.40937000000000445
-0.5276800000000048
-0.6479100000000048
-0.7700000000000049
-0.8938900000000052
-1.0195200000000053
-1.1468300000000053
-1.2757600000000053
-1.4062500000000062
-1.538240000000006
-1.6716700000000062
-1.8064800000000063
-1.9426100000000066
-2.0800000000000067
-2.218590000000007
-2.3583200000000066
-2.499130000000007
-2.6409600000000077
-2.783750000000008
-2.927440000000008
-3.0719700000000083
-3.217280000000008
-3.363310000000008
-3.5100000000000087
-3.6572900000000086
-3.805120000000009
-3.9534300000000084
-4.102160000000009
-4.251250000000009
-4.40064000000001
-4.550270000000009
-4.7000800000000105
-4.85001000000001
Multiply the set of points by some scale factor, then round to an integer. I would suggest the factor Screen Width/MAX(list of x points) for x, and Screen Height/MAX(list of y points) for y. This should give you a list of points scaled to the current size of the screen. Here's some python code that implements the idea.
X = "-5.0 -4.85001 -4.700079999999999 -4.55027 -4.400639999999999 -4.25125 -4.102159999999999 -3.9534299999999996 -3.80512 -3.6572900000000006 -3.5100000000000007 -3.3633100000000002 -3.2172800000000006 -3.07197 -2.92744 -2.7837499999999995 -2.6409599999999993 -2.4991299999999996 -2.3583199999999995 -2.2185899999999994 -2.0799999999999996 -1.9426099999999988 -1.8064799999999992 -1.6716699999999989 -1.5382399999999987 -1.4062499999999998 -1.2757599999999993 -1.1468299999999993 -1.0195199999999995 -0.8938899999999995 -0.7699999999999991 -0.6479099999999991 -0.527679999999999 -0.4093699999999989 -0.29303999999999886 -0.17874999999999885 -0.06655999999999862 0.043470000000001674 0.15128000000000164 0.256810000000002 0.3600000000000019 0.4607900000000018 0.5591200000000022 0.6549300000000019 0.7481600000000019 0.838750000000002 0.9266400000000021 1.011770000000002 1.094080000000002 1.173510000000002 1.2500000000000018 1.3234900000000016 1.3939200000000018 1.4612300000000018 1.5253600000000018 1.5862500000000017 1.6438400000000017 1.6980700000000017 1.7488800000000018 1.7962100000000016 1.8400000000000019 1.8801900000000014 1.916720000000001 1.9495300000000007 1.9785600000000012 2.0037500000000006 2.0250400000000006 2.042370000000001 2.05568 2.0649100000000002 2.0700000000000003 2.07089 2.06752 2.0598299999999994 2.0477599999999994 2.031249999999999 2.010239999999999 1.984669999999999 1.9544799999999984 1.9196099999999983 1.8799999999999981 1.8355899999999976 1.7863199999999972 1.7321299999999973 1.672959999999997 1.6087499999999963 1.539439999999996 1.4649699999999957 1.3852799999999954 1.3003099999999952 1.2099999999999946 1.1142899999999942 1.0131199999999938 0.9064299999999934 0.7941599999999929 0.6762499999999925 0.552639999999992 0.4232699999999916 0.28807999999999107 0.14700999999999054"
X = X.split(" ");
absX = list();
for x in X:
absX.append(abs(float(x)));
max_X = max(absX);
min_X = min(X);
screen_width = 1024;
scale_factor = screen_width/float(max_X + float(max(X)));
newX = list();
for x in X:
x = int(float(x)*scale_factor) + screen_width;
newX.append(x)
print(newX);
This returns the following list of X coordinates:
[300, 322, 344, 366, 387, 409, 430, 452, 473, 495, 516, 537, 559, 580, 601, 621, 642, 663, 683, 703, 723, 743, 763, 782, 802, 821, 840, 858, 877, 895, 913, 931, 948, 965, 982, 999, 1015, 1030, 1045, 1061, 1076, 1090, 1104, 1118, 1132, 1145, 1158, 1170, 1182, 1193, 1205, 1215, 1225, 1235, 1244, 1253, 1262, 1269, 1277, 1284, 1290, 1296, 1301, 1306, 1310, 1314, 1317, 1319, 1321, 1323, 1323, 1323, 1323, 1322, 1320, 1318, 1315, 1311, 1307, 1301, 1296, 1289, 1282, 1274, 1266, 1256, 1246, 1236, 1224, 1212, 1199, 1185, 1170, 1155, 1139, 1121, 1104, 1085, 1065, 1045]
And here's the Y:
[768, 784, 799, 814, 829, 843, 856, 868, 880, 891, 902, 912, 921, 930, 938, 946, 953, 960, 966, 971, 976, 981, 984, 988, 991, 993, 995, 996, 997, 997, 997, 997, 996, 994, 992, 990, 987, 984, 980, 976, 972, 967, 962, 956, 950, 944, 937, 930, 922, 914, 906, 898, 889, 880, 870, 861, 851, 840, 830, 819, 807, 796, 784, 772, 761, 749, 736, 723, 710, 697, 683, 669, 655, 641, 627, 612, 598, 583, 568, 553, 538, 522, 507, 491, 475, 460, 444, 428, 411, 395, 379, 363, 346, 330, 313, 297, 280, 264, 247, 230]
This script isn't perfect and doesn't return values within the correct range, but it should give you an idea as of where to start.
Related
How can I avoid keyPressed method to make my draw permenant in Processing 3?
I'm trying to create a HangMan game since it's an assignment. In the game() method, I want to call some methods from dead class if player's input is wrong. These methods creates HangMan. When I input a wrong letter and press Enter (increasing numWrong) related method is called but only flashes and disappears. I know it because of keyPressed method but how can I draw HangMan on screen and make it permanent till end of the game. String[] words = {"dictionary","game","player","result"}; char[] strChar, guessed, wrong; float x, y; String str, display, display2, typing="", guess="", win = "", lost = "", wrongAnswers; int rnd, c, numRight=0, winner=0, numWrong=0; void setup() { size(800, 800); surface.setLocation(960, 0); x = width; y = height; rnd = words.length-1; str = words[(int)random(rnd)].toUpperCase(); strChar = new char[str.length()]; winner = str.length(); guessed = new char[str.length()]; for (int c=0; c<str.length(); c++) { strChar[c] = str.charAt(c); guessed[c] = '_'; } wrong = new char[6]; for (int i=0; i<wrong.length; i++) { wrong[i] = '*'; } } void draw() { background(10); display = "Write a letter then press ENTER."; display2 = " "; wrongAnswers = "Incorrect Guesses: "; for (int d=0; d<guessed.length; d++) { display2 = display2 +" "+guessed[d]; } for (int i=0; i<wrong.length; i++) { wrongAnswers = wrongAnswers+" "+wrong[i]; } fill(255); textSize(40); text(display2, 40, 750); textSize(20); text(display, 450, 380); textSize(250); text(typing.toUpperCase(), 450, 245); strokeWeight(1); d.gallows(); fill(55, 200, 155); textSize(50); text(win, 110, 680); textSize(50); text(lost, 110, 680); textSize(20); text(wrongAnswers, 410, 690); //origins stroke(100, 150, 200); strokeWeight(2); line(0, 700, x, 700);//seperate line line(x/2, 0, x/2, 700);//vertical line line(x/2, y/2, x, y/2);//horizontal line fill(255); textSize(35); text(str, 90, 560); } void game(String guess) { guess = guess.toUpperCase(); char myGuess = guess.charAt(0); boolean guessedRight = false; for (int m=0; m<str.length(); m++) { if (myGuess==str.charAt(m)) { if (exist(display2, myGuess)) { guessed[m] = myGuess; numRight++; guessedRight = true; } if (numRight == winner) { win = "YOU WIN!!"; } } } if (guessedRight == false) { wrong[numWrong] = myGuess; numWrong++; noStroke(); fill(255); if (numWrong==1) { d.headAndRope(); } if (numWrong==2) { d.body(); d.headAndRope(); } if (numWrong==3) { d.leftArm(); d.body(); d.headAndRope(); } if (numWrong==4) { d.rightArm(); d.leftArm(); d.body(); d.headAndRope(); } if (numWrong==5) { d.leftLeg(); d.rightArm(); d.leftArm(); d.body(); d.headAndRope(); } if (numWrong==6) { d.rightLeg(); d.leftLeg(); d.rightArm(); d.leftArm(); d.body(); d.headAndRope(); } if (numWrong == wrong.length) { lost = "YOU LOSE!!"; } } } void keyPressed() { if (key == '\n') { game(typing); typing = ""; } else { typing+=key; } } boolean exist(String sng, char cha) { for (int i=0; i<sng.length(); i++) { if (sng.charAt(i)==cha) { return false; } } return true; } //DEAD CLASS class Dead { void gallows() { stroke(0); //base fill(127); rect(40, 600, 340, 30); //left vertical rect fill(200, 100, 50); rect(60, 50, 10, 550); //right vertical rect fill(100, 50, 25); rect(70, 50, 10, 550); //top horizontal rect fill(200, 100, 50); rect(60, 40, 300, 10); //bottom horizontal rect fill(100, 50, 25); rect(70, 50, 290, 10); //diagonal bottom rect fill(100, 50, 25); beginShape(); vertex(80, 250); vertex(80, 265); vertex(265, 60); vertex(250, 60); endShape(CLOSE); //diagonal top rect fill(200, 100, 50); beginShape(); vertex(70, 245); vertex(70, 260); vertex(260, 50); vertex(245, 50); endShape(CLOSE); } void headAndRope() { //rope fill(255); rect(300, 40, 2, 95); //head fill(255); ellipse(301, 155, 50, 75); } void body() { //body stroke(255); strokeWeight(3); line(301, 193, 301, 375); } void leftArm() { //left arm stroke(255); strokeWeight(2); line(301, 223, 251, 300); } void rightArm() { //right arm stroke(255); strokeWeight(2); line(301, 223, 351, 300); } void leftLeg() { //left leg stroke(255); strokeWeight(2); line(301, 375, 251, 450); } void rightLeg() { //right leg stroke(255); strokeWeight(2); line(301, 375, 351, 450); } }
In Processing the draw function gets called every frame, completely redrawing everything on the screen. So if you want the hangman to always appear on the screen, you'll need to draw it in the draw function (or in a function that gets called from draw). If you restructure your code slightly everything should work: void draw() { // draw UI and input prompt based on `numRight` value // NEW: draw the current state of the hangman based on `numWrong` value } void game(String guess) { // update the game state (`numRight` and `numWrong` variables) // based on `guess` input // NEW: since this is only called momentarily, don't do any drawing here } void keyPressed() { // receive key input and pass it to `game` } Since game only gets called momentarily when the user enters a guess, you don't want to be doing any drawing in there. It will get overwritten in the next draw cycle. You can still update the game state in game, but the drawing representation of that game state should happen from draw to ensure it gets drawn every time the screen updates.
Java - Trouble Passing Variables Between Main Method and Drawing Method
I'm creating a dots and boxes program and I am trying to feed the coordinate values from the main method where the user inputs them to the paintComponent method in the JFrame class. However I have to throw in the (Graphics g) parameter, and I don't see a way around it to feed in the values. It's probably big cringe because I'm still starting out but any help would be great. import javax.swing.*; import java.awt.*; import java.util.Scanner; public class Main { public static void main(String[] args){ JFrame f = new JFrame("Dots & Boxes"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Drawing a = new Drawing(); f.add(a); f.setSize(1440,990); f.setVisible(true); Scanner input = new Scanner(System.in); System.out.println("You will choose two coordinates on the dot grid to place a line between."); System.out.println("Make sure that they are right next to each other, either vertically or horizontally (not diagonal)"); int xOne; int yOne; int xTwo; int yTwo; boolean playerOneTurn = true; for (int i = 0; i <= 760; i++){ System.out.println("Pick Your First X-Coordinate: "); xOne = input.nextInt(); System.out.println("Pick Your First Y-Coordinate: "); yOne = input.nextInt(); System.out.println("Pick Your Second X-Coordinate: "); xTwo = input.nextInt(); System.out.println("Pick Your Second Y-Coordinate: "); yTwo = input.nextInt(); playerOneTurn = !playerOneTurn; } } } class Drawing extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); this.setBackground(Color.WHITE); setFont(new Font("TimesRoman", Font.PLAIN, 20)); g.drawString("0", 75, 45); g.drawString("1", 110, 45); g.drawString("2", 145, 45); g.drawString("3", 180, 45); g.drawString("4", 215, 45); g.drawString("5", 250, 45); g.drawString("6", 285, 45); g.drawString("7", 320, 45); g.drawString("8", 355, 45); g.drawString("9", 390, 45); g.drawString("10", 417, 45); g.drawString("11", 452, 45); g.drawString("12", 487, 45); g.drawString("13", 522, 45); g.drawString("14", 557, 45); g.drawString("15", 592, 45); g.drawString("16", 627, 45); g.drawString("17", 662, 45); g.drawString("18", 697, 45); g.drawString("19", 732, 45); g.drawString("0", 40, 75); g.drawString("1", 40, 110); g.drawString("2", 40, 145); g.drawString("3", 40, 180); g.drawString("4", 40, 215); g.drawString("5", 40, 250); g.drawString("6", 40, 285); g.drawString("7", 40, 320); g.drawString("8", 40, 355); g.drawString("9", 40, 390); g.drawString("10", 35, 425); g.drawString("11", 35, 460); g.drawString("12", 35, 495); g.drawString("13", 35, 530); g.drawString("14", 35, 565); g.drawString("15", 35, 600); g.drawString("16", 35, 635); g.drawString("17", 35, 670); g.drawString("18", 35, 705); g.drawString("19", 35, 740); int dotx1 = 80; int doty1 = 70; ((Graphics2D) g).setStroke(new BasicStroke(5)); for (int h = 0; h <= 19; h++) { for (int i = 0; i <= 19; i++) { g.drawLine(dotx1, doty1, dotx1, doty1); dotx1 = dotx1 + 35; } dotx1 = 80; doty1 = doty1 + 35; } } }
You shouldn't call paintComponent by yourself. The method is called when Java is drawing the window. The reason is that the user shouldn't care about re-drawing, most of the time. Java will decide when to redraw, for example on window minimize/maximize or resizing or when an element is clicked. To draw animated shapes you must ask Java to redraw your window. You can add f.revalidate() to force components to redraw. Take a look at the documentation here. Be really careful when using this. Never call this inside a loop without waiting between frames, because your CPU will go crazy if not ! If you want animated shapes, call revalidate() at fixed rate, for example 60fps. In your case, you could simple put your Scanner inside the loop, ask the user for input, process it and then redraw the window. Also, revalidate() is not blocking, it will just tell Java that the components tree has changed and must be redraw. But it will do it when he will have the time to do it. Edit : As we are discussing in commentary, to feed your Drawing with the data to draw you must give him the informations. First, create your graphical elements, let start with points : class Point { public float x, y; Point(float x, float y) { this.x = x; this.y = y; } } In your Drawing (who is in fact your Scene, let rename it), add setters and List class Scene { private List<Point> mPoints = new LinkedList<Point>(); public addPoint(Point p) { mPoints.add(p); } public void paintComponent(Graphics g) { super.paintComponent(g); .... for (Point p : mPoints) { // draw your points here } .... } } There several way to improve this simple example. First, you could add a paint(Graphics g) method on Point class. When you will add another kind of component, you should create a base class Item and extend it in your graphical items (Point, Box...) and put the paint method in this new base class. It's the way to don't have one list for each kind of element, and then you can just iterate over items and call paint without worrying about the kind behind it (Point, Box...)
Processing 3.x not recognizing random() function
I'm trying to create a basic "tree" shape in processing. I have an initial constructor that takes my arguments and draws them in fixed locations on the background, but I also have a secondary constructor that is supposed to assign random values that I specify so that each time it is drawn the trees are in different locations. However, I am having an issue with Processing where it says The function random(int) does not exist and I can't seem to find a solution to the issue. I realize this is a naive approach to graphics, but I am just trying to get my feet wet with Processing. My Tree.java class: import processing.core.PApplet; import java.util.Random; public class Tree{ // Instance variables private int centerX, centerY; private float scale; private int trunkR, trunkG, trunkB, leavesR, leavesG, leavesB; private static PApplet sketch; public Tree(int theCenterX, int theCenterY, float theScale, int theTrunkR, int theTrunkG, int theTrunkB, int theLeavesR, int theLeavesG, int theLeavesB) { centerX = theCenterX; centerY = theCenterY; scale = theScale; trunkR = theTrunkR; trunkG = theTrunkG; trunkB = theTrunkB; leavesR = theLeavesR; leavesG = theLeavesG; leavesB = theLeavesB; } public Tree(){ centerX = random(960.0); centerY = random(700.0); scale = random(2.0); trunkR = random(255.0); trunkG = random(255.0); trunkB = random(255.0); leavesR = random(255.0); leavesG = random(255.0); leavesB = random(255.0); } public void draw(){ sketch.noStroke(); sketch.fill(trunkR, trunkG, trunkB); sketch.rect(centerX, centerY, 80*scale, 300*scale); sketch.fill(leavesR, leavesG, leavesB); sketch.triangle(centerX - 40*scale, centerY + 40*scale, centerX + 40*scale, centerY - 80*scale, centerX + 120*scale, centerY + 40*scale); } public static void setup(PApplet theSketch){ sketch = theSketch; } } And here is my main class that calls the tree class to create the objects: Tree tree, tree2, tree3, tree4, randomTree; void settings(){ size(1000, 1000); } void setup(){ setupGraphicClasses(); tree = new Tree(width/2 - 400, height/2 - 100, 1.0, 67, 12, 12, 27, 129, 28); tree2 = new Tree(width/2 + 200, height/2 + 150, 1.5, 67, 12, 12, 27, 129, 28); tree3 = new Tree(width/2, height/2 - 80, 0.5, 67, 12, 12, 27, 129, 28); tree4 = new Tree(width/2 + 320, height/2 - 170, 0.9, 67, 12, 12, 27, 129, 28); randomTree = new Tree(); } void draw() { background(127); noStroke(); fill(16, 85, 17); rect(0, 500, 1000, 500); fill(70, 195, 255); rect(0, 0, 1000, 500); tree.draw(); tree4.draw(); tree2.draw(); tree3.draw(); randomTree.draw(); } public void setupGraphicClasses() { Tree.setup(this); } Why would I be getting this error? I have tried casting the instance variables as float since those are the parameters the random() function accepts as parameters, but then I get a different error message.
You're calling the random() function from your Tree class, not your sketch class. That won't work, because only the sketch class knows about the random() function. One approach to fix this is to pass an instance of your sketch into the Tree class, then use that to get to the random function. Something like this: void setup(){ Tree tree = new Tree(this); } class Tree{ public Tree(PApplet sketch){ float x = sketch.random(100); } } If all you need is the random() function, this might be overkill though. You could just use the Math.random() function instead. Of course, this locks you into deploying as Java, which might be overly restrictive.
Parallel sieve of Eratosthenes - Java Multithreading
I wanted to write sieve of Eratosthenes which will work using specific number of threads. I figured out, that it will work in following way: For 2 threads up to 17. Thread-1 takes 2, and starts to remove multiple of 2 from List. Parallel Thread-2 takes 3 and does the same. After that Thread-1 takes 5( because there is no 4 in List) and Thread-2 takes 7 and so on until they reach end. I wrote following piece of code: private List<Integer> array = new ArrayList<Integer>(); private List<Integer> results = new ArrayList<Integer>(); public synchronized void run(){ while(array.size() > 0){ Integer tmp = array.get(0); for(int i = 1; i < array.size(); i++){ if( (array.get(i).intValue() % tmp.intValue()) == 0) array.remove(i); } results.add(array.get(0)); array.remove(0); } } public void setArray(int x){ for(int i = 2; i < x; i++) array.add(Integer.valueOf(i)); } public void printArray(){ for(Integer i: results){ System.out.println(i); } } This code works, but I added time measurement "tool" to my main class: ThreadTask task = new ThreadTask(); task.setArray(5000); Long beg = new Date().getTime(); for(int i = 0; i < 3;i++){ new Thread(task).start(); } Long sleep = 1000L; Thread.sleep(sleep);// I am sleeping main thread to wait until other Threads are done task.printArray(); System.out.println("Time is "+(new Date().getTime()-beg-sleep)); The problem is that running this with 2 threads is slower than running with 1 thread, and 3 threads are slower than 2 threads. Could anyone explain me, why? EDIT: There is one important thing about that. I don't need it to be done as fast as it can be. I need it working on Threads for one reason. My Teacher wants to compare runtimes of running same program with 1, 2 .. n threads. Results should look like in this graph. EDIT2: I have rewritten code to following private HashMap<Integer,Boolean> array = new HashMap<Integer,Boolean>(); private int counter = 1; private int x; public void run(){ while(counter < x-1){ do{ counter++; } while( array.get(counter)); int tmp = counter; for(int i = tmp; i < array.size(); i+=tmp){ if( i!= tmp) array.put(i,true); } try{ Thread.sleep(0L, 1); } catch (Exception e){} } } public void setArray(int x){ this.x = x; for(int i = 2; i < x; i++) array.put(i, false); } public void printArray(){ for(int i = 2; i < array.size();i++){ if( !array.get(i)) System.out.println(i); } } Now it uses HashMap and this is how it works: Fill HashMap with keys from 2 to n and false values. New thread goes into while loop which is based on counter variable. Counter represents current key. Increment counter on the begging so new threads doesn't operate on counter of earlier started thread. Put counter value into temporary variable tmp so we can work even when another thread increment counter Iterate through the HashMap by incrementing i with tmp ( it is actually jumping on the multiplies of i) and set their values to true. All keys which has true value are ignored in print method. Also counter skips them when incremented. The problem is that it still works slower with more threads. What's wrong now?
The mistake is simpler than I first thought. All your threads are doing the same thing so each thread does more work. To make a multi-threaded program work faster you have to divide up the work, which has to be performed concurrently. When you have one thread accessing a data structure, it can be in the fastest cache of one core, use multiple threads and they need to co-ordinate their actions and since most of the work is updating the data structure, a lot of time is lost as overhead. This is the case even though your data structure is not thread safe and is likely to have a corrupted result. BTW updating an ArrayList is very expensive and using a collection objects is also an overhead. You will get a much faster result using a BitSet and just one thread. public class BitSetSieveMain { private final BitSet set; private final int size; public BitSetSieveMain(int x) { size = x + 1; set = new BitSet(size); set.flip(2, size); } public static void main(String[] args) { for (int i = 0; i < 10; i++) { long start = System.nanoTime(); BitSetSieveMain bitSetSieveMain = new BitSetSieveMain(5000); bitSetSieveMain.sieve(); long time = System.nanoTime() - start; System.out.println(time / 1000 + " micro-seconds to perform " + bitSetSieveMain); } } public void sieve() { int i = 2; do { for (int j = i*2; j < size; j += i) set.clear(j); i = set.nextSetBit(i+1); } while (i > 0); } public String toString() { return set.toString(); } } finally prints 87 micro-seconds to perform {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999}
Trouble with creating a string array of int arrays
I need to create an array of integers contained within an array of words. I keep getting an error cannot convert from int[] to java.lang.String[] where it says private String[][] expenseName = {clothes, tuition, transportation, food, housing, books}; This is my code public class Budget{ ///////////////fields//////////////// int expenseAmount[] = {1000, 2000, 3000, 4000, 5000, 6000}; int monthNumber[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; int clothes[]= {100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210}; int tuition[] = {200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200}; int transportation[]={100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210}; int food[]={80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80}; int housing[]={150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150}; int books[]= {200, 0, 0, 0, 0, 0, 0, 300, 0, 0, 0, 0}; int i=0; // this is arbitrary. Never hard code numbers unless that number is never going to change. in that case you make a variable and define it. int month_num; private String[][] expenseName = {clothes, tuition, transportation, food, housing, books}; /////////constructors/////////////// public Budget() {} public Budget(int name) {this.expenseName[i][i] = name;} public Budget(String name, int clothes, int tuition, int transportation, int food, int housing, int books) { this.expenseName[i] = name; this.clothes[i] = clothes; this.tuition[i] = tuition; this.transportation[i] = transportation; this.food[i] = food; this.housing[i] = housing; this.books[i] = books; this.monthNumber[i] = month_num; } /////////////methods/////////// public String getexpenseName() {return expenseName[i][i];} public int getclothes() {return clothes[i];}//change the I public int gettuition() {return tuition[i];} public int gettransporation() {return transportation[i];} public int getfood() {return food[i];} public int gethousing() {return housing[i];} public int books() {return books[i];} public int getmonthNumber() {return monthNumber[i];}//change the i public void setExpenseName(String name) { this.expenseName[i][i] = name; } public boolean setexpenseAmount(int num) { if (this.expenseAmount[i] == 0) {this.expenseAmount[i] = num; return true; } else return false;
You need quotes around your strings: private String[] expenseName = {"clothes", "tuition", "etc"}; or you need to declare it int[][] private int[][] expenseName = {clothes, tuition};
As the error message clearly states, you can't put an int[] into an array of String[]s.
Change private String[][] expenseName = {clothes, tuition, transportation, food, housing, books}; to private int[][] expenseName = {clothes, tuition, transportation, food, housing, books};
In Java, you cannot use an array of int as an array of String. That will just not work, like the error says. I guess that you must be familiar with some language where there is no type checking and you can do whatever you want. But this is Java.
You have to convert the int[] to String[], for example like this. public class ArrayIntToString { public static void main(String[] args) { int[] numbers = { 1, 2, 3 }; String[] numbersAsStrings = new String[numbers.length]; int i = 0; for (Integer number : numbers) { numbersAsStrings[i++] = number.toString(); } } }