diff --git a/image.png b/image.png new file mode 100644 index 0000000..d7d6c6c Binary files /dev/null and b/image.png differ diff --git a/pic.png b/pic.png deleted file mode 100644 index 94bf0e6..0000000 Binary files a/pic.png and /dev/null differ diff --git a/src/paint/ColorChoose.java b/src/paint/ColorChoose.java index b9cf5ba..74bd2f0 100644 --- a/src/paint/ColorChoose.java +++ b/src/paint/ColorChoose.java @@ -81,7 +81,7 @@ public class ColorChoose extends javax.swing.JDialog { }// //GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed - this.setVisible(false); + this.dispose(); }//GEN-LAST:event_jButton1ActionPerformed diff --git a/src/paint/Paint.form b/src/paint/Paint.form index a3a66e9..53a7e52 100644 --- a/src/paint/Paint.form +++ b/src/paint/Paint.form @@ -1,6 +1,6 @@ -
+ @@ -23,29 +23,25 @@ - - + + + - + - + + + - - - - - - - @@ -59,38 +55,15 @@ - - - - - - - - - - - - - - - + - - - - - - - - - - - - + + + @@ -104,6 +77,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -122,7 +136,7 @@ - + @@ -144,24 +158,122 @@ - + + + - - - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/paint/Paint.java b/src/paint/Paint.java index 6162052..f3df70c 100644 --- a/src/paint/Paint.java +++ b/src/paint/Paint.java @@ -23,9 +23,14 @@ */ package paint; -import javax.swing.JColorChooser; -import javax.swing.JComponent; -import javax.swing.colorchooser.DefaultColorSelectionModel; +import java.awt.Dimension; +import java.awt.FileDialog; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.imageio.ImageIO; /** * @@ -50,75 +55,42 @@ public class Paint extends javax.swing.JFrame { // //GEN-BEGIN:initComponents private void initComponents() { - paint = new paint.PaintPanel(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); + jToolBar1 = new javax.swing.JToolBar(); + jButton17 = new javax.swing.JButton(); + jButton12 = new javax.swing.JButton(); + jButton16 = new javax.swing.JButton(); + jSeparator1 = new javax.swing.JToolBar.Separator(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jButton7 = new javax.swing.JButton(); jButton8 = new javax.swing.JButton(); jButton9 = new javax.swing.JButton(); - jButton10 = new javax.swing.JButton(); + jSeparator2 = new javax.swing.JToolBar.Separator(); + jButton13 = new javax.swing.JButton(); + jButton14 = new javax.swing.JButton(); + jSeparator3 = new javax.swing.JToolBar.Separator(); + jButton1 = new javax.swing.JButton(); + jButton2 = new javax.swing.JButton(); + jButton3 = new javax.swing.JButton(); + jButton4 = new javax.swing.JButton(); + jButton11 = new javax.swing.JButton(); + jSeparator4 = new javax.swing.JToolBar.Separator(); jToggleButton1 = new javax.swing.JToggleButton(); + jButton10 = new javax.swing.JButton(); + jButton15 = new javax.swing.JButton(); + pan = new javax.swing.JPanel(); + scroll = new javax.swing.JScrollPane(); + paint = new paint.PaintPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - paint.setBackground(new java.awt.Color(255, 255, 255)); - jPanel1.setBackground(new java.awt.Color(123, 189, 254)); jLabel1.setFont(new java.awt.Font("Noto Sans", 0, 18)); // NOI18N jLabel1.setText("Paint"); - jButton5.setText("pencil"); - jButton5.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton5ActionPerformed(evt); - } - }); - - jButton6.setText("line"); - jButton6.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton6ActionPerformed(evt); - } - }); - - jButton7.setText("rectangle"); - jButton7.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton7ActionPerformed(evt); - } - }); - - jButton8.setText("oval"); - jButton8.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton8ActionPerformed(evt); - } - }); - - jButton9.setText("circle"); - jButton9.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton9ActionPerformed(evt); - } - }); - - jButton10.setBackground(new java.awt.Color(0, 0, 0)); - jButton10.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton10ActionPerformed(evt); - } - }); - - jToggleButton1.setText("filled"); - jToggleButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jToggleButton1ActionPerformed(evt); - } - }); - javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( @@ -126,50 +98,196 @@ public class Paint extends javax.swing.JFrame { .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton5) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton6) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton7) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton8) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton9) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 181, Short.MAX_VALUE) - .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap()) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() - .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel1) - .addComponent(jButton5) - .addComponent(jButton6) - .addComponent(jButton7) - .addComponent(jButton8) - .addComponent(jButton9) - .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jToggleButton1)) - .addGap(0, 0, 0)) + .addComponent(jLabel1) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); + jToolBar1.setRollover(true); + + jButton17.setText("new"); + jButton17.setFocusable(false); + jButton17.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); + jButton17.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); + jButton17.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton17ActionPerformed(evt); + } + }); + jToolBar1.add(jButton17); + + jButton12.setText("save"); + jButton12.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton12ActionPerformed(evt); + } + }); + jToolBar1.add(jButton12); + + jButton16.setText("open"); + jButton16.setFocusable(false); + jButton16.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); + jButton16.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); + jButton16.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton16ActionPerformed(evt); + } + }); + jToolBar1.add(jButton16); + jToolBar1.add(jSeparator1); + + jButton5.setText("pencil"); + jButton5.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton5ActionPerformed(evt); + } + }); + jToolBar1.add(jButton5); + + jButton6.setText("line"); + jButton6.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton6ActionPerformed(evt); + } + }); + jToolBar1.add(jButton6); + + jButton7.setText("rect"); + jButton7.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton7ActionPerformed(evt); + } + }); + jToolBar1.add(jButton7); + + jButton8.setText("oval"); + jButton8.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton8ActionPerformed(evt); + } + }); + jToolBar1.add(jButton8); + + jButton9.setText("circle"); + jButton9.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton9ActionPerformed(evt); + } + }); + jToolBar1.add(jButton9); + jToolBar1.add(jSeparator2); + + jButton13.setText("undo"); + jButton13.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton13ActionPerformed(evt); + } + }); + jToolBar1.add(jButton13); + + jButton14.setText("redo"); + jButton14.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton14ActionPerformed(evt); + } + }); + jToolBar1.add(jButton14); + jToolBar1.add(jSeparator3); + + jButton1.setText("1"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + jToolBar1.add(jButton1); + + jButton2.setText("2"); + jButton2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2ActionPerformed(evt); + } + }); + jToolBar1.add(jButton2); + + jButton3.setText("3"); + jButton3.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton3ActionPerformed(evt); + } + }); + jToolBar1.add(jButton3); + + jButton4.setText("4"); + jButton4.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton4ActionPerformed(evt); + } + }); + jToolBar1.add(jButton4); + + jButton11.setText("5"); + jButton11.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton11ActionPerformed(evt); + } + }); + jToolBar1.add(jButton11); + jToolBar1.add(jSeparator4); + + jToggleButton1.setText("fill"); + jToggleButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jToggleButton1ActionPerformed(evt); + } + }); + jToolBar1.add(jToggleButton1); + + jButton10.setBackground(new java.awt.Color(0, 0, 0)); + jButton10.setText("F"); + jButton10.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton10ActionPerformed(evt); + } + }); + jToolBar1.add(jButton10); + + jButton15.setBackground(new java.awt.Color(255, 255, 255)); + jButton15.setText("B"); + jButton15.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton15ActionPerformed(evt); + } + }); + jToolBar1.add(jButton15); + + pan.setLayout(new java.awt.BorderLayout()); + + paint.setBackground(new java.awt.Color(255, 255, 255)); + scroll.setViewportView(paint); + + pan.add(scroll, java.awt.BorderLayout.CENTER); + 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) - .addComponent(paint, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE) + .addComponent(pan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); 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) + .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, 0) - .addComponent(paint, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE) + .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(0, 0, 0) + .addComponent(pan, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE) .addGap(0, 0, 0)) ); @@ -198,17 +316,88 @@ public class Paint extends javax.swing.JFrame { private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed ColorChoose ch = new ColorChoose(this, true); - ch.col.setColor(paint.color); + ch.col.setColor(paint.fcolor); ch.setVisible(true); - paint.color = ch.col.getColor(); - jButton10.setBackground(paint.color); - ch.dispose(); + paint.fcolor = ch.col.getColor(); + jButton10.setBackground(paint.fcolor); }//GEN-LAST:event_jButton10ActionPerformed private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed paint.isfilled = jToggleButton1.isSelected(); }//GEN-LAST:event_jToggleButton1ActionPerformed + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + paint.thick = 1; + }//GEN-LAST:event_jButton1ActionPerformed + + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + paint.thick = 2; + }//GEN-LAST:event_jButton2ActionPerformed + + private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed + paint.thick = 3; + }//GEN-LAST:event_jButton3ActionPerformed + + private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed + paint.thick = 4; + }//GEN-LAST:event_jButton4ActionPerformed + + private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed + paint.thick = 5; + }//GEN-LAST:event_jButton11ActionPerformed + + private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed + paint.save(); + }//GEN-LAST:event_jButton12ActionPerformed + + private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed + paint.undo(); + }//GEN-LAST:event_jButton13ActionPerformed + + private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed + paint.redo(); + }//GEN-LAST:event_jButton14ActionPerformed + + private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton15ActionPerformed + ColorChoose ch = new ColorChoose(this, true); + ch.col.setColor(paint.bcolor); + ch.setVisible(true); + paint.bcolor = ch.col.getColor(); + jButton15.setBackground(paint.bcolor); + ch.dispose(); + }//GEN-LAST:event_jButton15ActionPerformed + + private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton16ActionPerformed + FileDialog fd = new FileDialog(this, "open", FileDialog.LOAD); + fd.setVisible(true); + fd.setMultipleMode(false); + if(fd.getFiles().length > 0) + { + File file = fd.getFiles()[0]; + paint.open(file); + repaintWorkArea(); + } + }//GEN-LAST:event_jButton16ActionPerformed + + private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton17ActionPerformed + paint.newfile(); + repaintWorkArea(); + }//GEN-LAST:event_jButton17ActionPerformed + + public void repaintWorkArea() + { + int w,h; + + if(pan.getWidth() > paint.width) w = paint.width + 3; + else w = pan.getWidth(); + + if(pan.getHeight() > paint.height) h = paint.height + 3; + else h = pan.getHeight(); + + scroll.setSize(w, h); + scroll.revalidate(); + } + /** * @param args the command line arguments */ @@ -223,7 +412,18 @@ public class Paint extends javax.swing.JFrame { } // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton jButton1; private javax.swing.JButton jButton10; + private javax.swing.JButton jButton11; + private javax.swing.JButton jButton12; + private javax.swing.JButton jButton13; + private javax.swing.JButton jButton14; + private javax.swing.JButton jButton15; + private javax.swing.JButton jButton16; + private javax.swing.JButton jButton17; + private javax.swing.JButton jButton2; + private javax.swing.JButton jButton3; + private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JButton jButton6; private javax.swing.JButton jButton7; @@ -231,7 +431,14 @@ public class Paint extends javax.swing.JFrame { private javax.swing.JButton jButton9; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; + private javax.swing.JToolBar.Separator jSeparator1; + private javax.swing.JToolBar.Separator jSeparator2; + private javax.swing.JToolBar.Separator jSeparator3; + private javax.swing.JToolBar.Separator jSeparator4; private javax.swing.JToggleButton jToggleButton1; + private javax.swing.JToolBar jToolBar1; private paint.PaintPanel paint; + private javax.swing.JPanel pan; + private javax.swing.JScrollPane scroll; // End of variables declaration//GEN-END:variables } diff --git a/src/paint/PaintPanel.java b/src/paint/PaintPanel.java index ac01186..674d7a0 100644 --- a/src/paint/PaintPanel.java +++ b/src/paint/PaintPanel.java @@ -23,13 +23,22 @@ */ package paint; +import java.awt.BasicStroke; import java.awt.Color; import java.awt.Component; +import java.awt.Dimension; import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.imageio.ImageIO; /** * @@ -37,82 +46,204 @@ import java.util.ArrayList; */ public class PaintPanel extends Component implements MouseMotionListener, MouseListener { - public ArrayList parts = new ArrayList<>(); - public Color color = Color.BLACK; - public String shape = "pencil"; - public Boolean isfilled = false; + public int width , height; + public Color fcolor; + public Color bcolor; + public String shape; + public Boolean isfilled; + public int thick; + public BufferedImage it; + private ArrayList parts; + private ArrayList undo; + private Graphics2D gt; public PaintPanel() { + width = 600; + height = 300; + parts = new ArrayList<>(); + undo = new ArrayList<>(); + fcolor = Color.BLACK; + bcolor = Color.WHITE; + shape = "pencil"; + isfilled = false; + thick = 1; + it = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + gt = it.createGraphics(); + gt.setColor(Color.white); + gt.fillRect(0, 0, width, height); + this.addMouseListener(this); this.addMouseMotionListener(this); + } + + public void draw(Graphics2D gr, Part part) + { + gr.setColor(part.fcolor); + gr.setStroke(new java.awt.BasicStroke(part.thick)); + if(part.shape.equals("pencil")) + { + int x = part.cords.get(0).x; + int y = part.cords.get(0).y; + + + for(int i = 1; i < part.cords.size(); i++) + { + gr.drawLine(x, y, part.cords.get(i).x, part.cords.get(i).y); + x = part.cords.get(i).x; + y = part.cords.get(i).y; + } + } + else if(part.shape.equals("line")) + { + if(part.cords.size() > 1) gr.drawLine(part.cords.get(0).x, part.cords.get(0).y, part.cords.get(1).x, part.cords.get(1).y); + } + else if(part.shape.equals("rect")) + { + if(part.cords.size() > 1) + { + int x1,y1,x2,y2; + x1 = Math.min(part.cords.get(0).x, part.cords.get(1).x); + y1 = Math.min(part.cords.get(0).y, part.cords.get(1).y); + x2 = Math.max(part.cords.get(0).x, part.cords.get(1).x); + y2 = Math.max(part.cords.get(0).y, part.cords.get(1).y); + if(part.isfilled) { + gr.setColor(part.bcolor); + gr.fillRect(x1,y1,x2 - x1,y2 - y1); + gr.setColor(part.fcolor); + gr.drawRect(x1,y1,x2 - x1,y2 - y1); + } + else gr.drawRect(x1,y1,x2 - x1,y2 - y1); + } + } + else if(part.shape.equals("oval")) + { + if(part.cords.size() > 1) + { + int x1,y1,x2,y2; + x1 = Math.min(part.cords.get(0).x, part.cords.get(1).x); + y1 = Math.min(part.cords.get(0).y, part.cords.get(1).y); + x2 = Math.max(part.cords.get(0).x, part.cords.get(1).x); + y2 = Math.max(part.cords.get(0).y, part.cords.get(1).y); + if(part.isfilled) { + gr.setColor(part.bcolor); + gr.fillOval(x1,y1,x2 - x1,y2 - y1); + gr.setColor(part.fcolor); + gr.drawOval(x1,y1,x2 - x1,y2 - y1); + } + else gr.drawOval(x1,y1,x2 - x1,y2 - y1); + } + } + else if(part.shape.equals("circle")) + { + if(part.cords.size() > 1) + { + int x1,y1,x2,y2,r; + x1 = Math.min(part.cords.get(0).x, part.cords.get(1).x); + y1 = Math.min(part.cords.get(0).y, part.cords.get(1).y); + x2 = Math.max(part.cords.get(0).x, part.cords.get(1).x); + y2 = Math.max(part.cords.get(0).y, part.cords.get(1).y); + r = (int)Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); + if(part.isfilled) { + gr.setColor(part.bcolor); + gr.fillOval(part.cords.get(0).x - r,part.cords.get(0).y - r,2 * r,2 * r); + gr.setColor(part.fcolor); + gr.drawOval(part.cords.get(0).x - r,part.cords.get(0).y - r,2 * r,2 * r); + } + else gr.drawOval(part.cords.get(0).x - r,part.cords.get(0).y - r,2 * r,2 * r); + } + } + } + + public void paintToBuffer() + { + if(parts.size() > 10) + { + Part part = parts.get(0); + draw(gt, part); + parts.remove(0); + } + } + + public void save() + { + BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = buf.createGraphics(); + g.drawImage(it, 0,0,null); + for(Part part : parts) + { + draw(g, part); + } + try { + ImageIO.write(buf, "png", new File("image.png")); + } catch (IOException ex) { + Logger.getLogger(PaintPanel.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void open(File file) + { + BufferedImage buff = null; + try { + buff = ImageIO.read(file); + } catch (IOException ex) { + Logger.getLogger(Paint.class.getName()).log(Level.SEVERE, null, ex); + } + width = buff.getWidth(); + height = buff.getHeight(); + this.setPreferredSize(new Dimension(width, height)); + it = buff; + parts = new ArrayList<>(); + undo = new ArrayList<>(); + this.repaint(); + } + + public void newfile() + { + parts = new ArrayList<>(); + undo = new ArrayList<>(); + it = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + gt = it.createGraphics(); + gt.setColor(Color.white); + gt.fillRect(0, 0, width, height); + width = 600; + height = 300; + this.setSize(new Dimension(width, height)); + this.repaint(); + } + + public void undo() + { + if(parts.size() > 0) + { + undo.add(parts.get(parts.size()-1)); + parts.remove(parts.size()-1); + this.repaint(); + } + } + + public void redo() + { + if(undo.size() > 0) + { + parts.add(undo.get(undo.size()-1)); + undo.remove(undo.size()-1); + this.repaint(); + } } @Override public void paint(Graphics g) { + Graphics2D gr = (Graphics2D)g; + gr.drawImage(it, 0,0,null); for(Part part : parts) { - g.setColor(part.color); - if(part.shape.equals("pencil")) - { - int x = part.cords.get(0).x; - int y = part.cords.get(0).y; - - - for(int i = 1; i < part.cords.size(); i++) - { - g.drawLine(x, y, part.cords.get(i).x, part.cords.get(i).y); - x = part.cords.get(i).x; - y = part.cords.get(i).y; - } - } - else if(part.shape.equals("line")) - { - if(part.cords.size() > 1) g.drawLine(part.cords.get(0).x, part.cords.get(0).y, part.cords.get(1).x, part.cords.get(1).y); - } - else if(part.shape.equals("rect")) - { - if(part.cords.size() > 1) - { - int x1,y1,x2,y2; - x1 = Math.min(part.cords.get(0).x, part.cords.get(1).x); - y1 = Math.min(part.cords.get(0).y, part.cords.get(1).y); - x2 = Math.max(part.cords.get(0).x, part.cords.get(1).x); - y2 = Math.max(part.cords.get(0).y, part.cords.get(1).y); - if(part.isfilled) g.fillRect(x1,y1,x2 - x1,y2 - y1); - else g.drawRect(x1,y1,x2 - x1,y2 - y1); - } - } - else if(part.shape.equals("oval")) - { - if(part.cords.size() > 1) - { - int x1,y1,x2,y2; - x1 = Math.min(part.cords.get(0).x, part.cords.get(1).x); - y1 = Math.min(part.cords.get(0).y, part.cords.get(1).y); - x2 = Math.max(part.cords.get(0).x, part.cords.get(1).x); - y2 = Math.max(part.cords.get(0).y, part.cords.get(1).y); - if(part.isfilled) g.fillOval(x1,y1,x2 - x1,y2 - y1); - else g.drawOval(x1,y1,x2 - x1,y2 - y1); - } - } - else if(part.shape.equals("circle")) - { - if(part.cords.size() > 1) - { - int x1,y1,x2,y2,r; - x1 = Math.min(part.cords.get(0).x, part.cords.get(1).x); - y1 = Math.min(part.cords.get(0).y, part.cords.get(1).y); - x2 = Math.max(part.cords.get(0).x, part.cords.get(1).x); - y2 = Math.max(part.cords.get(0).y, part.cords.get(1).y); - r = (int)Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); - if(part.isfilled) g.fillOval(part.cords.get(0).x - r,part.cords.get(0).y - r,2 * r,2 * r); - else g.drawOval(part.cords.get(0).x - r,part.cords.get(0).y - r,2 * r,2 * r); - } - } + draw(gr, part); } + + paintToBuffer(); } @Override @@ -138,7 +269,8 @@ public class PaintPanel extends Component implements MouseMotionListener, MouseL @Override public void mousePressed(MouseEvent e) { - parts.add(new Part(shape, color,isfilled)); + if(undo.size() > 0) undo = new ArrayList<>(); + parts.add(new Part(shape, fcolor, bcolor,isfilled, thick)); parts.get(parts.size() - 1).cords.add(new Cord(e.getX(), e.getY())); this.repaint(); } @@ -169,29 +301,37 @@ class Part { String shape; ArrayList cords; - Color color; + Color fcolor; + Color bcolor; + int thick; Boolean isfilled; public Part() { this.shape = "pencil"; this.cords = new ArrayList<>(); - this.color = Color.black; + this.fcolor = Color.black; + this.bcolor = Color.white; this.isfilled = false; + this.thick = 1; } - public Part(String shape, Color color, Boolean isfilled) + public Part(String shape, Color fcolor,Color bcolor, Boolean isfilled, int thick) { this.shape = shape; this.cords = new ArrayList<>(); - this.color = color; + this.fcolor = fcolor; + this.bcolor = bcolor; this.isfilled = isfilled; + this.thick = thick; } - public Part(String shape, ArrayList cords, Color color, Boolean isfilled) + public Part(String shape, ArrayList cords, Color color, Boolean isfilled, int thick) { this.shape = shape; this.cords = cords; - this.color = color; + this.fcolor = color; + this.bcolor = bcolor; this.isfilled = isfilled; + this.thick = thick; } }