基于RGB的随机颜色设置器用于JPanel操作不起作用,颜色始终为黑色。

huangapple go评论59阅读模式
英文:

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:

  1. the Polygon to be painted
  2. 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());
   //...
}

huangapple
  • 本文由 发表于 2020年10月10日 22:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64294710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定