Creating a draw rectangle (filled with black color) function in Java for a grid
- Don't use
paint
, usepaintComponent
and don't forget to callsuper.paintComponent
-
JComponent
may not be the best choice,JPanel
is probably a better choice - What's wrong with
Graphics#fillRect(int, int, int, int)
?
You might to take a look at Performing Custom Painting and 2D Graphics for more details.
I'd advice against trying to have a second component to performing the filling. Simply provide a method in you grid class that supplies the cell's x/y position (in grid terms) and fill the cell within the paintComponent
method
Updated with example
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class CoreControl {
public static class Grid extends JPanel {
private List<Point> fillCells;
public Grid() {
fillCells = new ArrayList<>(25);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Point fillCell : fillCells) {
int cellX = 10 + (fillCell.x * 10);
int cellY = 10 + (fillCell.y * 10);
g.setColor(Color.RED);
g.fillRect(cellX, cellY, 10, 10);
}
g.setColor(Color.BLACK);
g.drawRect(10, 10, 800, 500);
for (int i = 10; i <= 800; i += 10) {
g.drawLine(i, 10, i, 510);
}
for (int i = 10; i <= 500; i += 10) {
g.drawLine(10, i, 810, i);
}
}
public void fillCell(int x, int y) {
fillCells.add(new Point(x, y));
repaint();
}
}
public static void main(String[] a) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
Grid grid = new Grid();
JFrame window = new JFrame();
window.setSize(840, 560);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(grid);
window.setVisible(true);
grid.fillCell(0, 0);
grid.fillCell(79, 0);
grid.fillCell(0, 49);
grid.fillCell(79, 49);
grid.fillCell(39, 24);
}
});
}
}
Veda Sadhak
Updated on April 08, 2020Comments
-
Veda Sadhak about 4 years
I have created a grid in my program. Below is the code used to create the grid.
import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.JFrame; class Grid extends JComponent { public void paint(Graphics g) { g.drawRect (10, 10, 800, 500); for (int i = 10; i <= 800; i+= 10) g.drawLine (i, 10, i, 510); for (int i = 10; i <= 500; i+= 10) g.drawLine (10, i, 810, i); } } public class CoreControl { public static void main(String[] a) { JFrame window = new JFrame(); window.setSize(840,560); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().add(new Grid()); window.setVisible(true); } }
What I want to do is to create a function which will draw a rectangle (filled with black color) based on the coordinates that I give it. Basically I want to populate certain cells of the grid with black color and my idea is to draw black filled rectangles on the cell coordinates. How do I make this function?
I tried making another class called drawRectangle and called it in the main function like so window.getContentPane().add(new drawRectangle()); however that did not work (only drawRectangle shows up and not the grid).
I also want to be able to use this function repeatedly to keep creating rectangles.
How do I do create this function?
Also if you know a better way that I should structure this program please let me know (I am new to Java so I am open to any suggestions).
-
Veda Sadhak about 11 yearsThis might sound stupid but w/e i'm new to Java. How do I use Graphics.fillRect(int,int.int,int)? Right now this is how to would like to use it: function drawRectangle(int x1, int y1, int x2, int y2) { Graphics.fillRect(int,int.int,int) } is this possible? Does the program automatically add the rectangle to the frame?
-
MadProgrammer about 11 yearsNo.
fillRect
is a method ofGraphics
, you need a reference to theGraphics
context. All painting should be done within the context of apaint
method (preferablypaintComponent
). In yourGrid
class I would provide method called something likefillGrid(int x, int y)
which tells you what cell to fill. In yourpaintComponent
method, I would then determine the rectangle bounds of this cell and fill usingfillRect
(ps I updated the answer ;)) -
Veda Sadhak about 11 yearsOk so just to test out the fillRect I have added public void paint(Graphics g, int x, int y) { g.fillRect(x, y, 10, 10); } in the Grid Class. How do I use this function?
-
Veda Sadhak about 11 yearsAwesome. Thanks, I really appreciate it.
-
Veda Sadhak about 11 yearsYour answer already solved my question. However, just for learning purposes could you explain the line for (Point fillCell : fillCells) { i'm not familiar with the syntax. Also could you explain private List<Point> fillCells;.
-
MadProgrammer about 11 yearsWell,basically, my example allows you to set 0 or more cells to be filled. In order to track what cells are filled, I use a List, which is simply a sequential list of objetcs (in this case of Point objects). Take a look at Colleations for more details. The for loop is a shortcutted version of a for-next loop know as a for-each loop. Take a look at this for more info