r/programminghomework • • Sep 26 '16

Java StdDraw help.

Using the Princeton StdDraw library http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html I have to program a "Chalkboard" in which when I press the mouse, the mouse should draw on the chalkboard where I am pointing. I can't get my code to actually draw anything. Before this I tried having a "While (pressed)" statement that would "StdDraw.line(StdDraw.mouseX, StdDraw.mouseY(), StdDraw.mouseX(), StdDraw.mouseY());" however that didn't work.

What can I do to get my code to work? Why isn't my code working? I am used to scripting in Unity, and GameMaker, so this baffles me that my code doesn't function the way I intend it to.

Here is my code:

import edu.princeton.cs.introcs.StdDraw;

public class TaiChiSymbol {

public static void main(String[] args) {

	boolean pressed;
	pressed = StdDraw.mousePressed();


	double x1 = 0;
	double y1 = 0;
	double x2 = 0;
	double y2 = 0;
	int counter = 0;

	StdDraw.setXscale(0, 200);
	StdDraw.setYscale(0, 200);
	StdDraw.clear(StdDraw.BLACK);
	StdDraw.setPenColor(StdDraw.WHITE);


	while (true)
	{

	    if (pressed && counter == 0)
	    {
		x1 = StdDraw.mouseX();
		y1 = StdDraw.mouseY();
		counter ++;
	    }

	    else if (pressed && counter == 1)
	    {
		x2 = StdDraw.mouseX();
		y2 = StdDraw.mouseY();
		StdDraw.line(x1, y1, x2, y2);
		counter = 0;
	    }
	}
}

}

1 Upvotes

1 comment sorted by

View all comments

1

u/hislittlecuzin Sep 27 '16

I figured out the problem with my code. For future students curious, it has to do with the variable pressed, and an animation function including the function "show"