英文:
RGB based random color setter for JPanel manipulation doesn't work, the color is always black
问题
public void randcol(){
Graphics g = xxx.getGraphics();
Random r = new Random();
int rand1 = r.nextInt(256);
int rand2 = r.nextInt(256);
int rand3 = r.nextInt(256);
g.setColor(new Color(rand1, rand2, rand3));
}
public void colorize(){
Graphics g = xxx.getGraphics();
randcol();
g.fillPolygon(tr1);
randcol();
g.fillPolygon(tr2);
randcol();
g.fillPolygon(tr3);
randcol();
g.fillPolygon(tr4);
randcol();
g.fillPolygon(tr5);
randcol();
g.fillPolygon(tr6);
randcol();
g.fillPolygon(tr7);
randcol();
g.fillPolygon(tr8);
}
英文:
I wrote code to generate random color using RGB and fill polygons with it, so that every polygon is different color using "util.Random". However, every time I run this code, all 8 polygons just get filled with black color. xxx is variable name for JPanel.
public void randcol(){
Graphics g = xxx.getGraphics();
Random r = new Random();
int rand1 = r.nextInt(256);
int rand2 = r.nextInt(256);
int rand3 = r.nextInt(256);
g.setColor(new Color(rand1, rand2, rand3));
}
public void colorize(){
Graphics g = xxx.getGraphics();
randcol();
g.fillPolygon(tr1);
randcol();
g.fillPolygon(tr2);
randcol();
g.fillPolygon(tr3);
randcol();
g.fillPolygon(tr4);
randcol();
g.fillPolygon(tr5);
randcol();
g.fillPolygon(tr6);
randcol();
g.fillPolygon(tr7);
randcol();
g.fillPolygon(tr8);
}
答案1
得分: 1
public void randcol(){
Graphics g = xxx.getGraphics();
...
}
public void colorize(){
Graphics g = xxx.getGraphics();
...
}
当你调用`getGraphics()`时,每次都会得到一个不同的Graphics对象实例。因此,在不同的方法中无法设置Graphics对象的颜色。
相反,你的`randcol()`方法应该返回一个Color对象。然后,`colorize()`方法将调用此方法,在绘制多边形之前设置Graphics对象的颜色。
然而,这仍然是错误的方法,因为Swing组件可以被多次绘制,每次重新绘制时,你当前的逻辑会再次随机化颜色。
因此,正确的解决方案是创建一个自定义对象,其中包含:
1. 要绘制的多边形
2. 多边形的颜色
这个对象应该在类开始时生成一次。然后,你将所有这些对象保存在一个ArrayList中,你的绘制方法将通过迭代ArrayList来绘制每个对象。
参考:[自定义绘图方法](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/),其中有这种方法的工作示例。
英文:
public void randcol(){
Graphics g = xxx.getGraphics();
...
}
public void colorize(){
Graphics g = xxx.getGraphics();
...
When you invoke getGraphics()
you get a different instance of the Graphics object each time. So you can't set the Color of the Graphics object in a different method.
Instead you randcol()
method should return a Color object. The colorize()
method will then invoke this method to set the color of the Graphics object before painting the polygon.
However this is still the wrong approach, because the Swing component can be painted multiple times and each time it is repainted your current logic will randomize the color again.
So the proper solution is to create a custom object that contains:
- the Polygon to be painted
- the Color of the Polygon.
This object should be generated once at the start of your class. Then you keep all these objects in an ArrayList and your painting method iterates through the ArrayList to paint each object.
See:Custom Painting Approaches for a working example of this approach.
答案2
得分: 0
你可以编写一个函数来生成随机颜色。
public Color getRandomColor(){
Random r = new Random();
int rand1 = r.nextInt(256);
int rand2 = r.nextInt(256);
int rand3 = r.nextInt(256);
return new Color(rand1, rand2, rand3);
}
public void colorize(){
Graphics g = xxx.getGraphics();
g.setColor(getRandomColor());
g.fillPolygon(tr1);
g.setColor(getRandomColor());
g.fillPolygon(tr2);
g.setColor(getRandomColor());
//...
}
英文:
You can write a function which gives you a random color
public Color getRandomColor(){
Random r = new Random();
int rand1 = r.nextInt(256);
int rand2 = r.nextInt(256);
int rand3 = r.nextInt(256);
return new Color(rand1, rand2, rand3);
}
public void colorize(){
Graphics g = xxx.getGraphics();
g.setColor(getRandomColor());
g.fillPolygon(tr1);
g.setColor(getRandomColor());
g.fillPolygon(tr2);
g.setColor(getRandomColor());
//...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论