← Vissza
A feladat forráskódja:
package szinkep;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
/**
* Az egyes pixelek színösszetevőinek (RGB) adatai.
* @author comtur
*/
class Pixel {
int r;
int g;
int b;
public Pixel(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
@Override
public String toString() {
return r + " " + g + " " + b;
}
}
/**
*
* @author comtur
*/
public class Szinkep {
static Pixel[][] pic = new Pixel[50][50];
static Pixel[][] picWithBorder = new Pixel[50][50];
public static void main(String[] args) {
task1();
task2();
task3();
task4();
task5();
task6();
task7();
}
private static void task1() {
taskNum(1);
System.out.println("(Adat beolvasása)");
try (BufferedReader br = new BufferedReader(new FileReader("kep.txt"))) {
String line;
int i = 0;
int j = 0;
while ((line = br.readLine()) != null) {
String[] rgbData = line.split(" ");
Pixel p = new Pixel(Integer.valueOf(rgbData[0]),
Integer.valueOf(rgbData[1]), Integer.valueOf(rgbData[2]));
pic[i][j] = p;
if (i == 49) {
i = 0;
j++;
} else {
i++;
}
}
} catch (IOException ioe) {
System.err.println(ioe);
}
}
private static void taskNum(int i) {
System.out.println("\n" + i + ". feladat");
System.out.println("----------");
}
private static void task2() {
taskNum(2);
Scanner sc = new Scanner(System.in);
System.out.print("Kérek egy RGB kódot, szóközzel elválasztva! ");
Pixel userPixel = null;
try {
String[] userRGB = sc.nextLine().split(" ");
userPixel = new Pixel(Integer.parseInt(userRGB[0]),
Integer.parseInt(userRGB[1]), Integer.parseInt(userRGB[2]));
} catch (Exception e) {
System.err.println(e);
}
boolean found = false;
for (Pixel[] pixels : pic) {
for (Pixel pixel : pixels) {
if (pixel.r == userPixel.r && pixel.g == userPixel.g && pixel.b == userPixel.b) {
System.out.println("A bekért szín megtalálható a képen.");
found = true;
break;
}
}
if (found) {
break;
}
}
if (!found) {
System.out.println("A bekért szín NEM található meg a képen.");
}
}
private static void task3() {
taskNum(3);
int row = 35 - 1;
int col = 8 - 1;
Pixel pixToSearch = pic[row][col];
int count35row = 0;
for (int i = 0; i < pic.length; i++) {
Pixel analyzed = pic[row][i];
if (pixToSearch.r == analyzed.r && pixToSearch.g == analyzed.g && pixToSearch.b == analyzed.b) {
count35row++;
}
}
int count8col = 0;
for (int i = 0; i < pic[0].length; i++) {
Pixel analyzed = pic[i][col];
if (pixToSearch.r == analyzed.r && pixToSearch.g == analyzed.g && pixToSearch.b == analyzed.b) {
count8col++;
}
}
System.out.println("A megadott pixel előfordulásai - Sorban: " + count35row + " Oszlopban: " + count8col);
}
private static void task4() {
taskNum(4);
int countR = 0;
int countG = 0;
int countB = 0;
Pixel red = new Pixel(255, 0, 0);
Pixel green = new Pixel(0, 255, 0);
Pixel blue = new Pixel(0, 0, 255);
for (Pixel[] pixels : pic) {
for (Pixel pixel : pixels) {
if (red.r == pixel.r && red.g == pixel.g && red.b == pixel.b) {
countR++;
}
if (green.r == pixel.r && green.g == pixel.g && green.b == pixel.b) {
countG++;
}
if (blue.r == pixel.r && blue.g == pixel.g && blue.b == pixel.b) {
countB++;
}
}
}
System.out.print("Top1 szereplő szín: ");
if (countR > countG) {
if (countR > countB) {
System.out.println("piros.");
} else {
System.out.println("kék.");
}
} else {
System.out.println(countG > countB ? "zöld." : "kék.");
}
}
private static void task5() {
taskNum(5);
System.out.println("(Keret készítése)");
Pixel black = new Pixel(0, 0, 0);
for (int i = 0; i < pic.length; i++) {
for (int j = 0; j < pic[0].length; j++) {
if (i < 3 || j < 3 || i >= 47 || j >= 47) {
picWithBorder[i][j] = black;
} else {
picWithBorder[i][j] = pic[i][j];
}
}
}
}
private static void task6() {
taskNum(6);
System.out.println("(Keretes kép fájlba írása)");
try (PrintWriter pw = new PrintWriter("keretes.txt")) {
for (int i = 0; i < picWithBorder.length; i++) {
for (int j = 0; j < picWithBorder[0].length; j++) {
pw.println(picWithBorder[i][j]);
}
}
} catch (FileNotFoundException fnfe) {
System.err.println(fnfe);
}
}
private static void task7() {
taskNum(7);
System.out.println("Kiindulási feltételezés: a képen VAN legalább egy sárga téglalap.");
System.out.println("Azt veszem téglalapnak, amikor x és y mérete is legalább 2.");
int[] yellowOrigo = {0, 0};
boolean rectangleFound = false;
Pixel yellow = new Pixel(255, 255, 0);
for (int i = 0; i < picWithBorder.length; i++) {
for (int j = 0; j < picWithBorder[0].length; j++) {
if (picWithBorder[i][j].toString().equals(yellow.toString())) {
if (picWithBorder[i + 1][j].toString().equals(yellow.toString()) && j != picWithBorder[0].length - 1) {
if (picWithBorder[i][j + 1].toString().equals(yellow.toString()) &&
picWithBorder[i + 1][j + 1].toString().equals(yellow.toString())) {
rectangleFound = true;
yellowOrigo[0] = i;
yellowOrigo[1] = j;
break;
}
}
}
}
if (rectangleFound) {
break;
}
}
int[] yellowEnd = new int[2];
int i = yellowOrigo[0];
int j = yellowOrigo[1];
boolean foundOtherColor = false;
while (!foundOtherColor && i < picWithBorder.length) {
if (!(picWithBorder[i][j].toString().equals(yellow.toString()) &&
picWithBorder[i][j + 1].toString().equals(yellow.toString()))) {
yellowEnd[0] = i - 1;
foundOtherColor = true;
}
i++;
}
i = yellowEnd[0];
foundOtherColor = false;
while (!foundOtherColor && j < picWithBorder[0].length) {
if (!picWithBorder[i][j].toString().equals(yellow.toString())) {
yellowEnd[1] = j - 1;
foundOtherColor = true;
}
j++;
}
System.out.println("Kezd: " + yellowOrigo[0] + ", " + yellowOrigo[1]);
System.out.println("Vége: " + yellowEnd[0] + ", " + yellowEnd[1]);
System.out.println("Képpontok száma: " + ((yellowEnd[0] - yellowOrigo[0] + 1) *
(yellowEnd[1] - yellowOrigo[1] + 1)));
}
}