Skip to content
Snippets Groups Projects
Commit 49a85964 authored by Dabjo0585's avatar Dabjo0585
Browse files

ferdig

parent e3884a0e
No related branches found
No related tags found
No related merge requests found
package no.uib.inf101.colorgrid;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
public class ColorGrid {
// TODO: Implement this class
public class ColorGrid implements IColorGrid{
private int cols;
private int rows;
private Color[][] grid;
public ColorGrid(int rows, int cols){
this.rows = rows;
this.cols = cols;
this.grid = new Color[rows][cols];
}
@Override
public Color get(CellPosition pos) {
checkBounds(pos);
return grid[pos.row()][pos.col()];
}
@Override
public void set(CellPosition pos, Color color) {
checkBounds(pos);
grid[pos.row()][pos.col()] = color;
}
@Override
public int rows() {
return rows;
}
@Override
public int cols() {
return cols;
}
@Override
public List<CellColor> getCells() {
List<CellColor> cells = new ArrayList<>();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
CellPosition pos = new CellPosition(row, col);
Color color = grid[row][col];
cells.add(new CellColor(pos, color));
}
}
return cells;
}
private void checkBounds(CellPosition pos) {
if (pos.row() < 0 || pos.row() >= rows || pos.col() < 0 || pos.col() >= cols) {
throw new IndexOutOfBoundsException("Position out of bounds: " + pos);
}
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ public final class TextQuestions {
* How many methods does a class implementing IColorGrid need to
* implement at minimum?
*/
static final Integer a1 = null;
static final Integer a1 = 5;
/**
* Consider the interface IColorGrid, and notice how it extends the
......@@ -18,7 +18,7 @@ public final class TextQuestions {
* true or false: If an object has the type IColorGrid, it must
* also have the type CellColorCollection.
*/
static final Boolean a2 = null;
static final Boolean a2 = true;
/**
* Consider the interface IColorGrid, and notice how it extends the
......@@ -27,7 +27,7 @@ public final class TextQuestions {
* true or false: If an object has the type CellColorCollection, it
* must also have the type IColorGrid.
*/
static final Boolean a3 = null;
static final Boolean a3 = false;
/**
* Consider the interface IColorGrid, and notice how it extends the
......@@ -36,7 +36,7 @@ public final class TextQuestions {
* true or false: If an object has the type GridDimension, it
* must also have the type CellColorCollection.
*/
static final Boolean a4 = null;
static final Boolean a4 = false;
/**
* Consider the interface IColorGrid, and notice how it extends the
......@@ -45,7 +45,7 @@ public final class TextQuestions {
* true or false: for an object to have the type IColorGrid, it must
* belong to a class that implements the interface IColorGrid.
*/
static final Boolean a5 = null;
static final Boolean a5 = true;
/**
* Consider the interface IColorGrid, and notice how it extends the
......@@ -54,5 +54,5 @@ public final class TextQuestions {
* true or false: for a class to inherit the type GridDimension, it is
* sufficient to implements IColorGrid.
*/
static final Boolean a6 = null;
static final Boolean a6 = true;
}
package no.uib.inf101.gridview;
import java.awt.geom.Rectangle2D;
import no.uib.inf101.colorgrid.CellPosition;
import no.uib.inf101.colorgrid.GridDimension;
public class CellPositionToPixelConverter {
// TODO: Implement this class
private Rectangle2D box;
private GridDimension gd;
private double margin;
public CellPositionToPixelConverter(Rectangle2D box, GridDimension gd, double margin){
this.box = box;
this.gd = gd;
this.margin = margin;
}
public Rectangle2D getBoundsForCell(CellPosition cp){
int row = cp.row();
int col = cp.col();
double cellWidth = (box.getWidth() - (gd.cols() + 1) * margin)/ gd.cols();
double cellHeight = (box.getHeight() - (gd.rows() + 1) * margin) / gd.rows();
double x = box.getX() + margin + col * (cellWidth + margin);
double y = box.getY() + margin + row * (cellHeight + margin);
return new Rectangle2D.Double(x, y, cellWidth, cellHeight);
}
}
package no.uib.inf101.gridview;
import javax.swing.JPanel;
public class GridView {
// TODO: Implement this class
}
import no.uib.inf101.colorgrid.CellColor;
import no.uib.inf101.colorgrid.CellColorCollection;
import no.uib.inf101.colorgrid.CellPosition;
import no.uib.inf101.colorgrid.IColorGrid;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import javax.swing.JPanel;
public class GridView extends JPanel {
private IColorGrid colorGrid;
private static final double OUTERMARGIN = 30;
private static final Color MARGINCOLOR = Color.LIGHT_GRAY;
public GridView(IColorGrid colorGrid){
this.colorGrid = colorGrid;
this.setPreferredSize(new Dimension(400, 300));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawGrid(g2);
}
private void drawGrid(Graphics2D G2D) {
double x = OUTERMARGIN;
double y = OUTERMARGIN;
double width = this.getWidth() - 2 * OUTERMARGIN;
double height = this.getHeight() - 2 * OUTERMARGIN;
Rectangle2D rectangle2d = new Rectangle2D.Double(x, y, width, height);
G2D.setColor(MARGINCOLOR);
G2D.fill(rectangle2d);
CellPositionToPixelConverter cellConverter = new CellPositionToPixelConverter(getBounds(), colorGrid, OUTERMARGIN);
drawCells(G2D, colorGrid,cellConverter);
}
private static void drawCells(Graphics2D G2D, CellColorCollection colorCollection, CellPositionToPixelConverter cellConverter) {
List<CellColor> cells = colorCollection.getCells();
for (CellColor cellColor : cells) {
CellPosition cellPosition = cellColor.cellPosition();
Color color;
if (cellColor.color() != null) {
color = cellColor.color();
} else {
color = Color.DARK_GRAY;
}
G2D.setColor(color);
G2D.fill(cellConverter.getBoundsForCell(cellPosition));
}
}
}
package no.uib.inf101.gridview;
import javax.swing.JFrame;
import no.uib.inf101.colorgrid.CellPosition;
import no.uib.inf101.colorgrid.ColorGrid;
import no.uib.inf101.colorgrid.IColorGrid;
import no.uib.inf101.gridview.GridView;
import java.awt.Color;
public class Main {
public static void main(String[] args) {
// TODO: Implement this method
public static void main(String[] args) {
ColorGrid colorGrid = new ColorGrid(3, 4);
GridView gridView = new GridView(colorGrid);
JFrame frame = new JFrame();
frame.setContentPane(gridView);
frame.setTitle("INF101");
frame.setContentPane(gridView);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
colorGrid.set(new CellPosition(0, 0), Color.RED);
colorGrid.set(new CellPosition(0, 3), Color.BLUE);
colorGrid.set(new CellPosition(2, 0), Color.YELLOW);
colorGrid.set(new CellPosition(2, 3), Color.GREEN);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment