j*****n 发帖数: 943 | 1 程序如下,
我在paintComponent中对bImage:BufferedImage 设置颜色,但是无论设置成什么颜色,
画出来的都是白颜色。请高手点解。
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
class Player extends JPanel {
Image bImage = null;
public Player() {
setSize(800, 600);
bImage = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
bImage.getGraphics().setColor(Color.RED);
bImage.ge | F****n 发帖数: 3271 | 2 This is because each time getGraphics() is invoked, it call createGraphics
which creates a new Graphics2D object, so the Graphics you used to draw Line
is not the Graphics you have set Color. You should Use:
Graphics2D g2D = bImage.createGraphics();
g2D.setColor(Color.red);
g2D.draw(...);
,
【在 j*****n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 程序如下, : 我在paintComponent中对bImage:BufferedImage 设置颜色,但是无论设置成什么颜色, : 画出来的都是白颜色。请高手点解。 : import javax.swing.*; : import java.awt.*; : import java.awt.image.*; : import java.awt.event.*; : class Player extends JPanel { : Image bImage = null; : public Player() {
|
|