Why is the image I painted on the canvas disappearing?

Asked

Viewed 57 times

-3

package Principal;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

public class Desenho {

    public static void main(String[] args){

        JanelaDesenho jd = new JanelaDesenho();
        jd.setVisible(true);
        desenhar();

    }

    public static void desenhar(){

        Canvas c = JanelaDesenho.getCanvas();
        Graphics g = c.getGraphics();
        c.setBackground(Color.WHITE);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 100, 100);

    }



}




package Principal;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Canvas;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;

public class JanelaDesenho extends JFrame {

    private JPanel contentPane;
    private static Canvas canvas;

    /*
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JanelaDesenho frame = new JanelaDesenho();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    */

    /**
     * Create the frame.
     */
    public JanelaDesenho() {
        setTitle("Digger");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 497, 505);
        contentPane = new JPanel();
        contentPane.setToolTipText("");
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        getCanvas().setBounds(0, 0, 481, 466);
        contentPane.add(getCanvas());       
    }   

    public static Canvas getCanvas(){

        if(canvas == null){

            canvas = new Canvas();

        }

        return canvas;

    }   
}




package Principal;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Canvas;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;

public class JanelaDesenho extends JFrame {

    private JPanel contentPane;
    private static Canvas canvas;

    /*
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JanelaDesenho frame = new JanelaDesenho();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    */

    /**
     * Create the frame.
     */
    public JanelaDesenho() {
        setTitle("Digger");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 497, 505);
        contentPane = new JPanel();
        contentPane.setToolTipText("");
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        getCanvas().setBounds(0, 0, 481, 466);
        contentPane.add(getCanvas());       
    }   

    public static Canvas getCanvas(){

        if(canvas == null){

            canvas = new Canvas();

        }

        return canvas;

    }   
}

1 answer

3

Hello, that would help you a little:

public static void main(String[] args){

        JanelaDesenho jd = new JanelaDesenho();
        jd.setVisible(true);
        while(true){
            desenhar();
        }
}

However, I have some suggestions:

  • Do not mix AWT with Swing unnecessarily.

  • Instead, use Jpanel or Jcomponent.

  • Read something about it: Basic Tutorial, more advanced.
  • In the Design class make a legacy of Jcomponent, and implement Runnable for Thread.

    public class Desenho extends JComponent implements Runnable {
    
        public Desenho() {
            new Thread(this).start();
        }   
    
        @Override
        public void paint(Graphics g){
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 100, 100);
        }
    
        @Override
        public void run() {
    
            while(true){
                try {
                    Thread.sleep(100);
                    repaint();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Desenho.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
        }
    }
    

Now just add an instance of the Drawing class in the GUI Jpanel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.