当前位置:   article > 正文

java画布canvas_使用Canvas在Java中绘图

java canvas

以下应该有效:

public static void main(String[] args)

{

final String title = "Test Window";

final int width = 1200;

final int height = width / 16 * 9;

//Creating the frame.

JFrame frame = new JFrame(title);

frame.setSize(width, height);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationRelativeTo(null);

frame.setResizable(false);

frame.setVisible(true);

//Creating the canvas.

Canvas canvas = new Canvas();

canvas.setSize(width, height);

canvas.setBackground(Color.BLACK);

canvas.setVisible(true);

canvas.setFocusable(false);

//Putting it all together.

frame.add(canvas);

canvas.createBufferStrategy(3);

boolean running = true;

BufferStrategy bufferStrategy;

Graphics graphics;

while (running) {

bufferStrategy = canvas.getBufferStrategy();

graphics = bufferStrategy.getDrawGraphics();

graphics.clearRect(0, 0, width, height);

graphics.setColor(Color.GREEN);

graphics.drawString("This is some text placed in the top left corner.", 5, 15);

bufferStrategy.show();

graphics.dispose();

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/67493
推荐阅读
相关标签
  

闽ICP备14008679号