编程辅导 C C++ Java Python MIPS Processing 网络家教 在线辅导

编程家教 远程写代码 Debug 讲解答疑 课后答疑 不是中介,本人直接答疑

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

Graphics Abstractions

The main goals of this assignment are to gain experience with the decorator and composite patterns, and to explore testing objects with more complex behaviors. And to get a little more practice with arrays.

2 Getting Started

Before writing code of your own, you will likely want to look over the code that is provided. Most of this you should have seen before, either in the previous problem set or in lecture, but there are a few things that are different:

  • BoundingBox (Javadoc) is now a class rather than an interface, because a need for other implementations seems unlikely, and a final class makes it easier to preserve invariants. There’s a new constant, EMPTY, for representing the empty bounding box.

  • Many methods that work with points and colors now take Point<Integer>Point<Double>, and Color objects instead. However, the colors written to Rasters are still represented as (32-bit) ints because that’s what must eventually be written to the PixelSurface.

  • The CirclesDemo class contains some commented-out code that will exercise the code you need to write, once it’s ready. (But this is no substitute for actual testing.)

  • This problem set builds on the previous problem set—it depends on the IntegerPointDoublePoint, and Colors classes that you implemented. You are free to use your implementations of these classes, but if you prefer to use mine, compiled binary class files are available in pset07.jar.

3 Your Task

3.1 ArrayRaster

The first part of your task is to implement the Raster interface with a more efficient representation than the ArrayList<ArrayList<Integer>> representation that we’ve been using. To this end, you will write a class ArrayRaster that implements the Raster interface, while representing the raster as a single row-major int[] of length width() * height().

In addition to implementing the Raster interface, ArrayRaster should have two public constructors and one additional method:

  • public ArrayRaster(int width, int height) constructs an ArrayRaster with the given width and height, allocating a buffer of the appropriate size.

  • public ArrayRaster(int width, int height, int[] buffer) allows the client to supply the buffer for the new ArrayRaster to use. This is useful if we already have an array that we wish to render into and wish to avoid copying.

  • public int[] getBuffer() returns the ArrayRaster’s buffer to the client. Because it does not make a copy, the ArrayRaster continues to hold a reference to the returned buffer, and changes via one reference will be visible via the other.

There are additional details in the ArrayRaster Javadoc, so I recommend that you read it carefully.

  1. Implement the ArrayRaster class, as described above. You may extend the AbstractRaster class if you wish. Be sure to test your code thoroughly.

  2. Modify the RasterApplication class to use your ArrayRaster rather than ListRaster.

3.2 addTransparency

The Sampleable interface is simple—just two methods—but it gives us a lot of flexibility to compose and transform image elements by defining decorators, which dynamically extend an object with different behavior. We saw some geometric transformations in class, such as the Sampleables.scale(double, Sampleable) method that scales a Sampleable. It is also possible to write decorators that modify the colors of the decorated image element. As an example, you will write aSampleable decorator that makes the decorated Sampleable more transparent.

  1. In class Sampleables, write a public static method Sampleable addTransparency(double alpha, Sampleable base) that multiplies the alpha of each point in base by the provided alpha. Be sure to test your method.

There are additional details in the Sampleables.addTransparency Javadoc that may be helpful.

3.3 overlay

In class, we wrote a static method Sampleable overlay(Sampleable... layers) (renamed opaqueOverlay in the version of Sampleables.java that I’ve provided you) that composes several Sampleables by overlaying them from front to back.2 This method respects full transparency, in the sense that when rendering a pixel in a layer where the alpha is 0, it goes to the layer behind it, and so on, until running out of layers.

Now it’s time to write a version of overlay that properly handles partial transparency as well. You’ve already written an overlay method for two colors, and we can define the overlay of n colors in terms of that. Given a front-to-back sequence of colors c1,,cn, we can define their overlay recursively in terms of the two-color overlay method. Using uppercase Overlay for the n-ary version and overlay for the binary version:

 

Overlay(c1,c2,,cn)Overlay()==c1.overlay(Overlay(c2,,cn))TRANSPARENT

 

In an implementation of this algorithm, the recursion should stop early in the case where the c1 is opaque, since in that case the layers behind it are hidden. (Note that there is also an iterative version of the algorithm, which is most easily expressed using a ColorBlender class like in lecture. Talk to me if you want to try this.)

  1. In class Sampleables, write a public static method Sampleable overlay(Sampleable... layers) that overlays the layers front-to-back while respecting each layer’s alpha channel. Be sure to test your method.

3.4 Make a Pretty Picture

Use the library we are building to write a program that renders something interesting to the screen. Can you do more interesting things by factoring out some functionality, and perhaps parameterizing it? If you make something especially neat, post a screen shot to Piazza. (There is no deliverable associated with this task.)

 

  1.  

相关推荐