问题描述
我在我的程序中创建了一个网格.下面是用于创建网格的代码.
I have created a grid vpn下载 free vpn 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 vpn下载 void paint(Graphics g) vpn free {
免费vpn下载 g.drawRect (10, 10, 免费vpn下载 800, 500);
免费vpn for (int i = 10; i <= 800; i+= 10)
免费vpn vpn下载 vpn free g.drawLine (i, 免费vpn 10, i, 510);
free vpn for (int i = 10; i <= vpn free 500; i+= 10)
免费vpn g.drawLine vpn free (10, i, 810, i);
free vpn 免费vpn }
}
public class CoreControl {
public static void 免费vpn下载 main(String[] a) {
JFrame window = new JFrame();
vpn下载 window.setSize(840,560);
free vpn free vpn window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(new Grid());
免费vpn下载 vpn free window.setVisible(true);
}
}
我想做的是创建一个函数,该函数将根据我给它的坐标绘制一个矩形(用黑色填充).基本上我想用黑色填充网格的某些单元格,我的想法是在单元格坐标上绘制黑色填充矩形.如何实现这个功能?
What I want to do is to create a function which will draw a free vpn rectangle (filled with black color) based on the vpn下载 coordinates that I give it. Basically I want to 免费vpn populate certain cells of the grid with black color and my idea is to draw black filled rectangles 免费vpn下载 on the cell coordinates. How do I make this function?
我尝试创建另一个名为 drawRectangle 的类并在主函数中调用它,例如 window.getContentPane().add(new drawRectangle());但这不起作用(只显示 drawRectangle 而不是网格).
I tried making another vpn下载 class called drawRectangle vpn 免费vpn free and called vpn free 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.
如何创建这个函数?
另外,如果您知道我应该构建这个程序的更好方法,请告诉我(我是 Java vpn free 新手,所以我愿意接受任何建议).
Also if you know a better way that I should structure this program please 免费vpn let me know (I am new to Java so I am open vpn 免费vpn free to any suggestions).
推荐答案
- 不要使用
paint,使用paintComponent并且不要忘记调用super.paintComponent JComponent可能不是最佳选择,JPanel可能是更好的选择Graphics#fillRect(int, int, int, int)?
- Don't use
paint, usepaintComponentand don't forget to callsuper.paintComponent JComponentmay vpn下载 not be the best choice,JPanelis probably vpn下载 a better choice- What's wrong 免费vpn with
Graphics#fillRect(int, int, int, int)?
您可以看看 Performing Custom Painting 和 2D 图形了解更多详情.
You might to take a look at Performing Custom Painting and 2D Graphics for vpn下载 more details.
我建议不要尝试使用第二个组件来执行填充.只需在网格类中提供一个方法,该方法提供单元格的 x/y 免费vpn 位置(以网格形式)并在 vpn下载 paintComponent 方法中填充单元格
I'd vpn free advice against trying to have a second component to performing the filling. Simply provide a method 免费vpn in you grid class that supplies the vpn free cell's x/y position (in grid terms) free vpn and fill the cell within the paintComponent method
更新示例
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import 免费vpn下载 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> 免费vpn fillCells;
public Grid() {
免费vpn vpn下载 fillCells = new ArrayList<>(25);
}
vpn free @Override
protected 免费vpn void paintComponent(Graphics g) {
免费vpn下载 super.paintComponent(g);
vpn下载 vpn下载 vpn free for (Point fillCell : fillCells) {
free vpn int cellX = 10 + (fillCell.x * 10);
vpn下载 int 免费vpn cellY = 10 + (fillCell.y * 10);
免费vpn g.setColor(Color.RED);
g.fillRect(cellX, cellY, 10, 10);
}
free vpn g.setColor(Color.BLACK);
g.drawRect(10, 10, 800, 500);
免费vpn 免费vpn for vpn下载 (int i 免费vpn下载 = 10; i vpn free <= 800; i += 10) {
g.drawLine(i, free vpn 10, i, 510);
}
免费vpn 免费vpn vpn free for (int i vpn free = 免费vpn 10; i <= 500; i += 10) {
g.drawLine(10, vpn下载 i, 810, i);
}
}
public void fillCell(int x, int y) {
free vpn free vpn fillCells.add(new Point(x, vpn free y));
vpn下载 vpn free free vpn repaint();
免费vpn }
}
public static void main(String[] a) {
vpn free EventQueue.invokeLater(new Runnable() {
vpn下载 @Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
免费vpn下载 vpn下载 Grid grid vpn vpn下载 free vpn下载 = new Grid();
JFrame 免费vpn window = new JFrame();
免费vpn下载 vpn下载 window.setSize(840, 560);
vpn free 免费vpn下载 window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(grid);
vpn下载 免费vpn下载 window.setVisible(true);
vpn下载 vpn free grid.fillCell(0, 0);
vpn free 免费vpn grid.fillCell(79, 0);
免费vpn下载 vpn下载 vpn free grid.fillCell(0, 49);
免费vpn vpn下载 grid.fillCell(79, 49);
grid.fillCell(39, vpn free 24);
vpn下载 vpn free }
免费vpn });
}
}
这篇关于在Java中为网格创建绘制矩形(填充黑色)函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账免费vpn织梦模板(带手机端)
成人高考自考在职研究生教育机构免费vpn源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)