Why is the image I painted on the canvas disappearing? [closed]

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

    }   
}
    
asked by anonymous 10.09.2016 / 18:38

1 answer

3

Hello, this 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 about it: Basic Tutorial , more advanced .
  • In the Draw class, make a JComponent inheritance, and implement Runnable to 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 JPanel of the graphical interface.

    
10.09.2016 / 19:43