first part of lab 8

This commit is contained in:
Rémi Heredero 2021-12-12 18:01:08 +01:00
parent 7a0d8f3cca
commit cd6bd96b94
17 changed files with 511 additions and 0 deletions

21
.vscode/launch.json vendored
View File

@ -4,6 +4,27 @@
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch TheaterApplication",
"request": "launch",
"mainClass": "lab8_tableaux.TheaterApplication",
"projectName": "Labo_6a2f7ad1"
},
{
"type": "java",
"name": "Launch Task1",
"request": "launch",
"mainClass": "lab8_tableaux.Task1",
"projectName": "Labo_6a2f7ad1"
},
{
"type": "java",
"name": "Launch App",
"request": "launch",
"mainClass": "lab8_tableaux.App",
"projectName": "Labo_6a2f7ad1"
},
{
"type": "java",
"name": "Launch Task2Runner",

Binary file not shown.

View File

@ -0,0 +1,19 @@
package lab8_tableaux;
public class Seat {
public boolean busy;
private char row;
private int column;
Seat(char row, int column){
this.row = row;
this.column = column;
}
public String getPlace(){
String s = "";
s += this.column;
s += this.row;
return s;
}
}

View File

@ -0,0 +1,19 @@
package lab8_tableaux;
public class Task0 {
public static void main(String[] args) {
int[] foo = even_array(5);
for (int i = 0; i < foo.length; i++) {
System.out.println(foo[i]);
}
}
public static int[] even_array(int n){
int[] foo = new int[n];
for (int i = 0; i < foo.length; i++) {
foo[i] = i*2;
}
return foo;
}
}

View File

@ -0,0 +1,8 @@
package lab8_tableaux;
public class Task1 {
public static void main(String[] args) {
Seat mySeat = new Seat('a', 7);
System.out.println(mySeat.busy);
}
}

View File

@ -0,0 +1,104 @@
package lab8_tableaux;
public class Theater {
private Seat[][] seats;
Theater(int r, int c){
seats = new Seat[c][r];
for (int i = 1; i <= c; i++) {
for (char j = 'A'; j < 'A'+r; j++) {
seats[i-1][j-'A'] = new Seat(j,i);
}
}
}
public String getSeat(int row, int column){
return seats[column][row].getPlace();
}
public boolean isSeatBusy (int row, int column){
return seats[column][row].busy;
}
public boolean occupySeat(int row, int column) {
if (isSeatBusy(row, column)) {
return false;
}
seats[column][row].busy = true;
return true;
}
private int numberOfBusySeats(){
int total =0 ;
int r = seats[0].length;
int c = seats.length;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if(isSeatBusy(i, j)){
total++;
}
}
}
return total;
}
private int numberOfSeats(){
int total =0 ;
int r = seats[0].length;
int c = seats.length;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
total++;
}
}
return total;
}
public String occupation() {
String s = "";
s += "Theater occupation: ";
s += numberOfBusySeats();
s += " / ";
s += numberOfSeats();
return s;
}
public String toString(){
String s = "";
s += "Theater seats occupation:";
s += "\n";
s += "\n";
s += " ";
int r = seats[0].length;
int c = seats.length;
System.out.println(c + "x" + r);
for (int i = 0; i < c; i++) {
s += " ";
s += i;
}
s += "\n";
for (int i = 0; i < r; i++) {
s += i;
s += " ";
for (int j = 0; j < c; j++) {
if (isSeatBusy(i, j)) {
s += "X";
} else {
s += " ";
}
s += space(j);
}
s += "\n";
}
return s;
}
private String space (int i){
String s = "";
int length = String.valueOf(i).length();
for (int j = 0; j < length; j++) {
s += " ";
}
return s;
}
}

View File

@ -0,0 +1,24 @@
package lab8_tableaux;
public class TheaterApplication {
public static void main(String[] args) {
Theater cinema = new Theater(10, 20);
cinema.occupySeat(2, 1);
cinema.occupySeat(2, 3);
cinema.occupySeat(2, 5);
cinema.occupySeat(2, 8);
cinema.occupySeat(0, 1);
cinema.occupySeat(0, 3);
cinema.occupySeat(0, 5);
cinema.occupySeat(0, 8);
cinema.occupySeat(0, 9);
cinema.occupySeat(0, 10);
cinema.occupySeat(0, 11);
cinema.occupySeat(0, 13);
cinema.occupySeat(0, 15);
cinema.occupySeat(0, 18);
System.out.println(cinema);
System.out.println(cinema.occupation());
}
}

View File

@ -0,0 +1,62 @@
package lab9_image_processing;
/**
* This class implements the various image filters
* @author Pierre-André Mudry
* @date 2012
* @version 1.0
*/
public class ImageFilters {
/**
* Simply duplicates every pixel from the source image
* @param a
* @return
*/
public static int[][] duplicate(int[][] a) {
/**
* Write your code hereunder
*/
return null;
}
/**
* Dichotomy of the image
* @param a
* @param threshold
* @return
*/
public static int[][] threshold(int[][] a, int threshold) {
return a;
}
/**
* Mean filter that blurs the image a bit
* @param a
* @return
*/
public static int[][] mean(int[][] a) {
return a;
}
/**
* Derivative of the image
* @param a
* @return
*/
public static int[][] derivative(int[][] a) {
return a;
}
/**
* Sobel filter of the image
* @param a
* @param intensity
* @return
*/
public static int[][] sobel(int[][] a, double intensity) {
return a;
}
}

View File

@ -0,0 +1,22 @@
package lab9_image_processing;
import hevs.graphics.ImageGraphics;
public class ImageProcessing {
public static void main(String[] args) {
final String imageUsed = "/images/rice.jpg";
/**
* Create the windows from images
*/
ImageGraphics org = new ImageGraphics(imageUsed, "Original", -450, -250);
ImageGraphics cpy = new ImageGraphics(imageUsed, "Copy", 0, -250);
int[][] thePixels = org.getPixelsBW();
int[][] theCopy = ImageFilters.duplicate(thePixels);
// Simple copy and display
cpy.setPixelsBW(theCopy);
}
}

View File

@ -0,0 +1,232 @@
package lab9_image_processing.hevs.graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
* This class was made to deal with images as grayscale multidimensional arrays.
* Mainly used in the ImageProcessing lab
*
* It expects the images to reside in the src directory
*
* @author Pierre-Andre Mudry 2011
*/
public class ImageGraphics extends JFrame {
private static final long serialVersionUID = 6832022057915586803L;
private BufferedImage backgroundBitmap = null;
private int w, h;
public ImageGraphics(String backGroundFilePath, String title, int xPositionOffset, int yPositionOffset) {
try {
// Fill the frame content with the image
try {
backgroundBitmap = ImageIO.read(ImageGraphics.class.getResource(backGroundFilePath));
w = backgroundBitmap.getWidth();
h = backgroundBitmap.getHeight();
} catch (Exception e) {
System.out.println("Could not find image " + backGroundFilePath + ", exiting !");
e.printStackTrace();
System.exit(-1);
}
this.setResizable(false);
this.setSize(backgroundBitmap.getWidth(),backgroundBitmap.getHeight());
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// Determine the new location of the window
int w = this.getSize().width;
int h = this.getSize().height;
int x = (dim.width - w) / 2 + xPositionOffset;
int y = (dim.height - h) / 2 + yPositionOffset;
// Move the window
this.setLocation(x, y);
this.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Sets a grayscale pixel, does not sets values for invalid pixels
* outside the screen. Does not repaint the screen either because it
* is slow. If required, please call {@link #repaint()} if needed after
* you have updated all the pixels you need.
*
* @param x
* @param y
* @param intensity
*/
public void setPixelBW(int x, int y, int intensity) {
if (!((x < 0) || (y < 0) || (x >= w) || (y >= h))) {
backgroundBitmap.setRGB(x, y, intensity << 16 | intensity << 8| intensity);
}
}
/**
* Sets an array of grayscale pixels (from 0 to 255) and displays them
* @param pixels
*/
public void setPixelsBW(int[][] pixels) {
try {
if (pixels[0].length != h || pixels.length != w) {
throw new Exception("Invalid size of the pixel array !");
}
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++) {
// FIXME this is slow, should use rasters instead
int c = pixels[i][j] << 16 | pixels[i][j] << 8 | pixels[i][j];
backgroundBitmap.setRGB(i, j, c);
}
this.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Sets an array of pixels of Color and displays them
* @param pixels
*/
public void setPixelsColor(Color[][] pixels) {
try {
if (pixels[0].length != h || pixels.length != w) {
throw new Exception("Invalid size of the pixel array !");
}
// FIXME this is slow, should use rasters instead
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++) {
backgroundBitmap.setRGB(i, j, pixels[i][j].getRGB());
}
this.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Gets a single pixel from the background image and returns its
* grayscale value
*
* @param x
* @param y
* @return
*/
public int getPixelBW(int x, int y) {
if ((x < 0) || (y < 0) || (x >= w) || (y >= h)) {
return 0;
} else {
// Inside the image. Make the gray conversion and return the value
Color c = new Color(backgroundBitmap.getRGB(x, y));
return (int) (0.3 * c.getRed() + 0.59 * c.getGreen() + 0.11 * c.getBlue());
}
}
/**
* Gets the array of the pixels (which have been converted to grayscale
* if required)
*
* @return The arrays of gray pixels
*/
public int[][] getPixelsBW() {
int[][] values = new int[w][h];
// FIXME this is slow
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++) {
Color c = new Color(backgroundBitmap.getRGB(i, j));
values[i][j] = (int) (0.3 * c.getRed() + 0.59 * c.getGreen() + 0.11 * c.getBlue());
}
return values;
}
/**
* Gets the array of the pixels as Colors (see #Color)
* @return The arrays of pixels
*/
public Color[][] getPixelsColor(){
Color[][] values = new Color[w][h];
// FIXME this is slow
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++) {
values[i][j] = new Color(backgroundBitmap.getRGB(i, j));
}
return values;
}
/**
* Converts a color array to a black-or-white array
* @param c The color array
* @return The array converted to BW
*/
public static Color[][] convertToGray(Color[][] c){
int w = c.length; int h = c[0].length;
Color[][] values = new Color[w][h];
// FIXME this is slow
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++) {
Color col = c[i][j];
int intColor = (int)(0.3 * col.getRed() + 0.59 * col.getGreen() + 0.11 * col.getBlue());
values[i][j] = new Color(intColor, intColor, intColor);
}
return values;
}
/**
* Converts a color array to a black-or-white array
* @param c The color array
* @return The array converted to BW
*/
public static int[][] convertToGrayInt(Color[][] c){
int w = c.length; int h = c[0].length;
int[][] values = new int[w][h];
// FIXME this is slow
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++) {
Color col = c[i][j];
int intColor = (int)(0.3 * col.getRed() + 0.59 * col.getGreen() + 0.11 * col.getBlue());
values[i][j] = intColor;
}
return values;
}
/**
* Paint method
*/
public void paint(Graphics g) {
g.drawImage(backgroundBitmap, 0, 0, null);
g.dispose();
}
public static void main(String args[]) {
final String imageUsed = "/images/lena.bmp";
ImageGraphics org = new ImageGraphics(imageUsed, "Original", 0, 0);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 768 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 279 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB