Je fais un programme de peinture simple en java, je peux dessiner mais quand je minimise la fenêtre ou redimensionne ce que j'ai peint disparaît. Comment dois-je faire pour rester?


Voici ce que j'ai jusqu'à présent, j'ai essayé de créer la fonction draw juste pour tester si cela fonctionnerait. La palette est juste une autre fenêtre gui qui aura des options de couleur une fois que j'ai compris ce problème de sauvegarde en premier

import csc260final.Palette;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JOptionPane;


/**
*
* @author Jared Hughes
*/
public class Canvas extends javax.swing.JFrame {

/**
 * Creates new form Canvas
 */


Draw line;
private int curX, curY, oldX, oldY;
public Graphics2D g2;
Palette palette;



public Canvas() {

    initComponents();
    palette = new Palette();
    palette.setVisible(true);



}
public void draw(){

     g2 = (Graphics2D) jPanel1.getGraphics();
     g2.setColor(Color.yellow);
     g2.setStroke(new BasicStroke(10));
     g2.drawLine(oldX, oldY, curX, curY);

}

public void paint(Graphics g){

    Graphics2D g2 = (Graphics2D)g;
    super.paint(g);




}



/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    jPanel1.setPreferredSize(new java.awt.Dimension(1130, 550));
    jPanel1.setRequestFocusEnabled(false);
    jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
        public void mouseDragged(java.awt.event.MouseEvent evt) {
            jPanel1MouseDragged(evt);
        }
        public void mouseMoved(java.awt.event.MouseEvent evt) {
            jPanel1MouseMoved(evt);
        }
    });
    jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jPanel1MouseClicked(evt);
        }
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            jPanel1MouseEntered(evt);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            jPanel1MouseExited(evt);
        }
        public void mousePressed(java.awt.event.MouseEvent evt) {
            jPanel1MousePressed(evt);
        }
        public void mouseReleased(java.awt.event.MouseEvent evt) {
            jPanel1MouseReleased(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 1130, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 550, Short.MAX_VALUE)
    );

    jButton1.setText("jButton1");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addGap(94, 94, 94))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    // save x and y when mouse is pressed        
     curX = evt.getX();
     curY = evt.getY();
     oldX = curX;
     oldY = curY; 


}                                    

private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    // get x and y when mouse is dragged

     curX = evt.getX();
     curY = evt.getY();

     g2 = (Graphics2D) jPanel1.getGraphics();
     g2.setColor(Color.yellow);
     g2.setStroke(new BasicStroke(10));
     g2.drawLine(oldX, oldY, curX, curY);

     oldX = curX;
     oldY = curY; 



}                                    

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:      


}                                    

private void jPanel1MouseEntered(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
}                                    

private void jPanel1MouseMoved(java.awt.event.MouseEvent evt) {                                   
    // TODO add your handling code here:
}                                  

private void jPanel1MouseExited(java.awt.event.MouseEvent evt) {                                    
    // TODO add your handling code here:
}                                   

private void jPanel1MouseReleased(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:


}                                     

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Canvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Canvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Canvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Canvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>


    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Canvas().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   

}

Author: Hovercraft Full Of Eels, 2016-12-14

1 answers

Votre dessin de swing est mal fait. Vous ne devez pas dessiner avec un objet graphique obtenu en appelant getGraphics() sur un composant. Cela renverra un objet graphique de courte durée, risquant de disparaître des graphiques ou pire, une NullPointerException. Au lieu de cela, dessinez dans la méthode paintComponent(...) de JPanel directement ou indirectement en dessinant sur un BufferedImage (oui, vous pouvez obtenir son objet graphique via getGraphics()), puis dessinez le BufferedImage sur l'interface GRAPHIQUE dans paintComponent méthode.

Tutoriels Java utiles:

Petites piqûres:

  • Évitez de donner à vos classes des noms qui entrent en conflit avec les classes Java de base, telles que Canvas. Cela aidera à éviter de confondre les autres et votre futur moi.
  • Lors de la publication du code ici, nous l'apprécions grandement si vous vous efforcez de le formater, y compris l'utilisation judicieuse des lignes vides. Votre code ci-dessus a plusieurs grandes régions vides qui rendent le suivi difficile. Une seule ligne vide ici ou là est OK, mais beaucoup, pas tellement.

Par exemple:

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;

@SuppressWarnings("serial")
public class TestDrawingCanvas extends JPanel {
    private DrawingCanvas drawingCanvas = new DrawingCanvas();

    public TestDrawingCanvas() {
        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        btnPanel.add(new JButton(new ClearAction()));

        setLayout(new BorderLayout());
        add(drawingCanvas, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_END);
    }

    private class ClearAction extends AbstractAction {
        public ClearAction() {
            super("Clear Canvas");
            putValue(MNEMONIC_KEY, KeyEvent.VK_C);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            drawingCanvas.clear();
        }
    }

    private static void createAndShowGui() {
        TestDrawingCanvas mainPanel = new TestDrawingCanvas();

        JFrame frame = new JFrame("Drawing Canvas");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

@SuppressWarnings("serial")
class DrawingCanvas extends JPanel {
    private static final int PREF_W = 800;
    private static final int PREF_H = 600;
    public static final Color LINE_COLOR = Color.YELLOW;
    public static final Stroke IMG_STROKE = new BasicStroke(10f);
    private Color lineColor = LINE_COLOR;
    private BufferedImage image;

    public DrawingCanvas() {
        setBackground(Color.WHITE);
        image = new BufferedImage(PREF_W, PREF_H, BufferedImage.TYPE_INT_ARGB);
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
            g.drawImage(image, 0, 0, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public void clear () {
        image = new BufferedImage(PREF_W, PREF_H, BufferedImage.TYPE_INT_ARGB);
        repaint();
    }

    private class MyMouse extends MouseAdapter {
        private Graphics2D imgG2d;
        private Point p1;

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }

            imgG2d = image.createGraphics();
            imgG2d.setColor(lineColor);
            imgG2d.setStroke(IMG_STROKE);
            p1 = e.getPoint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            drawLine(e);
            imgG2d.dispose();
            p1 = null;
            imgG2d = null;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            drawLine(e);
            p1 = e.getPoint();
        }

        private void drawLine(MouseEvent e) {
            if (imgG2d == null || p1 == null) {
                return;
            }
            Point p2 = e.getPoint();
            int x1 = p1.x;
            int y1 = p1.y;
            int x2 = p2.x;
            int y2 = p2.y;
            imgG2d.drawLine(x1, y1, x2, y2);
            repaint();
        }
    }
}
 2
Author: Hovercraft Full Of Eels, 2016-12-13 23:46:30