Drawing in Java using Canvas

134,648

Solution 1

Suggestions:

  • Don't use Canvas as you shouldn't mix AWT with Swing components unnecessarily.
  • Instead use a JPanel or JComponent.
  • Don't get your Graphics object by calling getGraphics() on a component as the Graphics object obtained will be transient.
  • Draw in the JPanel's paintComponent() method.
  • All this is well explained in several tutorials that are easily found. Why not read them first before trying to guess at this stuff?

Key tutorial links:

Solution 2

You've got to override your Canvas's paint(Graphics g) method and perform your drawing there. See the paint() documentation.

As it states, the default operation is to clear the canvas, so your call to the canvas' graphics object doesn't perform as you would expect.

Solution 3

Why would the first way not work. Canvas object is created and the size is set and the grahpics are set. I always find this strange. Also if a class extends JComponent you can override the

paintComponent(){
  super...
}

and then shouldn't you be able to create and instance of the class inside of another class and then just call NewlycreateinstanceOfAnyClass.repaint();

I have tried this approach for some game programming I have been working and it doesn't seem to work the way I think that it should be.

Doug Hauf

Solution 4

The following should work:

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();
    }
}
Share:
134,648
kazinix
Author by

kazinix

Updated on March 21, 2020

Comments

  • kazinix
    kazinix about 4 years

    I want to draw in Java's Canvas but can't get it work because I don't know what I'm doing. Here's my simple code:

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

    Nothing appears on the window.

    Am I wrong to think that Canvas is a paper and Graphics is my Pencil? Is that how it works?