diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..3e75efefb826175b4b86b7b95ecaf7529ebb5b9e --- /dev/null +++ b/.gitignore @@ -0,0 +1,347 @@ +mvnw +mvnw.cmd + +# Created by https://www.toptal.com/developers/gitignore/api/eclipse,maven,java,intellij,visualstudiocode,macos,windows +# Edit at https://www.toptal.com/developers/gitignore?templates=eclipse,maven,java,intellij,visualstudiocode,macos,windows + +### Eclipse ### +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ +.apt_generated_test/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +# Uncomment this line if you wish to ignore the project description file. +# Typically, this file would be tracked if it contains build/dependency configurations: +#.project + +### Eclipse Patch ### +# Spring Boot Tooling +.sts4-cache/ + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +# Azure Toolkit for IntelliJ plugin +# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij +.idea/**/azureSettings.xml + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### macOS Patch ### +# iCloud generated files +*.icloud + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/eclipse,maven,java,intellij,visualstudiocode,macos,windows + + +target/ +.mvn/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 5fd323287f7f844404821f575faad5f415becbeb..7a78bc90c8bbe9f030ab20f0eaf5655d6679f4eb 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@  -I denne lab'en skal vi tegne et rutenett som vist over. Oppgaven består i hovedsak av to deler: +I denne lab'en skal vi tegne et rutenett som vist over. Oppgaven består i hovedsak av tre deler: 1. Skriv en klasse `ColorGrid` som representerer et rutenett av farger. 2. Skriv en klasse `CellPositionToPixelConverter` som har en metode som regner ut piksel-koordinatene for en gitt rute. 2. Skriv en klasse `GridView` som kan tegne et rutenett av farger. @@ -39,11 +39,15 @@ I denne oppgaven skal du lage klassen `ColorGrid` ([link](./src/main/java/no/uib Du kan leke deg litt i `Main::main` for å sjekke for deg selv at klassen fungerer som du forventer. For eksempel: ```java +// Opprett et rutenett med 3 rader og 4 kolonner IColorGrid grid = new ColorGrid(3, 4); System.out.println(grid.rows()); // forventer 3 System.out.println(grid.cols()); // forventer 4 + +// Sjekk at standard-verdien er null System.out.println(grid.get(new CellPosition(1, 2))); // forventer null - + +// Sjekk at vi kan endre verdien på en gitt posisjon grid.set(new CellPosition(1, 2), Color.RED); System.out.println(grid.get(new CellPosition(1, 2))); // forventer rød System.out.println(grid.get(new CellPosition(2, 1))); // forventer null @@ -83,14 +87,16 @@ I `Main::main` skal vi nå opprette et rutenett, og gi det til `GridView`-konstr * Grønn i hjørnet nede til høyre (posisjon (2, 3)) * [ ] I `GridView`, legg til en parameter av typen `IColorGrid` i konstruktøren, og legg også til en instansvariabel av samme type. Initialiser feltvariabelen med argumentet gitt til konstruktøren. -For å tegne rutenettet gjenstår det å endre på *paintComponent* -metoden. Vi skal benytte oss av tre hjelpemetoder for å tegne rutenettet: +For å tegne rutenettet gjenstår det å endre på *paintComponent* -metoden. Vi skal benytte oss av tre hjelpemetoder for å tegne rutenettet. I *paintComponent* gjør vi et kall til -* `drawGrid` har som ansvar å tegne et fullstendig rutenett, inkludert alle rammer og ruter (alt innenfor det grå området i illustrasjonen). For å tegne selve rutene, kaller denne metoden på +* `drawGrid`, som har som ansvar å tegne et fullstendig rutenett, inkludert alle rammer og ruter (alt innenfor det grå området i illustrasjonen). For å tegne selve rutene, kaller denne metoden på * `drawCells`, som har som ansvar å tegne en samling av ruter. For hver rute regner denne metode ut hvor ruten skal være ved å kalle på hjelpemetoden * `getBoundsForCell` som vet hvordan å regne ut posisjonen til én rute i rutenettet. Det viser seg at det er den sistnevnte metoden som er mest komplisert. Vi kunne hatt `getBoundForCell` som en vanlig hjelpemetode, men siden dette er en relativt isolert operasjon som vi ønsker å kunne teste separat, oppretter vi en en klasse for denne hjelpemetoden: *CellPositionToPixelConverter*. +> Måten vi tenker på når vi skal utvikle et program er «top-down» -- man begynner med å dele opp oppgaven i store steg, og så drømmer vi opp hjelpemetoder vi trenger før disse hjelpemetodene faktisk eksisterer. Når vi faktisk koder, er det ofte lettest å gjøre det «bottom up», slik at vi kan teste hver enkelt byggeklosse/metode mens vi holder på. Dette for å si: hvilken rekkefølge du løser resten av oppgaven er opp til deg. Det kan være at det er lettere å hoppe litt frem og tilbake mellom de neste avsnittene. + ## CellPositionToPixelConverter diff --git a/src/main/java/no/uib/inf101/colorgrid/CellColor.java b/src/main/java/no/uib/inf101/colorgrid/CellColor.java index 39e5c69e010f40af6a2803f79d06d2d16fbcefb3..a15b50c7643afca77620329138ef39e51f0b9270 100644 --- a/src/main/java/no/uib/inf101/colorgrid/CellColor.java +++ b/src/main/java/no/uib/inf101/colorgrid/CellColor.java @@ -1,5 +1,7 @@ package no.uib.inf101.colorgrid; +// Les om records her: https://inf101.ii.uib.no/notat/mutabilitet/#record + import java.awt.Color; /** diff --git a/src/main/java/no/uib/inf101/colorgrid/CellPosition.java b/src/main/java/no/uib/inf101/colorgrid/CellPosition.java index 6a7ecba96590353f621bc20c420109c8ed386389..6e82b437e3205f0b47a05ce18fba17fb75139412 100644 --- a/src/main/java/no/uib/inf101/colorgrid/CellPosition.java +++ b/src/main/java/no/uib/inf101/colorgrid/CellPosition.java @@ -1,5 +1,7 @@ package no.uib.inf101.colorgrid; +// Les om records her: https://inf101.ii.uib.no/notat/mutabilitet/#record + /** * A CellPosition consists of a row and a column. * diff --git a/src/test/java/no/uib/inf101/colorgrid/TestTextQuestions.java b/src/test/java/no/uib/inf101/colorgrid/TestTextQuestions.java index ee7da3ed50a5010730c449d1cf347c0c307e189a..94efb96fd277739ad3036ba90d720087fd2184ee 100644 --- a/src/test/java/no/uib/inf101/colorgrid/TestTextQuestions.java +++ b/src/test/java/no/uib/inf101/colorgrid/TestTextQuestions.java @@ -28,7 +28,7 @@ public class TestTextQuestions { @Test public void q3() { assertEquals(false, TextQuestions.a3, - "It is possible to create a class which implements" + + "It is possible to create a class which implements " + "CellColorCollection directly, without implementing IColorGrid. " + "Objects in such a class would have the type GridDimension, " + "but not the type IColorGrid." @@ -38,8 +38,8 @@ public class TestTextQuestions { @Test public void q4() { assertEquals(false, TextQuestions.a4, - "It is possible to create a class which implements" + - "GridDimension directly, without implementing IColorGrid or" + + "It is possible to create a class which implements " + + "GridDimension directly, without implementing IColorGrid or " + "CellColorCollection. Objects in such a class would have the " + "type GridDimension, but not the type CellColorCollection." ); @@ -48,9 +48,9 @@ public class TestTextQuestions { @Test public void q5() { assertEquals(true, TextQuestions.a5, - "If an object has the type IColorGrid, it must indeed" + - "belong to a class that implements the interface IColorGrid." + - "If the class does not do so directly, it must do so" + + "If an object has the type IColorGrid, it must indeed " + + "belong to a class that implements the interface IColorGrid. " + + "If the class does not do so directly, it must do so " + "indirectly through inheritance." ); } diff --git a/src/test/java/no/uib/inf101/gridview/TestCellPositionToPixelConverter.java b/src/test/java/no/uib/inf101/gridview/TestCellPositionToPixelConverter.java index b94925d50f747641ccc2c996d5b680190ebc878a..2b2fa14a01444c8541851efd62465fbcef4c1cdc 100644 --- a/src/test/java/no/uib/inf101/gridview/TestCellPositionToPixelConverter.java +++ b/src/test/java/no/uib/inf101/gridview/TestCellPositionToPixelConverter.java @@ -27,7 +27,8 @@ public class TestCellPositionToPixelConverter { "Y--G") ); CellPositionToPixelConverter converter = getConverter( - grid, new Rectangle2D.Double(30, 30, 340, 240), 30); + new Rectangle2D.Double(30, 30, 340, 240), grid,30 + ); Rectangle2D expected = new Rectangle2D.Double(215, 130, 47.5, 40); assertEquals(expected, getBoundsForCell(converter, new CellPosition(1, 2))); @@ -57,21 +58,21 @@ public class TestCellPositionToPixelConverter { } - static CellPositionToPixelConverter getConverter(GridDimension grid, Rectangle2D rect, double margin) { + static CellPositionToPixelConverter getConverter(Rectangle2D box, GridDimension gd, double margin) { try { Constructor<?> constructor = CellPositionToPixelConverter.class.getConstructor( - GridDimension.class, Rectangle2D.class, double.class + Rectangle2D.class, GridDimension.class, double.class ); // Check that the constructor is public assertTrue(Modifier.isPublic(constructor.getModifiers()), - "The constructor CellPositionToPixelConverter(IColorGrid, Rectangle2D, double)" + "The constructor CellPositionToPixelConverter(Rectangle2D, GridDimension, double)" + " should be public"); // Create a new object using the constructor and return it - return (CellPositionToPixelConverter) constructor.newInstance(grid, rect, margin); + return (CellPositionToPixelConverter) constructor.newInstance(box, gd, margin); } catch (NoSuchMethodException e) { - fail("Could not find the constructor CellPositionToPixelConverter(IColorGrid, Rectangle2D, " + + fail("Could not find the constructor CellPositionToPixelConverter(Rectangle2D, GridDimension, " + "double) in the CellPositionToPixelConverter class"); } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); diff --git a/src/test/java/no/uib/inf101/gridview/TestGridView.java b/src/test/java/no/uib/inf101/gridview/TestGridView.java index d1d938912cb766bf1a1f70cac630e5270599e1f3..81abfc19d447b3587eadbc48361ed26cb6e5ba55 100644 --- a/src/test/java/no/uib/inf101/gridview/TestGridView.java +++ b/src/test/java/no/uib/inf101/gridview/TestGridView.java @@ -116,7 +116,7 @@ public class TestGridView { try { Constructor<?> constructor = GridView.class.getConstructor(IColorGrid.class); - // Check that the constructor is public + // Check that the constructor is not private assertFalse(Modifier.isPrivate(constructor.getModifiers()), "The constructor GridView(IColorGrid) should not be private"); @@ -167,7 +167,7 @@ public class TestGridView { RecordGraphics2D g2 = new RecordGraphics2D(); // Invoke the method - drawCell.invoke(null, g2, grid, TestCellPositionToPixelConverter.getConverter(grid, rect, margin)); + drawCell.invoke(null, g2, grid, TestCellPositionToPixelConverter.getConverter(rect, grid, margin)); return g2; } catch (NoSuchMethodException e) { fail("Could not find the method drawCells(Graphics2D, CellColorCollection,"