使用画布在Java中绘图

使用画布在Java中绘图,java,swing,graphics,canvas,awt,Java,Swing,Graphics,Canvas,Awt,我想在Java的画布上画画,但无法让它工作,因为我不知道自己在做什么。以下是我的简单代码: import javax.swing.JFrame; import java.awt.Canvas; import java.awt.Graphics; import java.awt.Color; public class Program { public static void main(String[] args) { JFrame frmMain = new JF

我想在Java的画布上画画,但无法让它工作,因为我不知道自己在做什么。以下是我的简单代码:

import javax.swing.JFrame;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;

public class Program
{
    public static void main(String[] args)
    {
        JFrame frmMain = new JFrame();
        frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmMain.setSize(400, 400);

        Canvas cnvs = new Canvas();
        cnvs.setSize(400, 400);

        frmMain.add(cnvs);
        frmMain.setVisible(true);

        Graphics g = cnvs.getGraphics();
        g.setColor(new Color(255, 0, 0));
        g.drawString("Hello", 200, 200);
    }
}
窗口上没有显示任何内容


我认为画布是纸,图形是我的铅笔,这是错的吗?这就是它的工作原理吗?

您必须覆盖画布的
绘制(Graphics g)
方法并在那里进行绘制。看

如前所述,默认操作是清除画布,因此对画布的图形对象的调用不会像预期的那样执行。

建议:

  • 不要使用画布,因为您不应该不必要地将AWT与Swing组件混合使用
  • 而是使用JPanel或JComponent
  • 不要通过调用组件上的
    getGraphics()
    来获取图形对象,因为获取的图形对象是瞬态的
  • 绘制JPanel的
    paintComponent()
    方法
  • 所有这些都在一些容易找到的教程中得到了很好的解释。为什么不先读一读,然后再去猜呢

主要教程链接:

  • 基础教程:
  • 更多高级信息:

    • 为什么第一种方法不起作用。画布对象已创建,大小已设置,grahpics已设置。我总是觉得这很奇怪。此外,如果类扩展了JComponent,则可以重写

      paintComponent(){
        super...
      }
      
      然后,您是否应该能够在另一个类中创建该类的实例,然后调用
      NewlycreateinstanceOfAnyClass.repaint()

      我已经在一些游戏编程中尝试过这种方法,但它似乎没有按照我认为应该的方式工作


      道格·豪夫(Doug Hauf)

      以下方法应该有效:

      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();
          }
      }
      

      你只是想画图吗?谢谢!我确实在网上搜索过。是的,它们很容易找到,但不容易理解。我希望我能放弃对每一点的投票,但似乎不使用画布来挥杆在一开始就有很大的影响力。很好的观点。但请记住,人们在搜索这个主题时也会找到你的答案。这就是为什么我认为你应该包括到你提到的教程的链接。你得到了我想要在Java的画布上绘制的优势的投票,但无法让它工作,因为我不知道我在做什么。单独地