add lab13

This commit is contained in:
2022-03-07 08:18:03 +01:00
parent 9ba816c6d9
commit e6c2cd31ac
19 changed files with 1167 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,167 @@
package lab13_streams;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* This class allows to save a BMP file
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @version 1.0
*/
public class BMPWriter {
// Data writer
private DataOutputStream out = null;
// Image dimensions
private int width;
private int height;
private final int headerSize = 54;
/**
* Constructor
*
* @param fileName
* Name of the BMP file to save
* @param width
* Width of the image
* @param height
* Height of the image
* @throws FileNotFoundException
*/
BMPWriter(File file, int height, int width) throws FileNotFoundException {
this.width = width;
this.height = height;
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
}
/**
* Close the file when the class is destroyed
*/
protected void finalize() throws Throwable {
this.close();
}
/**
* Close the file
*
* @throws IOException
*/
public void close() throws IOException {
if (out != null) {
out.flush();
out.close();
}
}
/**
* Write the BMP header
*
* @throws IOException
*/
public void writeHeader() throws IOException {
// Write header
// You can refer to this web site
// (http://www.fastgraph.com/help/bmp_header_format.html) to
// understand the use of each value
// Signature
out.write(0x42);
out.write(0x4D);
// FileSize (Byte 3-6)
out.writeInt(convertToLED(width * height * 3 + headerSize));
// Reserved (Byte 7-10)
out.writeInt(0);
// Header Size (Byte 11-14)
out.writeInt(convertToLED(headerSize));
// TODO Complete header
out.flush();
}
/**
* Write a pixel data in the BMP file
*
* @param color
* @throws IOException
*/
public void writePixel(int color) throws IOException {
// TODO Complete here
}
/**
* Fill the image with a color gradient
*
* @throws IOException
*/
public void fillImage() throws IOException {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// Each pixel is written one after the other
// Blue
out.write(j % 256);
// Green
out.write(0);
// Red
out.write(i % 256);
}
insertPadding();
}
out.flush();
}
/**
* Write the pixel values of a graphic to the file
*
* @param display
* Graphic
* @throws IOException
*/
public void writeImage(SimpleGraphics display) throws IOException {
// TODO complete here
}
/**
* Insert the padding needed at the end of each line in a BMP file, to a end
* a line in a multiple of 4 (in byte)
*
* @throws IOException
*/
public void insertPadding() throws IOException {
// Padding for the end of line
for (int j = 0; j < (4 - ((3 * width) % 4)) % 4; j++)
out.write(0);
}
/**
* Convert an integer into a Little Endian integer
*
* @param input
* @return
*/
static private int convertToLED(int input) {
int result = 0;
for (int shiftBy = 24; shiftBy >= 0; shiftBy -= 8) {
result |= (input & 0xff) << shiftBy;
input >>= 8;
}
return result;
}
/**
* Convert a short number into a Little Endian short
*
* @param input
* @return
*/
static private short convertToLED(short input) {
return (short) (((input & 0xff) << 8) | ((input & 0xff00) >> 8));
}
}

View File

@ -0,0 +1,75 @@
package lab13_streams;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
/**
* A sample garage application for demonstrating text file output
* @author Pierre-André Mudry, HES-SO Valais 2010
*/
public class GarageManager {
HashMap<String, Integer> services = new HashMap<String, Integer>();
GarageManager(){
// The various services provided in this garage
services.put("Oil level control", 20);
services.put("Tire replacement ", 50);
services.put("Windshield exchange", 60);
services.put("Oil filter change", 210);
services.put("Battery replacement", 320);
services.put("Pollution control", 200);
services.put("Brake revision", 400);
}
String generateBill(int[] operationsPerformed){
int total_sum = 0;
Object[] keys = services.keySet().toArray();
String result = "";
result += "*************************\n";
result += "* Super Auto 20000 invoice ****\n";
result += "*******************************\n\n";
// Generate the text corresponding to the operations done in the garage
for(int i = 0; i < operationsPerformed.length; i++){
if(operationsPerformed[i] > services.size()){
System.out.println("Error, non existing prestation !");
System.exit(-1);
}
String cKey = (String) keys[operationsPerformed[i]];
result += "- " + cKey + " \t" + services.get(cKey) + "\n";
total_sum += services.get(cKey);
}
result += "\n----------------------------------\n";
result += " Invoice total \t\t" + total_sum + "\n";
result += "----------------------------------\n";
result += "\nPayment in 30 days. Thank you !";
return result;
}
public static void main(String[] args) {
GarageManager gm = new GarageManager();
// Prestation 0 is "Windshield exchange"
// Prestation 1 is "Battery replacement"
// Prestation 2 is "Oil level control"
// Prestation 3 is "Pollution control"
// Prestation 4 is "Tire replacement"
// Prestation 5 is "Brake revision"
// Prestation 6 is "Oil filter change"
int[] client1 = {1, 2, 3};
String bill1 = gm.generateBill(client1);
System.out.println(bill1);
int[] client2 = {4, 4, 4, 4, 4, 6};
String bill2 = gm.generateBill(client2);
System.out.println(bill2);
// Complete with your code here !
}
}

View File

@ -0,0 +1,37 @@
package lab13_streams;
import java.awt.Color;
import java.awt.Point;
/**
* Class stores the information corresponding to a line
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @version 1.0
*
*/
public class Line {
public Point p1, p2;
public Color color;
/**
* Constructor
*
* @param p1
* First point
* @param p2
* Second point
* @param color
* Line color
*/
public Line(Point p1, Point p2, Color color) {
this.p1 = p1;
this.p2 = p2;
this.color = color;
}
public String toString()
{
return "Line from " + p1.x + "-" + p1.y + " to " + p2.x + "-" + p2.y;
}
}

View File

@ -0,0 +1,145 @@
package lab13_streams;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
/**
* Part 1 of the lab session on streams
*
* @author <a href='mailto:pandre.mudry&#64;hevs.ch'> Pierre-Andre Mudry</a>
* @version 1.0
*/
public class ReadFileApplication {
static Vector<Line> parseFile(String in){
// This vector hold the lines
Vector<Line> lines = new Vector<Line>();
/*
* Read the lines from the CSV file
*/
try {
// Read the CSV File
BufferedReader csvReader = new BufferedReader(new FileReader(in));
// TODO: complete here
System.out.println(lines.size() + " shapes found in csv file.");
csvReader.close();
} catch (FileNotFoundException e) {
System.err.println("Bad input file " + in + " !\n");
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
System.err.println("Error reading file " + " !\n");
e.printStackTrace();
System.exit(-1);
}
return lines;
}
/**
* Entry point of the program
*/
public static void main(String[] args) {
Vector<Line> theLines = ReadFileApplication.parseFile("drawingTest.csv");
// Display what we read
ReadFileApplication.displayLines(theLines);
// Optional part
ReadFileApplication.writeSVG(theLines);
}
/**
* Writes the lines to an SVG file !
* @param lines
*/
static void writeSVG(Vector<Line> lines){
final String SVG_FILENAME = "drawingTest.svg";
/*
* Write the SVG File
*/
try {
SVGWriter svgWriter = new SVGWriter(new File(SVG_FILENAME));
svgWriter.writeHeader(getDimensions(lines));
svgWriter.writeLines(lines);
svgWriter.close();
} catch (FileNotFoundException e) {
System.err.println("Bad output file " + SVG_FILENAME + " !\n");
e.printStackTrace();
System.exit(-1);
}
}
/**
* Computes the required space on the screen for the given lines
* @param lines Different vector lines
* @return The space required to display that on screen
*/
static Dimension getDimensions(Vector<Line> lines){
// The dimension of the image, used for SVG output & the view Frame.
Dimension imgDim = new Dimension();
/*
* Compute the image dimension
*/
for (int i = 0; i < lines.size(); i++) {
imgDim.height = Math.max(imgDim.height, lines.get(i).p2.x);
imgDim.width = Math.max(imgDim.width, lines.get(i).p2.y);
}
imgDim.height += 50;
imgDim.width += 50;
return imgDim;
}
/**
* Displays a vector of lines on a graphical window
* @param lines
*/
static void displayLines(Vector<Line> lines){
// This is a display Frame for the lines
SimpleGraphics display;
// The dimension of the image, used for SVG output & the view Frame.
Dimension imgDim = new Dimension(0, 0);
/*
* Compute the image dimension
*/
for (int i = 0; i < lines.size(); i++) {
imgDim.height = Math.max(imgDim.height, lines.get(i).p2.x);
imgDim.width = Math.max(imgDim.width, lines.get(i).p2.y);
}
imgDim.height += 50;
imgDim.width += 50;
/*
* Display the lines
*/
display = new SimpleGraphics(imgDim.height, imgDim.width,"CSV File Drawing");
// Draw each line
for (int i = 0; i < lines.size(); i++){
Line p = lines.get(i);
display.setColor(p.color);
display.drawLine(p.p1.x, p.p1.y, p.p2.x, p.p2.y);
}
// Refresh the screen
for(int i = 0; i < 10; i++)
display.repaint();
}
}

View File

@ -0,0 +1,81 @@
package lab13_streams;
import java.awt.Dimension;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Vector;
/**
* This class allows to save a SVG file
*
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @version 1.0
*
*/
public class SVGWriter {
// Output stream. PrintStream enable to print strings (println()).
PrintStream svgOut;
/**
* Constructor
*
* @param svgFile
* File where to save the SVG drawing
* @throws FileNotFoundException
*/
public SVGWriter(File svgFile) throws FileNotFoundException {
svgOut = new PrintStream(new BufferedOutputStream(new FileOutputStream(svgFile)));
}
/**
* Write the SVG header to the file
*
* @param imgDim
* Dimension of the image
*/
public void writeHeader(Dimension imgDim) {
// Print XML Header
svgOut.println("<?xml version=\"1.0\" standalone=\"no\"?>");
svgOut.println("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">");
svgOut.print("<svg width=\"");
svgOut.print(imgDim.height);
svgOut.print("\" height=\"");
svgOut.print(imgDim.width);
svgOut.println("\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">");
}
/**
* Close the file when the class is destroyed
*/
protected void finalize() throws Throwable {
this.close();
}
/**
* Write the footer, flush the data and close the file
*/
public void close() {
// Print XML Footer
svgOut.println("</svg>");
if (svgOut != null) {
svgOut.flush();
svgOut.close();
}
}
/**
* Write the shapes contained in the dynamic list in the SVG file.
*
* @param shapes Vector of shapes
*/
void writeLines(Vector<Line> lines) {
// TODO Complete here
}
}

View File

@ -0,0 +1,587 @@
package lab13_streams;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
/**
* <p>This class is a simple interface to draw figures and graphics. It is based on
* the Swing framework and uses a {@link JFrame} as a drawing billboard.</p>
*
* <p>Several methods are provided to paint things on the screen.</p>
*
* <p>
* Revision notes :
* <ul>
* <li>1.0 (roi) :<br>
* <ul>
* <li>Initial revision, October 5th 2009
* </ul>
* </li>
*
* <li>
* 1.1 (mui) :<br>
* <ul>
* <li>Changed displayFrame to DisplayFrame(naming convention) - Added new constructor for setting the window title.
* <li>Changed visibility of background as an accessible member {@link #backgroundColor}.
* <li>Made the clean function faster by using a clipping mask.
* <li>Added {@link #setPixel(int, int, Color)} and {@link #setPixel(int, int, int)} methods.
* <li>Changed behaviour of the init function to display {@link #backgroundColor} as initial color.
* <li>Added default constructor behaviour to enable changing window title.
* <li>Added threaded pause function ({@link #pause(int)}).
* </ul>
*
* <li>
* 1.2 (mui) :<br>
* <ul>
* <li>Changed paint method for flicker-free and smoother behaviour (which also means that <code>repaint</code> calls have
* been replaced by <code>invalidate</code> calls).
* <li>Added rendering hints for nicer display.
* </ul>
*
* <li>
* 1.21 (mui) :<br>
* <ul>
* <li>Window is now centered on screen at startup.
* <li>Window is now non-resizable.
* </ul>
*
* <li>
* 1.3 (mui) :<br>
* <ul>
* <li>Added key manager interface
* <li>Changed members visibility
* <li>Removed obsolete <code>display()</code> method
* </ul>
*
* <li>1.4 (roi) :<br>
* <ul>
* <li>Added {@link fillOval()} method
* </ul>
*
* <li>1.41 (mui) :<br>
* <ul>
* <li>Changed how constructors handle rendering hints so
* that the quality can be chosen in the constructor instead
* of being hard-coded.
* <li>Added {@link #drawRotatedPicture(int, int, double, double, String)} method
* </ul>
*
* <li>1.42 (mui) :<br>
* <ul>
* <li>Changed fields visibility and added getters/setters
* </ul>
*
* <li>2.0 (mui) :<br>
* <ul>
* <li>Major overhaul in drawing for increased speed and stability using
* double buffering.
*
* </ul>
*
* </ul>
* </p>
*
*
* @author Pierre-Andre Mudry <a href='mailto:pandre.mudry&#64;hevs.ch'></a>
* @author Pierre Roduit (pierre.roduit@hevs.ch)
* @version 2.00
*
*/
public class SimpleGraphics {
/**
* The subclass which create the windows frame
*/
protected DisplayFrame display;
protected int frameWidth;
protected int frameHeight;
protected boolean enableRenderingHints = false;
protected boolean checkBorders = true;
protected int backgroundColor = Color.white.getRGB();
/**
* Constructor
* @param width Width of the display window
* @param height Height of the display window
* @param title Title of the display window
* @param high_quality Use high quality rendering (slower)
*/
public SimpleGraphics(int width, int height, String title, boolean high_quality){
enableRenderingHints = high_quality;
display = new DisplayFrame(width, height, title);
setFrameWidth(width);
setFrameHeight(height);
clear();
}
/**
* @see #SimpleGraphics(int, int, String, boolean)
*/
public SimpleGraphics(int width, int height, String title){
this(width, height, title, false);
}
/**
* @see #SimpleGraphics(int, int, String, boolean)
*/
public SimpleGraphics(int width, int height) {
this(width, height, null, false);
}
/**
* @see #SimpleGraphics(int, int, String, boolean)
*/
public SimpleGraphics(int width, int height, boolean rendering_hints){
this(width, height, null, rendering_hints);
}
/**
* Sets a keyboard listener
* @param k The KeyListener to listen to
*/
public void setKeyManager(KeyListener k){
display.addKeyListener(k);
}
/**
* Method which cleans up the display. Everything becomes the background
* again.
*/
public void clear() {
display.g2d.clearRect(0, 0, display.imageWidth, display.imageHeight);
display.invalidate();
}
/**
* Method which cleans up the display. Everything becomes the background
* again.
*/
public void clear(Color c) {
Color old = display.g2d.getBackground();
display.g2d.setBackground(c);
display.g2d.clearRect(0, 0, display.imageWidth, display.imageHeight);
display.g2d.setBackground(old);
display.invalidate();
}
/**
* Set the color of the future drawings
*
* @param c Selected color for drawing
*/
public void setColor(Color c) {
display.g2d.setColor(c);
}
/**
* Draw the selected pixel with the color selected with setColor.
*
* @param x
* X coordinate of the pixel
* @param y
* Y coordinate of the pixel
*/
public void setPixel(int x, int y) {
// Test that the pixel to set is in the frame
if ((x < 0) || (y < 0) || (x >= getFrameWidth()) || (y >= getFrameHeight())){
if(checkBorders)
System.out.println("Coordinates out of frame");
}
else{
display.img.setRGB(x, y, display.g2d.getColor().getRGB());
}
display.invalidate();
}
/**
* Draws a pixel with a given color. Does not change the current color.
*
* @param x
* X coordinate
* @param y
* Y coordinate
* @param c
* Color to use for this pixel (this pixel only, see
* {@link #setColor(Color)}
*/
public void setPixel(int x, int y, Color c) {
Color oldColor = display.g2d.getColor();
setColor(c);
setPixel(x, y);
setColor(oldColor);
display.invalidate();
}
/**
* Draws a pixel with a given color. Does not change the current color
*
* @param x
* X coordinate
* @param y
* Y coordinate
* @param c
* Color to use (RGB coded)
*/
public void setPixel(int x, int y, int c) {
setPixel(x, y, new Color(c));
}
/**
* Clears a pixel's by replacing its current color with the background's
*
* @param x
* X coordinate of the pixel to set
* @param y
* Y coordinate of the pixel to set
*/
public void clearPixel(int x, int y) {
setPixel(x, y, backgroundColor);
}
/**
* Draws a string at a given location. Note that the boundaries are not
* checked and text may be painted outside the window.
*
* @param x
* X coordinate of the text to be drawn
* @param y
* Y coordinate of the text to be drawn
* @param text
* The text to be drawn
*/
public void paintText(int x, int y, String text) {
display.g2d.drawString(text, x, y);
display.invalidate();
}
/**
* Methods which shows the buffered image. For most drawing methods, it is
* useless, as this method is called in the drawing method itself. However,
* for method such as setPixel, it is necessary.
*/
public void repaint(){
display.myPaint();
}
/**
* Stops the execution of the program for a certain period
* @param delay Number of millisecond to stop execution
*/
public void pause(int delay){
try{
Thread.sleep(delay);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
/**
* Draw a line from P1 to P2 in the color selected with setColor.
*
* @param p1x
* X coordinate of P1
* @param p1y
* Y coordinate of P1
* @param p2x
* X coordinate of P2
* @param p2y
* Y coordinate of P2
*/
public void drawLine(int p1x, int p1y, int p2x, int p2y) {
display.g2d.drawLine(p1x, p1y, p2x, p2y);
display.invalidate();
}
public void drawPolygon(Polygon p){
setColor(Color.gray);
display.g2d.fill(p);
display.g2d.drawPolygon(p);
display.invalidate();
}
public void drawFilledPolygon(Polygon p, Color c){
Color oldColor = display.g2d.getColor();
setColor(c);
display.g2d.fill(p);
display.g2d.drawPolygon(p);
display.invalidate();
setColor(oldColor);
}
/**
* Draw an empty rectangle in the color selected with setColor().
*
* @param posX
* X coordinate of the top left corner of the rectangle
* @param posY
* Y coordinate of the top left corner of the rectangle
* @param width
* Width of the rectangle
* @param height
* Height of the rectangle
*/
public void drawRect(int posX, int posY, int width, int height) {
display.g2d.drawRect(posX, posY, width, height);
display.invalidate();
}
/**
* Draws a circle starting from <code>(centerX, centerY)</code>
* @param posX X top-left position of the circle
* @param posY Y top-left position of the circle
* @param diameter Diameter of the drawn circle
*/
public void drawCircle(int posX, int posY, int diameter){
display.g2d.drawOval(posX, posY, diameter, diameter);
display.invalidate();
}
/**
* Write a given string at <code>(posX, posY)</code>
* @param posX X position of string
* @param posY Y position of string
* @param str the string to write
*/
public void drawString(int posX, int posY, String str)
{
display.g2d.drawString(str, posX, posY);
display.invalidate();
}
public void drawString(int posX, int posY, String str, Color color, int size)
{
Font oldFont = display.g2d.getFont();
Color oldColor = display.g2d.getColor();
Font font = new Font("SansSerif", Font.PLAIN, size);
display.g2d.setFont(font);
display.g2d.setColor(color);
display.g2d.drawString(str, posX, posY);
display.g2d.setFont(oldFont);
display.g2d.setColor(oldColor);
display.invalidate();
}
/**
* Draw a centered picture from a file (gif, jpg, png) to <code>(posX, posY)</code>
* @param posX X position of the image
* @param posY Y position of the image
* @param filename path of the image file
*/
public void drawPicture(int posX, int posY, SimpleGraphicsBitmap bitmap)
{
display.g2d.drawImage(bitmap.mBitmap,posX-bitmap.getWidth()/2,posY-bitmap.getHeight()/2,null);
display.invalidate();
}
/**
* Draw a centered picture from a file (gif, jpg, png) to <code>(posX, posY)</code>
* @param posX X position of the image
* @param posY Y position of the image
* @param angle The rotation angle of the image to be drawn
* @param imageName path of the image file
*/
public void drawTransformedPicture(int posX, int posY, double angle, double scale, String imageName)
{
drawTransformedPicture(posX, posY, angle, scale, new SimpleGraphicsBitmap(imageName));
}
/**
* Draw a centered picture from a file (gif, jpg, png) to <code>(posX, posY)</code>
* @param posX X position of the image
* @param posY Y position of the image
* @param angle The rotation angle of the image to be drawn
* @param bitmap A {@link #SimpleGraphicsBitmap()} bitmap
*/
public void drawTransformedPicture(int posX, int posY, double angle, double scale, SimpleGraphicsBitmap bitmap)
{
AffineTransform t = new AffineTransform();
t.rotate(angle, posX, posY);
t.translate(posX-bitmap.getWidth()/2, posY-bitmap.getHeight()/2);
t.scale(scale, scale);
display.g2d.drawImage(bitmap.mBitmap, t, null);
display.invalidate();
}
/**
* Draw a filled rectangle in the color selected with setColor.
*
* @param posX
* X coordinate of the top left corner of the rectangle
* @param posY
* Y coordinate of the top left corner of the rectangle
* @param width
* Width of the rectangle
* @param height
* Height of the rectangle
*/
public void drawFillRect(int posX, int posY, int width, int height) {
display.g2d.fillRect(posX, posY, width, height);
display.invalidate();
}
/**
* Getters and setters
*/
public void setFrameWidth(int frameWidth) {
this.frameWidth = frameWidth;
}
public int getFrameWidth() {
return frameWidth;
}
public void setFrameHeight(int frameHeight) {
this.frameHeight = frameHeight;
}
public int getFrameHeight() {
return frameHeight;
}
public Graphics2D getGraphics(){
return display.g2d;
}
/**
* Subclass which extends JFrame, to create the windows and keep all the
* frame stuff hidden from the end user.
*/
protected class DisplayFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 2D graphic used to draw
*/
public Graphics2D g2d;
/**
* Buffered image used to draw g2d into and to set pixels.
*/
public BufferedImage img;
/**
* Color used to draw objects and pixels
*/
private int imageWidth, imageHeight;
/**
* Default constructor
*
* @param width
* Number of pixels for the window's width
* @param height
* Number of pixels for the window's height
* @param title
* Displayed title of the window
*/
public DisplayFrame(int width, int height, String title) {
super(title);
if(title == null){
this.setTitle("Informatics 1 - SimpleGraphics window");
}
imageWidth = width;
imageHeight = height;
this.setSize(this.imageWidth, this.imageHeight);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Prevent resizing
this.setResizable(false);
// Display the windows at the center of the screen
this.setLocationRelativeTo(null);
// The frame must be set to visible, if we want to access the
// corresponding Graphics to draw into.
this.setVisible(true);
// Fixes the bug with the invalid buffer strategies
while(!this.isVisible() && !this.isValid())
{}
// Create a double buffer strategy
this.createBufferStrategy(2);
this.setBackground(Color.white);
// Create graphics and image
img = new BufferedImage(this.imageWidth, this.imageHeight, BufferedImage.TYPE_INT_ARGB);
g2d = img.createGraphics();
// Set rendering hints for nicer display (if speed allows)
if (enableRenderingHints) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
}
this.setIgnoreRepaint(true);
Font font = new Font("SansSerif", Font.PLAIN, 12);
g2d.setFont(font);
// Set drawing color to black
g2d.setColor(Color.black);
g2d.setBackground(new Color(backgroundColor));
}
/**
* Override the paint method.
*/
public void myPaint() {
BufferStrategy bf = this.getBufferStrategy();
while(bf == null)
bf = this.getBufferStrategy();
do {
do {
Graphics2D g1 = (Graphics2D) bf.getDrawGraphics();
g1.drawImage(img, 0, 0, this);
// We don't need that anymore
g1.dispose();
} while (bf.contentsRestored());
// Shows the contents of the backbuffer on the screen.
bf.show();
// Tell the system to draw, otherwise it can take a few extra ms
// until it does
Toolkit.getDefaultToolkit().sync();
// Repeat the rendering if the drawing buffer was lost
} while (bf.contentsLost());
}
}
static {
System.setProperty("sun.java2d.transaccel", "True");
// System.setProperty("sun.java2d.trace", "timestamp,log,count");
// System.setProperty("sun.java2d.opengl", "True");
//System.setProperty("sun.java2d.d3d", "True");
System.setProperty("sun.java2d.ddforcevram", "True");
}
}

View File

@ -0,0 +1,43 @@
package lab13_streams;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* An implementation of the {@link CommonBitmap} interface that is intended to
* be used on an operating systems that provides the {@link java.awt.BufferedImage} classes
*
* @version 1.0, April 2010
* @author <a href='mailto:pandre.mudry&#64;hevs.ch'> Pierre-Andr<64> Mudry</a>
*/
public class SimpleGraphicsBitmap {
protected BufferedImage mBitmap;
public SimpleGraphicsBitmap(String imageName)
{
try {
mBitmap = ImageIO.read(SimpleGraphicsBitmap.class.getResource(imageName));
} catch (Exception e) {
System.out.println("Could not find image " + imageName + ", exiting !");
e.printStackTrace();
System.exit(-1);
}
}
public int getWidth()
{
return mBitmap.getWidth();
}
public int getHeight()
{
return mBitmap.getHeight();
}
public BufferedImage getImage(){
return this.mBitmap;
}
}

View File

@ -0,0 +1,16 @@
50;250;50;650;20;20;20
50;650;450;650;20;20;20
450;650;450;250;20;20;20
50;250;250;50;255;0;0
250;50;320;130;255;0;0
380;170;450;250;255;0;0
320;130;320;80;0;255;0
320;80;380;80;0;255;0
380;80;380;170;0;255;0
150;650;150;450;0;0;255
150;450;230;450;0;0;255
230;450;230;650;0;0;255
280;550;280;450;0;0;255
280;450;420;450;0;0;255
420;450;420;550;0;0;255
280;550;420;550;0;0;255
1 50 250 50 650 20 20 20
2 50 650 450 650 20 20 20
3 450 650 450 250 20 20 20
4 50 250 250 50 255 0 0
5 250 50 320 130 255 0 0
6 380 170 450 250 255 0 0
7 320 130 320 80 0 255 0
8 320 80 380 80 0 255 0
9 380 80 380 170 0 255 0
10 150 650 150 450 0 0 255
11 150 450 230 450 0 0 255
12 230 450 230 650 0 0 255
13 280 550 280 450 0 0 255
14 280 450 420 450 0 0 255
15 420 450 420 550 0 0 255
16 280 550 420 550 0 0 255