我在我的程序中创建了一个网格。下面是用于创建网格的代码。
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);
}
}
我想做的是创建一个函数,该函数将根据我给它的坐标绘制一个矩形(填充为黑色)。基本上我想用黑色填充网格的某些单元格,我的想法是在单元格坐标上绘制黑色填充的矩形。如何实现这个功能?
我尝试制作另一个名为 drawRectangle 的类,并在主函数中调用它,就像 window.getContentPane().add(new drawRectangle());然而,这不起作用(只显示 drawRectangle 而不是网格)。
我也希望能够重复使用这个函数来不断地创建矩形。
我该如何创建这个函数?
此外,如果您知道构建此程序的更好方法,请告诉我(我是 Java 的新手,所以我愿意接受任何建议)。
最佳答案
paint,使用paintComponent并且不要忘记调用super.paintComponentJComponent 可能不是最佳选择,JPanel 可能是更好的选择Graphics#fillRect(int, int, int, int) 怎么了? ?你可以看看Performing Custom Painting和 2D Graphics了解更多详情。
我建议不要尝试使用第二个组件来执行填充。只需在您的网格类中提供一个方法来提供单元格的 x/y 位置(以网格形式)并在 paintComponent 方法
更新了例子
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);
}
});
}
}
关于java - 在 Java 中为网格创建绘制矩形(填充为黑色)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15870608/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了