您好我正在创建一个程序,在Java中的青色背景上创建一个多闪存块。我正在使用JFrames和Canvas,但由于某种原因,即使我指定颜色为青色,它也会使我的背景变黑。但是,如果你移动盒子,如果块用keylistener填充空间中的青色。以下是我认为与问题相关的代码。任何帮助都将非常感谢并感谢您阅读本文。(请注意,这可能是错误的语法和编码,但我的理论是在使代码工作之前使代码工作。)
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
// Create off-screen drawing surface
BufferedImage bi = gc.createCompatibleImage( 640, 480 );
// Objects needed for rendering...
Graphics graphics = null;
Graphics2D g2d = null;
//Why isn't the background color cyan? (It's black)
Color background = Color.CYAN;
Random rand = new Random();
//The x and y positions are randomly generated as are the length and width
int x = rand.nextInt(640 / 2), y = rand.nextInt(640 / 2);
int w = rand.nextInt( 640/2 );
int h = rand.nextInt( 480/2 );
//To Do: Infinite loops suck, make a death variable
while( true ) {
try {
// clear back buffer...
g2d = bi.createGraphics();
g2d.setColor(Color.CYAN);
g2d.fillRect( -1000, 1000, 1000, 1000 );
// draw the rectangle...
int r = rand.nextInt(256);
int g = rand.nextInt(256);
int b = rand.nextInt(256);
g2d.setColor(new Color(r, g, b));
g2d.fillRect( x, y, w, h );
//Get the newly drawn rectangles and flip
graphics = buffer.getDrawGraphics();
graphics.drawImage(bi, x, y, canvas);
答案 0 :(得分:0)
我不确定我完全明白你要做什么。如果你试图让JFrame
青色用帆布着色,这就是我所做的:
import java.awt.Canvas;
import java.awt.Color;
import javax.swing.JFrame;
public class test {
static JFrame frame;
static Canvas canvas;
public static void main(String[] args){
frame = new JFrame();
canvas = new Canvas();
canvas.setBackground(Color.cyan);
frame.getContentPane().add(canvas);
frame.setVisible(true);
}
}
我很确定实际上并不是你想要的。