2016-12-13 1 views
1

Voici ce que j'ai jusqu'ici, j'ai essayé de créer la fonction de tirage juste pour tester si cela fonctionnerait. La palette est juste une autre fenêtre qui aura IUG options de couleur une fois que je figure ce problème d'économie d'abordJe fais un simple programme de peinture en java, je peux dessiner mais quand je minimise la fenêtre ou redimensionne ce que je peins disparaît. Comment puis-je les faire rester?

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     

}

+0

'je peux dessiner, mais quand je réduis la fenêtre ou redimensionner ce que je peignais va away.' Voir [Approches de peinture personnalisée] (https://tips4java.wordpress.com/2009/05/08/custom-painting-approches /) pour deux approches. – camickr

Répondre

2

Votre dessin Swing est mal fait. Vous ne devez pas dessiner avec un objet Graphics obtenu en appelant getGraphics() sur un composant. Cela retournera un objet Graphics qui est de courte durée, risquant de faire disparaître des graphiques ou pire, une exception NullPointerException. Dessinez à la place la méthode paintComponent(...) de JPanel soit directement, soit indirectement en dessinant sur une BufferedImage (oui, vous pouvez obtenir son objet Graphics via getGraphics()) puis dessinez le BufferedImage sur l'interface graphique dans la méthode paintComponent.

utiles Java Tutoriels:

Petits nitpicks:

  • Évitez de donner vos classes noms qui se heurtent aux classes Java principales, telles que Canvas. Cela aidera à éviter d'embrouiller les autres et votre futur soi.
  • Lors de la publication du code ici, nous apprécions grandement si vous vous efforcez de le formater correctement, y compris l'utilisation judicieuse des lignes vides. Votre code ci-dessus a plusieurs grandes régions vides qui le rendent dur. 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(); 
     } 
    } 
}