Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
lab4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Malvika.Singh
lab4
Commits
7ba646b5
Commit
7ba646b5
authored
2 years ago
by
Malvika.Singh
Browse files
Options
Downloads
Patches
Plain Diff
Update src/main/java/no/uib/inf101/gridview/GridView.java
parent
5163017e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/no/uib/inf101/gridview/GridView.java
+69
-2
69 additions, 2 deletions
src/main/java/no/uib/inf101/gridview/GridView.java
with
69 additions
and
2 deletions
src/main/java/no/uib/inf101/gridview/GridView.java
+
69
−
2
View file @
7ba646b5
package
no.uib.inf101.gridview
;
public
class
GridView
{
// TODO: Implement this class
import
java.awt.Dimension
;
import
java.awt.Graphics
;
import
java.awt.Graphics2D
;
import
javax.swing.JPanel
;
import
no.uib.inf101.colorgrid.CellColor
;
import
no.uib.inf101.colorgrid.CellColorCollection
;
import
no.uib.inf101.colorgrid.IColorGrid
;
import
java.awt.geom.Rectangle2D
;
import
java.util.List
;
import
java.awt.Color
;
import
no.uib.inf101.colorgrid.CellPosition
;
public
class
GridView
extends
JPanel
{
//Field variables
IColorGrid
grid
;
private
static
final
double
OUTERMARGIN
=
30
;
private
static
final
Color
MARGINCOLOR
=
Color
.
LIGHT_GRAY
;
//Constructor
public
GridView
(
IColorGrid
grid
)
{
this
.
setPreferredSize
(
new
Dimension
(
400
,
300
));
this
.
grid
=
grid
;
}
@Override
public
void
paintComponent
(
Graphics
g
)
{
super
.
paintComponent
(
g
);
Graphics2D
g2
=
(
Graphics2D
)
g
;
drawGrid
(
g2
);
}
private
static
void
drawCells
(
Graphics2D
g
,
CellColorCollection
c
,
CellPositionToPixelConverter
cp
)
{
List
<
CellColor
>
cellList
=
c
.
getCells
();
for
(
CellColor
cell
:
cellList
)
{
Color
color
=
cell
.
color
();
CellPosition
pos
=
cell
.
cellPosition
();
if
(
color
==
null
)
{
color
=
Color
.
DARK_GRAY
;
}
Rectangle2D
g2
=
cp
.
getBoundsForCell
(
pos
);
g
.
setColor
(
color
);
g
.
fill
(
g2
);
}
}
public
void
drawGrid
(
Graphics2D
g
){
double
width
=
this
.
getWidth
()
-
2
*
OUTERMARGIN
;
double
height
=
this
.
getHeight
()
-
2
*
OUTERMARGIN
;
Rectangle2D
g2
=
new
Rectangle2D
.
Double
(
OUTERMARGIN
,
OUTERMARGIN
,
width
,
height
);
g
.
setColor
(
MARGINCOLOR
);
g
.
fill
(
g2
);
CellPositionToPixelConverter
cp
=
new
CellPositionToPixelConverter
(
g2
,
grid
,
OUTERMARGIN
);
drawCells
(
g
,
grid
,
cp
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment