so the final outcome of my quilt program would look something like this: so far
ID: 3839234 • Letter: S
Question
so the final outcome of my quilt program would look something like this:
so far my quilt class code looks like this:
public class Quilt extends Applet
{
//instance variables
private static final int ROWS = 4;
private static final int COLS = 6;
private static final int PATCH = 70; //patch size
/**
* Creates a new Quilt object
*/
public Quilt(){
}
/**
* Repaints the applet. This is your "main" method for drawing.
* @param g the Graphics context on which the applet is painted
*/
public void paint(Graphics g)
{
g.setColor(Color.WHITE); // background color
g.fillRect(0,0,600,500);
drawGrid(g); //draws the grid of patches
}
/**
* Draws an grid on the given Graphics object
* @param g the Graphics object to draw on.
*/
public void drawGrid(Graphics g)
{
g.setColor(Color.BLACK); //change the color to black
for(int i=0; i<=ROWS; i++) //draw the rows
{
g.drawLine(0, i*PATCH, PATCH*(COLS+1), i*PATCH);
}
for(int i=0; i<=COLS; i++) //draw the columns
{
g.drawLine(i*PATCH, 0, i*PATCH, PATCH*(ROWS+1));
}
}
}
}
im having trouble with the alternations, theres directions but i dont understand it and how i should write in my code. here are the directions given...
The Patch Grid
- If you look at the quilt as a whole, one of the first things to notice is that the different block types are arranged in the quilt so that they form a regular pattern. Each successive row in the quilt has the same blocks in the same cyclic order. You can produce a cycle by using the modulo operator. Notice how
That is, if you keep modding by the number of options, you can simply add one to a count in order to form a cycle!
- In addition, each "patch" will need to be able to be drawn anywhere in the quilt. This means that you'll need to give arguments to the drawing methods that are relative to some position on the screen--generally the upper left corner of the patch. This acts as the "origin" (kind of like (0,0)) for the patch. Notice that the origin for the first patch in the first row is at (0,0), the second is at (PATCH_SIZE,0) the third is at (2*PATCH_SIZE,0)... etc. (do you see a pattern?).
- By making the origin a variable and drawing relative to that origin, we can easily redraw the panel just by changing the origin. So instead of drawing a dot at (50,50), you'd draw a dot at (rx+50, ry+50), where (rx,ry) is the origin for the patch. For this reason, all of the patch drawing methods will take an rx (relative x) and ry (relative y) as parameters--you can experiment with seeing your pattern drawn multiple times in different spots by simply calling the method repeatedly.
- In short, inside your paint() method you'll want to use some kind of loop to cycle through all the patches, and then for each patch you can use the modulo operator to choose which pattern to draw, and then call the appropriate method with the correct (rx,ry) parameters to actually draw the pattern.
Java Applet started. Java Applet Viewer: Quilt.class Java Java Java Java JavaExplanation / Answer
class ProgramQuilt
{
private static final int MAXDIM = 2;
Mapdesc mapdesc;
public CArrayOfQuiltspecs qspec;
public CArrayOfQuiltspecs eqspec;
public CArrayOfFloats cpts;
public Quilt next;
public Quilt(Mapdesc mapdesc)
{
this.mapdesc = mapdesc;
Quiltspec[] tmpquilts = new Quiltspec[MAXDIM];
for (int i = 0; i < tmpquilts.length; i++)
tmpquilts[i] = new Quiltspec();
this.qspec = new CArrayOfQuiltspecs(tmpquilts);
}
public void toBezier(Knotvector sknotvector, Knotvector tknotvector,
CArrayOfFloats ctrlarr, int coords)
{
Splinespec spline = new Splinespec(2);
spline.kspecinit(sknotvector, tknotvector);
spline.select();
spline.layout(coords);
spline.setupquilt(this);
spline.copy(ctrlarr);
spline.transform();
}
public void toBezier(Knotvector knots, CArrayOfFloats ctlarray, int ncoords)
{
Splinespec spline = new Splinespec(1);
spline.kspecinit(knots);
spline.select();
spline.layout(ncoords);
spline.setupquilt(this);
spline.copy(ctlarray);
spline.transform();
}
public void downloadAll(float[] pta, float[] ptb, Backend backend)
{
for (Quilt m = this; m != null; m = m.next)
{
m.select(pta, ptb);
m.download(backend);
}
}
private void download(Backend backend)
{
if (getDimension() == 2)
{
CArrayOfFloats ps = new CArrayOfFloats(cpts);
ps.raisePointerBy(qspec.get(0).offset);
ps.raisePointerBy(qspec.get(1).offset);
ps.raisePointerBy(qspec.get(0).index * qspec.get(0).order
* qspec.get(0).stride);
ps.raisePointerBy(qspec.get(1).index * qspec.get(1).order
* qspec.get(1).stride);
backend.surfpts(mapdesc.getType(), ps, qspec.get(0).stride, qspec
.get(1).stride, qspec.get(0).order, qspec.get(1).order,
qspec.get(0).breakpoints[qspec.get(0).index],
qspec.get(0).breakpoints[qspec.get(0).index + 1], qspec
.get(1).breakpoints[qspec.get(1).index], qspec
.get(1).breakpoints[qspec.get(1).index + 1]);
}
else
{
CArrayOfFloats ps = new CArrayOfFloats(cpts.getArray(), 0);
ps.raisePointerBy(qspec.get(0).offset);
ps.raisePointerBy(qspec.get(0).index * qspec.get(0).order
* qspec.get(0).stride);
backend.curvpts(mapdesc.getType(), ps, qspec.get(0).stride, qspec .get(0).order,
qspec.get(0).breakpoints[qspec.get(0).index],
qspec.get(0).breakpoints[qspec.get(0).index + 1]);
}
}
private int getDimension()
{
return eqspec.getPointer() - qspec.getPointer();
}
private void select(float[] pta, float[] ptb)
{
// DONE
int dim = eqspec.getPointer() - qspec.getPointer();
int i, j;
for (i = 0; i < dim; i++)
{
for (j = qspec.get(i).width - 1; j >= 0; j--)
if (qspec.get(i).breakpoints[j] <= pta[i]
&& ptb[i] <= qspec.get(i).breakpoints[j + 1])
break;
assert (j != -1);
qspec.get(i).index = j;
}
}
public void getRange(float[] from, float[] to, Flist bpts)
{
getRange(from, to, 0, bpts);
}
private void getRange(float[] from, float[] to, int i, Flist list)
{
Quilt maps = this;
from[i] = maps.qspec.get(i).breakpoints[0];
to[i] = maps.qspec.get(i).breakpoints[maps.qspec.get(i).width];
int maxpts = 0;
Quilt m;
for (m = maps; m != null; m = m.next) {
if (m.qspec.get(i).breakpoints[0] > from[i])
from[i] = m.qspec.get(i).breakpoints[0];
if (m.qspec.get(i).breakpoints[m.qspec.get(i).width] < to[i])
to[i] = m.qspec.get(i).breakpoints[m.qspec.get(i).width];
maxpts += m.qspec.get(i).width + 1;
}
list.grow(maxpts);
for (m = maps; m != null; m = m.next)
{
for (int j = 0; j <= m.qspec.get(i).width; j++)
{
list.add(m.qspec.get(i).breakpoints[j]);
}
}
list.filter();
list.taper(from[i], to[i]);
}
public int isCulled()
{
if (mapdesc.isCulling())
{
System.out.println("TODO quilt.isculled mapdesc.isculling");
return 0;
}
else
{
return Subdivider.CULL_ACCEPT;
}
}
public void getRange(float[] from, float[] to, Flist slist, Flist tlist)
getRange(from, to, 0, slist);
getRange(from, to, 1, tlist);
}
public void findRates(Flist sbrkpts, Flist tbrkpts, float[] rate)
{
System.out.println("TODO quilt.findrates");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.