Question

In: Computer Science

Hi, Java Question and thanks in advance. // TODO once BoundingBox and Draw are implemented, change...

Hi, Java Question and thanks in advance.

// TODO once BoundingBox and Draw are implemented, change Fixtures.simpleCircle
// to Fixtures.complexGroup and test the app on an emulator or Android device


@Override
@SuppressLint("DrawAllocation")
protected void onDraw(final Canvas canvas) {
   final Shape shape = Fixtures.simpleCircle;
   final Location b = shape.accept(new BoundingBox());
   canvas.translate(-b.getX(), -b.getY());
   b.accept(new Draw(canvas, paint));
   shape.accept(new Draw(canvas, paint));
   canvas.translate(b.getX(), b.getY());
}
public class BoundingBox implements Visitor {

   // TODO entirely your job (except onCircle)

   @Override
   public Location onCircle(final Circle c) {
      final int radius = c.getRadius();
      return new Location(-radius, -radius, new Rectangle(2 * radius, 2 * radius));
   }

   @Override
   public Location onFill(final Fill f) {
      return null;
   }

   @Override
   public Location onGroup(final Group g) {

      return null;
   }

   @Override
   public Location onLocation(final Location l) {

      return null;
   }

   @Override
   public Location onRectangle(final Rectangle r) {
      return null;
   }

   @Override
   public Location onStrokeColor(final StrokeColor c) {
      return null;
   }

   @Override
   public Location onOutline(final Outline o) {
      return null;
   }

   @Override
   public Location onPolygon(final Polygon s) {
      return null;
   }
}
mport android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import edu.luc.etl.cs313.android.shapes.model.*;

/**
 * A Visitor for drawing a shape to an Android canvas.
 */
public class Draw implements Visitor {

   // TODO entirely your job (except onCircle)

   private final Canvas canvas;

   private final Paint paint;

   public Draw(final Canvas canvas, final Paint paint) {
      this.canvas = null; // FIXME
      this.paint = null; // FIXME
      paint.setStyle(Style.STROKE);
   }

   @Override
   public Void onCircle(final Circle c) {
      canvas.drawCircle(0, 0, c.getRadius(), paint);
      return null;
   }

   @Override
   public Void onStrokeColor(final StrokeColor c) {

      return null;
   }

   @Override
   public Void onFill(final Fill f) {

      return null;
   }

   @Override
   public Void onGroup(final Group g) {

      return null;
   }

   @Override
   public Void onLocation(final Location l) {

      return null;
   }

   @Override
   public Void onRectangle(final Rectangle r) {

      return null;
   }

   @Override
   public Void onOutline(Outline o) {

      return null;
   }

   @Override
   public Void onPolygon(final Polygon s) {

      final float[] pts = null;

      canvas.drawLines(pts, paint);
      return null;
   }
}
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import edu.luc.etl.cs313.android.shapes.model.*;
import java.util.*;

/**
 * A Visitor for drawing a shape to an Android canvas.
 */
public class Draw implements Visitor<Void> {

   // TODO entirely your job (except onCircle)

   private final Canvas canvas;

   private final Paint paint;

   public Draw(final Canvas canvas, final Paint paint) {
      this.canvas = canvas; // FIXED
      this.paint = paint; // FIXED
      paint.setStyle(Style.STROKE);
   }

   @Override
   public Void onCircle(final Circle c) {
      canvas.drawCircle(0, 0, c.getRadius(), paint);
      return null;
   }

   @Override
   public Void onStrokeColor(final StrokeColor c) {
      final int x = paint.getColor();
      paint.setColor(c.getColor());
      c.getShape().accept(this);
      paint.setColor(x);
      return null;
   }

   @Override
   public Void onFill(final Fill f) {
      final Style a = paint.getStyle();
      paint.setStyle(Style.FILL_AND_STROKE);
      f.getShape().accept(this);
      paint.setStyle(a);
      return null;
   }

   @Override
   public Void onGroup(final Group g) {
      final Iterator<? extends Shape > shape = g.getShapes().iterator();
      while (shape.hasNext()) {
         shape.next().accept(this);
      }
      return null;
   }

   @Override
   public Void onLocation(final Location l) {
      canvas.translate(l.getX(), l.getY());
      l.getShape().accept(this);
      canvas.translate(-l.getX(), -l.getY());
      return null;
   }

   @Override
   public Void onRectangle(final Rectangle r) {
      canvas.drawRect(0, 0, r.getWidth(), r.getHeight(), paint);
      return null;
   }

   @Override
   public Void onOutline(Outline o) {
      final Style before = paint.getStyle();
      paint.setStyle(Style.STROKE);
      o.getShape().accept(this);
      paint.setStyle(before);
      return null;
   }

   @Override
   public Void onPolygon(final Polygon s) {
      List<? extends Point> points = s.getPoints();
      final float[] pts =
            {
                  points.get(0).getX(), points.get(0).getY(),
                  points.get(1).getX(), points.get(1).getY(),
                  points.get(1).getX(), points.get(1).getY(),
                  points.get(2).getX(), points.get(2).getY(),
                  points.get(2).getX(), points.get(2).getY(),
                  points.get(3).getX(), points.get(3).getY(),
                  points.get(0).getX(), points.get(0).getY(),
            };
      canvas.drawLines(pts, paint);
      return null;
   }
}
* A shape visitor for calculating the bounding box, that is, the smallest
 * rectangle containing the shape. The resulting bounding box is returned as a
 * rectangle at a specific location.
 */
public class BoundingBox implements Visitor<Location> {

   // TODO entirely your job (except onCircle)

   @Override
   public Location onCircle(final Circle c) {
      final int radius = c.getRadius();
      return new Location(-radius, -radius, new Rectangle(2 * radius, 2 * radius));
   }

   @Override
   public Location onFill(final Fill f) {
      return null;
   }

   @Override
   public Location onGroup(final Group g) {

      return null;
   }

   @Override
   public Location onLocation(final Location l) {

      return null;
   }

   @Override
   public Location onRectangle(final Rectangle r) {
      return null;
   }

   @Override
   public Location onStrokeColor(final StrokeColor c) {
      return null;
   }

   @Override
   public Location onOutline(final Outline o) {
      return null;
   }

   @Override
   public Location onPolygon(final Polygon s) {
      return null;
   }
}

Solutions

Expert Solution


@Override
@SuppressLint("DrawAllocation")
protected void onDraw(final Canvas canvas) {
   final Shape shape = Fixtures.simpleCircle;
   final Location b = shape.accept(new BoundingBox());
   canvas.translate(-b.getX(), -b.getY());
   b.accept(new Draw(canvas, paint));
   shape.accept(new Draw(canvas, paint));
   canvas.translate(b.getX(), b.getY());
}

public class BoundingBox implements Visitor {

   // TODO entirely your job (except onCircle)

   @Override
   public Location onCircle(final Circle c) {
      final int radius = c.getRadius();
      return new Location(-radius, -radius, new Rectangle(2 * radius, 2 * radius));
   }

   @Override
   public Location onFill(final Fill f) {
      return null;
   }

   @Override
   public Location onGroup(final Group g) {

      return null;
   }

   @Override
   public Location onLocation(final Location l) {

      return null;
   }

   @Override
   public Location onRectangle(final Rectangle r) {
      return null;
   }

   @Override
   public Location onStrokeColor(final StrokeColor c) {
      return null;
   }

   @Override
   public Location onOutline(final Outline o) {
      return null;
   }

   @Override
   public Location onPolygon(final Polygon s) {
      return null;
   }
}

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import edu.luc.etl.cs313.android.shapes.model.*;

/**
 * A Visitor for drawing a shape to an Android canvas.
 */
public class Draw implements Visitor {

   // TODO entirely your job (except onCircle)

   private final Canvas canvas;

   private final Paint paint;

   public Draw(final Canvas canvas, final Paint paint) {
      this.canvas = null; // FIXME
      this.paint = null; // FIXME
      paint.setStyle(Style.STROKE);
   }

   @Override
   public Void onCircle(final Circle c) {
      canvas.drawCircle(0, 0, c.getRadius(), paint);
      return null;
   }

   @Override
   public Void onStrokeColor(final StrokeColor c) {

      return null;
   }

   @Override
   public Void onFill(final Fill f) {

      return null;
   }

   @Override
   public Void onGroup(final Group g) {

      return null;
   }

   @Override
   public Void onLocation(final Location l) {

      return null;
   }

   @Override
   public Void onRectangle(final Rectangle r) {

      return null;
   }

   @Override
   public Void onOutline(Outline o) {

      return null;
   }

   @Override
   public Void onPolygon(final Polygon s) {

      final float[] pts = null;

      canvas.drawLines(pts, paint);
      return null;
   }
}

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import edu.luc.etl.cs313.android.shapes.model.*;
import java.util.*;

/**
 * A Visitor for drawing a shape to an Android canvas.
 */
public class Draw implements Visitor<Void> {

   // TODO entirely your job (except onCircle)

   private final Canvas canvas;

   private final Paint paint;

   public Draw(final Canvas canvas, final Paint paint) {
      this.canvas = canvas; // FIXED
      this.paint = paint; // FIXED
      paint.setStyle(Style.STROKE);
   }

   @Override
   public Void onCircle(final Circle c) {
      canvas.drawCircle(0, 0, c.getRadius(), paint);
      return null;
   }

   @Override
   public Void onStrokeColor(final StrokeColor c) {
      final int x = paint.getColor();
      paint.setColor(c.getColor());
      c.getShape().accept(this);
      paint.setColor(x);
      return null;
   }

   @Override
   public Void onFill(final Fill f) {
      final Style a = paint.getStyle();
      paint.setStyle(Style.FILL_AND_STROKE);
      f.getShape().accept(this);
      paint.setStyle(a);
      return null;
   }

   @Override
   public Void onGroup(final Group g) {
      final Iterator<? extends Shape > shape = g.getShapes().iterator();
      while (shape.hasNext()) {
         shape.next().accept(this);
      }
      return null;
   }

   @Override
   public Void onLocation(final Location l) {
      canvas.translate(l.getX(), l.getY());
      l.getShape().accept(this);
      canvas.translate(-l.getX(), -l.getY());
      return null;
   }

   @Override
   public Void onRectangle(final Rectangle r) {
      canvas.drawRect(0, 0, r.getWidth(), r.getHeight(), paint);
      return null;
   }

   @Override
   public Void onOutline(Outline o) {
      final Style before = paint.getStyle();
      paint.setStyle(Style.STROKE);
      o.getShape().accept(this);
      paint.setStyle(before);
      return null;
   }

   @Override
   public Void onPolygon(final Polygon s) {
      List<? extends Point> points = s.getPoints();
      final float[] pts =
            {
                  points.get(0).getX(), points.get(0).getY(),
                  points.get(1).getX(), points.get(1).getY(),
                  points.get(1).getX(), points.get(1).getY(),
                  points.get(2).getX(), points.get(2).getY(),
                  points.get(2).getX(), points.get(2).getY(),
                  points.get(3).getX(), points.get(3).getY(),
                  points.get(0).getX(), points.get(0).getY(),
            };
      canvas.drawLines(pts, paint);
      return null;
   }
}

* A shape visitor for calculating the bounding box, that is, the smallest
 * rectangle containing the shape. The resulting bounding box is returned as a
 * rectangle at a specific location.
 */
public class BoundingBox implements Visitor<Location> {

   // TODO entirely your job (except onCircle)

   @Override
   public Location onCircle(final Circle c) {
      final int radius = c.getRadius();
      return new Location(-radius, -radius, new Rectangle(2 * radius, 2 * radius));
   }

   @Override
   public Location onFill(final Fill f) {
      return null;
   }

   @Override
   public Location onGroup(final Group g) {

      return null;
   }

   @Override
   public Location onLocation(final Location l) {

      return null;
   }

   @Override
   public Location onRectangle(final Rectangle r) {
      return null;
   }

   @Override
   public Location onStrokeColor(final StrokeColor c) {
      return null;
   }

   @Override
   public Location onOutline(final Outline o) {
      return null;
   }

   @Override
   public Location onPolygon(final Polygon s) {
      return null;
   }
}

hi you did a minor mistake while import graphic canvas you missed letter " i " of import and you have not send the complete code.


Related Solutions

Hi there, Thanks for your quick response in Advance . Best, - Operating systems typically split...
Hi there, Thanks for your quick response in Advance . Best, - Operating systems typically split functionality into layers. - Explain what it means and how it works - Explain the advantages and disadvantages of the structure of software as layers in distributed systems. Thanks a Lot!!
Thanks in advance. In Java. I'm having an issue with one of my methods. Ex: The...
Thanks in advance. In Java. I'm having an issue with one of my methods. Ex: The array being pass in is a character array with element: w, ' ', K, Q, k, q, ' ', -, ' ', 0, ' ', 1 public class Find { public void enPassant(char[] array) {   for(int i = 0; i < array.length; ++i) { if(array[i] == 'e') { count = i; } else { count += 0; } }    if(count > 0) {...
Need some assistance or ideas with this discussion question. Thanks in advance What are barriers to...
Need some assistance or ideas with this discussion question. Thanks in advance What are barriers to communication, and how do you remove them?
How do we change source code on java programming? Thanks
How do we change source code on java programming? Thanks
Hello and Happy Holidays...Can you get me started on this question? Thanks in advance! If two...
Hello and Happy Holidays...Can you get me started on this question? Thanks in advance! If two large companies merge, why might you want to run a multi-objective optimization to trade off the total cost of company one versus the total cost to company two? That is, why might this be better than running with just the objective of minimizing the total cost of the combined supply chain?
Hi, please include a detailed answer for both parts of this question, thanks! 1 A) Identify...
Hi, please include a detailed answer for both parts of this question, thanks! 1 A) Identify the changes in TSH and TRH secretion that you would expect to see in a patient with hypothyroidism due to iodine deficiency. (1 mark-50-100 words) 1 B) Using your knowledge of thyroid hormone regulation, explain why you would see these changes in TSH and TRH secretion. (2 marks-150-200 words)
Hi, I would like to know how to do this question. Thanks Suppose that basketball (Raptors)...
Hi, I would like to know how to do this question. Thanks Suppose that basketball (Raptors) tickets are $60 each and hockey (Maple Leafs) tickets are $24 each. If you had basketball and hockey tickets such that you would initially trade 2 basketball tickets for each hockey ticket, which tickets would you trade and what would be your equilibrium exchange of basketball for hockey tickets? A) Trade hockey for basketball tickets until you reach 0.4 hockey per basketball tickets B)...
Hi all, Can someone please answer this question. Please, list all steps! thanks! Machines A and...
Hi all, Can someone please answer this question. Please, list all steps! thanks! Machines A and B are mutually exclusive and are expected to produce the following real cash flows:       Cash Flows ($ thousands) Machine C0 C1 C2 C3 A –102 +112 +123 B –122 +112 +123 +135       The real opportunity cost of capital is 12%.    a. Calculate the NPV of each machine. (Do not round intermediate calculations. Enter your answers in dollars not in thousands, e.g....
This is 1 java question with its parts. Thanks! Create a class named Lease with fields...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly...
This is 1 java question with 3 parts/codes called student.java, ShowStudent.java, ShowStudent2.java. Thanks so much! Create...
This is 1 java question with 3 parts/codes called student.java, ShowStudent.java, ShowStudent2.java. Thanks so much! Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT