Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java Mini-Paint Program

setpreferredsize

  • Please log in to reply
15 replies to this topic

#13 puneet

puneet

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:Java, C++
  • Learning:Java, C++

Posted 15 August 2012 - 04:09 AM

hey i checked out ur paint prgram it was good but i added two buttons one for line and other for brush
i am having a problem that when u click on brush and perfrorm the task its okkk bt afterwards if u click to line button then i am not able to draw the line HELP HELP any help wud be appreciated thnx in advance

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ppaint implements ActionListener
{
JButton redbutton=new JButton("");
JButton bluebutton=new JButton("");
JButton greenbutton=new JButton("");
JButton blackbutton=new JButton("");
JButton yellowbutton=new JButton("");
JButton clearbutton=new JButton("Clear");
JButton line =new JButton("Line");
JButton brush=new JButton("");


JPanel p1=new JPanel();
JPanel p2=new JPanel();

JFrame f=new JFrame("paint");
final PadDraw drawpad=new PadDraw();
public ppaint()
{
redbutton.setIcon(new ImageIcon("D:\\red.png"));
bluebutton.setIcon(new ImageIcon("D:\\blue.png"));

yellowbutton.setIcon(new ImageIcon("D:\\yellow.png"));

greenbutton.setIcon(new ImageIcon("D:\\green.png"));

blackbutton.setIcon(new ImageIcon("D:\\black.png"));

brush.setIcon(new ImageIcon("D:\\brush.png"));

line.setIcon(new ImageIcon("D:\\line.png"));

f.setSize(400,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



redbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
drawpad.red();

}});

bluebutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
drawpad.blue();

}});
greenbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
drawpad.green();

}});
yellowbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
drawpad.yellow();

}});
blackbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
drawpad.black();

}});
clearbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
drawpad.clear();

}});
/*brush.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
drawpad.brush();

}});
line.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
drawpad.line();

}});*/
p1.setPreferredSize(new Dimension(32, 68));
p1.setMinimumSize(new Dimension(32, 68));
p1.setMaximumSize(new Dimension(32, 68));

redbutton.setPreferredSize(new Dimension(16,16));
bluebutton.setPreferredSize(new Dimension(16,16));
yellowbutton.setPreferredSize(new Dimension(16,16));
greenbutton.setPreferredSize(new Dimension(16,16));
blackbutton.setPreferredSize(new Dimension(16,16));
brush.setPreferredSize(new Dimension(40,33));
line.setPreferredSize(new Dimension(40,33));
//clearbutton.setPreferredSize(new Dimension(16,16));

p1.add(redbutton);
p1.add(bluebutton);
p1.add(yellowbutton);
p1.add(greenbutton);
p1.add(blackbutton);
p1.add(brush);
p1.add(line);
p1.add(clearbutton);

f.add(p1,BorderLayout.WEST);
f.add(drawpad,BorderLayout.CENTER);

brush.addActionListener(this);
line.addActionListener(this);
}
public void actionPerformed(ActionEvent et)
{


if(et.getSource()==brush)
{
drawpad.brush();

}
else if(et.getSource()==line)
{
drawpad.line();

}

}

public class PadDraw extends JComponent
{

Image image;


Graphics2D gr;


int curx,cury,oldx,oldy;


public PadDraw()
{}

public void brush()
{

addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
oldx=e.getX();
oldy=e.getY();
}
});


addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e)
{
curx=e.getX();
cury=e.getY();
gr.drawLine(oldx,oldy,curx,cury);
repaint();
oldx=curx;
oldy=cury;

}
});

}
public void line()
{
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
oldx=e.getX();
oldy=e.getY();
}});

addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e)
{
curx=e.getX();
cury=e.getY();

gr.drawLine(oldx,oldy,curx,cury);
repaint();


}});

}

public void paintComponent(Graphics g){

if(image == null)
{
image = createImage(getSize().width, getSize().height);
gr = (Graphics2D)image.getGraphics();
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}


public void red()
{
gr.setPaint(Color.RED);
repaint();
}
public void blue()
{gr.setPaint(Color.BLUE);
repaint();
}

public void green()
{gr.setPaint(Color.GREEN);
repaint();
}
public void yellow()
{gr.setPaint(Color.YELLOW);
repaint();
}
public void black()
{gr.setPaint(Color.BLACK);
repaint();
}
public void clear()
{gr.setPaint(Color.WHITE);

gr.fillRect(0, 0, getSize().width, getSize().height);
gr.setPaint(Color.black);

repaint();
}

}


public static void main(String arg[])
{new ppaint();}



}

hey i checked out ur paint prgram it was good but i added two buttons one for line and other for brush
i am having a problem that when u click on brush and perfrorm the task its okkk bt afterwards if u click to line button then i am not able to draw the line HELP HELP any help wud be appreciated thnx in advance


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



public class paint{

public paint()
{
Icon iconB = new ImageIcon("D:\\blue.png");



Icon iconM = new ImageIcon("D:\\yellow.png");



Icon iconR = new ImageIcon("D:\\red.png");



Icon iconBl = new ImageIcon("D:\\black.png");



Icon iconG = new ImageIcon("D:\\green.png");



JFrame frame = new JFrame("Paint It");




Container content = frame.getContentPane();

//Creates a new container

content.setLayout(new BorderLayout());

//sets the layout



final PadDraw1 drawPad = new PadDraw1();

//creates a new padDraw, which is pretty much the paint program



frame.add(drawPad, BorderLayout.CENTER);





JPanel panel = new JPanel();



panel.setPreferredSize(new Dimension(32, 68));

panel.setMinimumSize(new Dimension(32, 68));

panel.setMaximumSize(new Dimension(32, 68));



JButton clearButton = new JButton("Clear");


clearButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

drawPad.clear();

}

});

//this is the clear button, which clears the screen. This pretty

//much attaches an action listener to the button and when the

//button is pressed it calls the clear() method



JButton redButton = new JButton(iconR);

//creates the red button and sets the icon we created for red

redButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

drawPad.red();

}


});

//when pressed it will call the red() method. So on and so on =]
JButton line=new JButton("LINE");
line.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

drawPad.line();

}


});

JButton blackButton = new JButton(iconBl);

//same thing except this is the black button

blackButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

drawPad.black();

}

});



JButton yellowButton = new JButton(iconM);

//magenta button

yellowButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

drawPad.yellow();

}

});



JButton blueButton = new JButton(iconB);

//blue button

blueButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

drawPad.blue();

}

});



JButton greenButton = new JButton(iconG);

//green button

greenButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

drawPad.green();

}

});

blackButton.setPreferredSize(new Dimension(16, 16));

yellowButton.setPreferredSize(new Dimension(16, 16));

redButton.setPreferredSize(new Dimension(16, 16));

blueButton.setPreferredSize(new Dimension(16, 16));

greenButton.setPreferredSize(new Dimension(16,16));
line.setPreferredSize(new Dimension(16,16));
//sets the sizes of the buttons



panel.add(greenButton);

panel.add(blueButton);

panel.add(yellowButton);

panel.add(blackButton);

panel.add(redButton);

panel.add(clearButton);
panel.add(line);
//adds the buttons to the panel



frame.add(panel, BorderLayout.WEST);

//sets the panel to the left



frame.setSize(600, 600);

//sets the size of the frame

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//makes it so you can close

frame.setVisible(true);

//makes it so you can see it



}

public static void main(String[] args){

new paint();
}

}



class PadDraw1 extends JComponent{

Image image;

//this is gonna be your image that you draw on

Graphics2D graphics2D;

//this is what we'll be using to draw on

int currentX, currentY, oldX, oldY;

//these are gonna hold our mouse coordinates


//Now for the constructors

public PadDraw1(){

//setDoubleBuffered(false);

addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e){

oldX = e.getX();

oldY = e.getY();

}

});

//if the mouse is pressed it sets the oldX & oldY

//coordinates as the mouses x & y coordinates




addMouseListener(new MouseAdapter(){

public void mouseReleased(MouseEvent e){

currentX = e.getX();

currentY = e.getY();

// if(graphics2D != null)

graphics2D.drawLine(oldX, oldY, currentX, currentY);

repaint();

oldX = currentX;

oldY = currentY;

}

});






addMouseMotionListener(new MouseMotionAdapter(){

public void mouseDragged(MouseEvent e){

currentX = e.getX();

currentY = e.getY();

// if(graphics2D != null)

graphics2D.drawLine(oldX, oldY, currentX, currentY);

repaint();

oldX = currentX;

oldY = currentY;

}


});
//while the mouse is dragged it sets currentX & currentY as the mouses x and y

//then it draws a line at the coordinates

//it repaints it and sets oldX and oldY as currentX and currentY

}


public void paintComponent(Graphics g){

if(image == null){

image = createImage(getSize().width, getSize().height);

graphics2D = (Graphics2D)image.getGraphics();

graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

clear();


}

g.drawImage(image, 0, 0, null);

}

//this is the painting bit

//if it has nothing on it then

//it creates an image the size of the window

//sets the value of Graphics as the image

//sets the rendering

//runs the clear() method

//then it draws the image


public void line()
{


addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e){

oldX = e.getX();

oldY = e.getY();

}

});

//if the mouse is pressed it sets the oldX & oldY

//coordinates as the mouses x & y coordinates




addMouseListener(new MouseAdapter(){

public void mouseReleased(MouseEvent e){

currentX = e.getX();

currentY = e.getY();

// if(graphics2D != null)

graphics2D.drawLine(oldX, oldY, currentX, currentY);

repaint();

oldX = currentX;

oldY = currentY;

}

});










}



public void clear(){

graphics2D.setPaint(Color.white);

graphics2D.fillRect(0, 0, getSize().width, getSize().height);

graphics2D.setPaint(Color.black);

repaint();

}

//this is the clear

//it sets the colors as white

//then it fills the window with white

//thin it sets the color back to black

public void red(){

graphics2D.setPaint(Color.red);

repaint();

}

//this is the red paint

public void black(){

graphics2D.setPaint(Color.black);

repaint();

}

//black paint

public void yellow(){

graphics2D.setPaint(Color.yellow);

repaint();

}

//magenta paint

public void blue(){

graphics2D.setPaint(Color.blue);

repaint();

}

//blue paint

public void green(){

graphics2D.setPaint(Color.green);

repaint();

}

//green paint


}
  • 0

#14 javacrooks

javacrooks

    CC Newcomer

  • Member
  • PipPip
  • 23 posts
  • Programming Language:Java, JavaScript, Visual Basic .NET
  • Learning:Java, JavaScript

Posted 20 August 2012 - 07:17 AM

getting some error padDraw in the original post!!!
  • 0

#15 preetisoft2

preetisoft2

    CC Regular

  • Member
  • PipPipPip
  • 26 posts
  • Programming Language:C, Java, C++
  • Learning:PHP

Posted 22 August 2012 - 03:35 AM

Thanks for this code.
It is really awesome.
  • 0

#16 compAkS

compAkS

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 17 June 2013 - 11:36 PM

 Here is my paint program...

I have added the option of save and open in the menubar

However I am not aware of the code for save and open....

Other forums code for save and open are not running for my program

 

Please help

 

The code is as follows..

 

 

 

 

 

 

 

 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;
 
 
public class paint{
    
    public static void main(String[] args){
        
        Icon iconB = new ImageIcon("blue.gif");
        Icon iconM = new ImageIcon("magenta.gif");
        Icon iconR = new ImageIcon("red.gif");
        Icon iconBl = new ImageIcon("black.gif");
        Icon iconW = new ImageIcon("white.gif");
        Icon iconGr = new ImageIcon("grey.gif");
        Icon iconY = new ImageIcon("yellow.gif");
        Icon iconG = new ImageIcon("green.gif");
        Icon iconO = new ImageIcon("orange.gif");
        Icon iconC = new ImageIcon("cyan.gif");
        Icon iconP = new ImageIcon("pink.gif");
        //image icons for different colors.
        Icon iconCl = new ImageIcon("close.gif");
        //image icons for close button
        
        
        JFrame frame = new JFrame("Paint It");
        //Creates a frame with a title of "Paint it"
        Container content = frame.getContentPane();
        //Creates a new container
        content.setLayout(new BorderLayout());
        //sets the layout
        final PadDraw drawPad= new PadDraw();
        //creates a new padDraw, which is pretty much the paint program
        content.add(drawPad, BorderLayout.CENTER);
        //sets the padDraw in the center
        
        JPanel panel = new JPanel();
        //creates a JPanel
        panel.setPreferredSize(new Dimension(150,300));
        panel.setMinimumSize(new Dimension(150,300));
        panel.setMaximumSize(new Dimension(150,300));
        
        JPanel panel2 =new JPanel();
        panel2.setPreferredSize(new Dimension(300, 30));
        panel2.setMinimumSize(new Dimension(300, 30));
        panel2.setMaximumSize(new Dimension(300, 30));
    
        JPanel panel3 = new JPanel();
        panel3.setPreferredSize(new Dimension(40,40 ));
        panel3.setMinimumSize(new Dimension(40, 40));
        panel3.setMaximumSize(new Dimension(40, 40));
            
        //This sets the size of the panels
        
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu file = new JMenu("File");
        menuBar.add(file);
        JMenuItem save = new JMenuItem("Save");
    file.add(save);
        save.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.save();
            }
        });
        
        JMenuItem open = new JMenuItem("Open");
        file.add(open);
        open.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.open();
 
            }
        });
        
        
        JButton clearButton = new JButton("Clear");
        //creates the clear button and sets the text as "Clear"
        clearButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.clear();
            }
        });
        //this is the clear button, which clears the screen.  This pretty
        //much attaches an action listener to the button and when the
        //button is pressed it calls the clear() method
        
        JButton eraseButton = new JButton("Eraser");
        //creates the eraser button and sets the text as "Eraser"
        eraseButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
        drawPad.erase();
            }
        });
          
        
        
JRadioButton thinButton = new JRadioButton("Thin Line");
    thinButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.thin();
            }
        });
    JRadioButton mediumButton = new JRadioButton("Medium Line");
    mediumButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.medium();
            }
        });
    JRadioButton thickButton = new JRadioButton("Thick Line");
    thickButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.thick();
            }
        });
        
        //the above three buttons are for the thickness
        
        
        
        JButton rectButton = new JButton("Hollow Rectange");
        rectButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){ 
               drawPad.rect();
              }
          });
          
          JButton rect2Button = new JButton("Filled Rectange");
        rect2Button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
               drawPad.rect2();
              }
          });
          
          JButton ovalButton = new JButton("Hollow Oval");
          ovalButton.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  drawPad.oval();
                  
              }
          });
          
          JButton oval2Button = new JButton("Filled Oval");
          oval2Button.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  drawPad.oval2();
                
              }
          });
            
          JButton penButton = new JButton("Pencil");
        penButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.pen();
            }
        });
          JButton lineButton = new JButton("Line");
        lineButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.line();
            }
        });
        
        JButton polygonButton = new JButton("Polygon");
        polygonButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.polygon();
            }
        });
        
        //the above buttons are for various operations in paint
        
        JButton closeButton = new JButton(iconCl);
        //creates the  close button
        closeButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
              
        
        
        
        // the buttons below are for various colors
        
        JButton redButton = new JButton(iconR);
        //creates the red button and sets the icon we created for red
        redButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.red();
            }
 
        });
        //when pressed it will call the red() method.  So on and so forth =]
        
        JButton blackButton = new JButton(iconBl);
        //same thing except this is the black button
        blackButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.black();
            }
        });
        
        JButton magentaButton = new JButton(iconM);
        //magenta button
        magentaButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.magenta();
            }
        });
        
        JButton blueButton = new JButton(iconB);
        //blue button
        blueButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.blue();
            }
        });
        
        JButton greenButton = new JButton(iconG);
        //green button
        greenButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.green();
            }
        });
        
        JButton greyButton = new JButton(iconGr);
        //grey button
        greyButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.grey();
            }
        });
        
        JButton whiteButton = new JButton(iconW);
        //white button
        whiteButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.white();
            }
        });
        
        JButton yellowButton = new JButton(iconY);
        //yellow button
        yellowButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.yellow();
            }
        });
        
    
        
        JButton orangeButton = new JButton(iconO);
        //orange button
        orangeButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.orange();
            }
        });
        
        
        JButton cyanButton = new JButton(iconC);
        //cyan button
        cyanButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.cyan();
            }
        });
        
        JButton pinkButton = new JButton(iconP);
        //pink button
        pinkButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.pink();
            }
        });
    
        
        blackButton.setPreferredSize(new Dimension(16,16));
        magentaButton.setPreferredSize(new Dimension(16, 16));
        redButton.setPreferredSize(new Dimension(16, 16));
        blueButton.setPreferredSize(new Dimension(16, 16));
        greenButton.setPreferredSize(new Dimension(16,16));
        greyButton.setPreferredSize(new Dimension(16,16));
        whiteButton.setPreferredSize(new Dimension(16,16));
        yellowButton.setPreferredSize(new Dimension(16,16));
        orangeButton.setPreferredSize(new Dimension(16,16));
        cyanButton.setPreferredSize(new Dimension(16,16));
        pinkButton.setPreferredSize(new Dimension(16,16));
        clearButton.setPreferredSize(new Dimension(140,25));
        eraseButton.setPreferredSize(new Dimension(140,25));
        lineButton.setPreferredSize(new Dimension(140,25));
        rectButton.setPreferredSize(new Dimension(140,25));
        rect2Button.setPreferredSize(new Dimension(140,25));
        penButton.setPreferredSize(new Dimension(140,25));
        ovalButton.setPreferredSize(new Dimension(140,25));
        oval2Button.setPreferredSize(new Dimension(140,25));
        polygonButton.setPreferredSize(new Dimension(140,25));
        
        closeButton.setPreferredSize(new Dimension(19,19));
        
        
        //sets the sizes of the buttons
        
    
    ButtonGroup lineOption = new ButtonGroup( );
    lineOption.add(thinButton);
    lineOption.add(mediumButton);
    lineOption.add(thickButton);
    
    panel2.add(blackButton);
    panel2.add(greyButton);
    panel2.add(whiteButton);
    panel2.add(redButton);
    panel2.add(greenButton);
    panel2.add(blueButton);
    panel2.add(cyanButton);
    panel2.add(magentaButton);
    panel2.add(pinkButton);
    panel2.add(orangeButton);
    panel2.add(yellowButton);
    
    
        panel.add(clearButton);
        panel.add(eraseButton);
        panel.add(penButton);
        panel.add(lineButton);
        panel.add(rectButton);
        panel.add(rect2Button);
        panel.add(ovalButton);
        panel.add(oval2Button);
         panel.add(polygonButton);
        panel.add(thickButton);
        panel.add(mediumButton);
        panel.add(thinButton);
        
        panel3.add(closeButton);
        
        //adds the buttons to the panels
        
        
        content.add(panel, BorderLayout.WEST);
        //sets the panel to the left
        content.add(panel2, BorderLayout.NORTH);
        
        content.add(panel3, BorderLayout.EAST);
        
        frame.setSize(1030, 735);
        //sets the size of the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //makes it so you can close
        frame.setVisible(true);
        //makes it so you can see it
    }
}
 
 
class PadDraw extends JComponent{
    Image image;
    //this is gonna be your image that you draw on
    Graphics2D graphics2D;
    //this is what we'll be using to draw on
    public int currentX, currentY, oldX, oldY,x1,y1,x2,y2,w,h,a1,a2,b1,b2,c1,d1,c2,d2,e1,f1,e2,f2,r1,s1,ch,m1,n1,fl,k1,k2=1,cc1,dd1,cc2,dd2,aa1,aa2,bb1,bb2;
    //these are gonna hold our mouse coordinates
//Now for the constructors
 
public void pen()
    {
        k1=1;
        setDoubleBuffered(false);
        cursorchange();
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                oldX = e.getX();
                oldY = e.getY();
            }
        });
        //if the mouse is pressed it sets the oldX & oldY
        //coordinates as the mouses x & y coordinates
        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                currentX = e.getX();
                currentY = e.getY();
                if(graphics2D != null && k1==1)
                    graphics2D.drawLine(oldX, oldY, currentX,currentY);
                    repaint();
                oldX = currentX;
                oldY = currentY;
                repaint();
            }
 
        });
}
        
public void line()
{
    k1=2;
setDoubleBuffered(false);
cursorchange();
        addMouseListener(new MouseAdapter()
        
          {
               public void mousePressed(MouseEvent m)
               {
               x1=m.getX();
               y1=m.getY();
               }
               public void mouseReleased(MouseEvent m)
               {
               x2=m.getX();
               y2=m.getY();               
                if(graphics2D != null && k1==2)
                graphics2D.drawLine(x1,y1, x2,y2);
                repaint();
             
            }
          });
}
 
public void rect()
        {
            k1=3;
            setDoubleBuffered(false);
            cursorchange();
                  addMouseListener(new MouseAdapter()
          {
             
               public void mousePressed(MouseEvent m)
               {
               a1=m.getX();
               b1=m.getY();
               repaint();
               }
        
               public void mouseReleased(MouseEvent m)
               {
               a2=m.getX();
               b2=m.getY();
                 if (a1>a2)
                 {
                int z=a1;
                 a1=a2;
                 a2=z;
                }
                if (b1>b2)
                 {
                int z=b1;
                 b1=b2;
                 b2=z;
                }
                w=a2-a1;
                h=b2-b1;
                if(graphics2D != null && k1==3)
                graphics2D.drawRect(a1,b1, w,h);
                    repaint();
                
}
          });
        
      }
 
      
      public void rect2()
        {
            k1=4;
            setDoubleBuffered(false);
            cursorchange();
                  addMouseListener(new MouseAdapter()
          {
               public void mousePressed(MouseEvent m)
               {
               aa1=m.getX();
               bb1=m.getY();
               repaint();
               }
               public void mouseReleased(MouseEvent m)
               {
               aa2=m.getX();
               bb2=m.getY();
                 if (aa1>aa2)
                 {
                int z=aa1;
                 aa1=aa2;
                 aa2=z;
                }
                if (bb1>bb2)
                 {
                int z=bb1;
                 bb1=bb2;
                 bb2=z;
                }
                w=aa2-aa1;
                h=bb2-bb1;
                if(graphics2D != null && k1==4)
                graphics2D.fillRect(aa1,bb1, w,h);
                    repaint();
                
}
          });
        
      }
 
      
      public void oval()
      {
          k1=5;
        setDoubleBuffered(false);
        cursorchange();
                  addMouseListener(new MouseAdapter()
          {
               public void mousePressed(MouseEvent e)
               {
               c1=e.getX();
               d1=e.getY();
               repaint();
               }
               public void mouseReleased(MouseEvent e)
               {
               c2=e.getX();
               d2=e.getY();
                 if (c1>c2)
                 {
                int z=c1;
                 c1=c2;
                 c2=z;
                }
                if (d1>d2)
                 {
                int z=d1;
                 d1=d2;
                 d2=z;
                }
                w=c2-c1;
                h=d2-d1;
                if(graphics2D != null && k1==5)
                    graphics2D.drawOval(c1,d1,w,h);
                repaint();
                }
          });
        }
        
        
        public void oval2()
      {
          k1=6;
        setDoubleBuffered(false);
        cursorchange();
                  addMouseListener(new MouseAdapter()
          {
               public void mousePressed(MouseEvent m)
               {
               cc1=m.getX();
               dd1=m.getY();
               repaint();
               }
               public void mouseReleased(MouseEvent m)
               {
               cc2=m.getX();
               dd2=m.getY();
                 if (cc1>cc2)
                 {
                int z=cc1;
                 cc1=cc2;
                 cc2=z;
                }
                if (dd1>dd2)
                 {
                int z=dd1;
                 dd1=dd2;
                 dd2=z;
                }
                w=cc2-cc1;
                h=dd2-dd1;
                if(graphics2D != null && k1==6)
                    graphics2D.fillOval(cc1,dd1, w,h);
                    repaint();
                }
          });
        }
        
        
        
        
        public void polygon()
{
    k1=8;
    k2=1;
setDoubleBuffered(false);
cursorchange();
        addMouseListener(new MouseAdapter()
        
          {
               public void mousePressed(MouseEvent m)
               {
               x1=m.getX();
               y1=m.getY();
               }
         final int r1=x1;
         final int s1=y1;
               public void mouseReleased(MouseEvent m)
               {
               x2=m.getX();
               y2=m.getY();
               if (x2==r1 && y2==s1)
               { graphics2D.drawLine(m1,n1, r1,s1);
               return;
            }
               else
               {
                   if(graphics2D != null && k1==8 && k2==1)
                    graphics2D.drawLine(x1,y1, x2,y2);
               
                if(graphics2D != null && k1==8 && k2!=1)
                graphics2D.drawLine(m1,n1, x2,y2);
                repaint();
                m1=x2;
                n1=y2;
                k2++;
                
            }
            
            }
          });
}
 
 
        //while the mouse is dragged it sets currentX & currentY as the mouses x and y
        //then it draws a line at the coordinates
        //it repaints it and sets oldX and oldY as currentX and currentY
        
    public void paintComponent(Graphics g){
        if(image == null){
            image = createImage(getSize().width, getSize().height);
            graphics2D = (Graphics2D)image.getGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            clear();
 
        }
        g.drawImage(image, 0, 0, null);
    }
    //this is the painting bit
    //if it has nothing on it then
    //it creates an image the size of the window
    //sets the value of Graphics as the image
    //sets the rendering
    //runs the clear() method
    //then it draws the image
 
 
    public void clear()
    {
        graphics2D.setPaint(Color.white);
        graphics2D.fillRect(0, 0, getSize().width, getSize().height);
        graphics2D.setPaint(Color.black);
        repaint();
    }
    //this is the clear
    //it sets the colors as white
    //then it fills the window with white
    //thin it sets the color back to black
    
    public void erase()
    {
        k1=7;
        
        cursorchange();
        graphics2D.setPaint(Color.white);
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                e1 = e.getX();
                f1 = e.getY();
            }
        });
        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                e2 = e.getX();
                f2 = e.getY();
                if(graphics2D != null && k1==7)
                graphics2D.fillRect(e1, f1, 27, 27);
                repaint();
                e1 = e2;
                f1 = f2;
            }
        });
 
}
 
 
public void cursorchange()
{
  if (k1==7)
        {
            
            Toolkit toolkit = Toolkit.getDefaultToolkit();  
        Image image = toolkit.getImage("eraser.gif"); 
        Point hotSpot = new Point(0,0);  
        Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Eraser");  
        setCursor(cursor);  
}
        
else 
setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
 
}
 
// changing the cursor icons
 
 
public void open()
{
 
 
                
        
}
 
public void save()
{
   
   
   
        }  
     
    
 
 
 
 
public void thick(){
        graphics2D.setStroke(new BasicStroke (7));
}
public void thin(){
        graphics2D.setStroke(new BasicStroke (1));
}
public void medium(){
        graphics2D.setStroke(new BasicStroke (4));
}
//thickness
 
 
// various colors
    public void red(){
        graphics2D.setPaint(Color.red);
        repaint();
    }
    //this is the red paint
    public void black(){
        graphics2D.setPaint(Color.black);
        repaint();
    }
    //black paint
    public void magenta(){
        graphics2D.setPaint(Color.magenta);
        repaint();
    }
    //magenta paint
    public void blue(){
        graphics2D.setPaint(Color.blue);
        repaint();
    }
    //blue paint
    public void green(){
        graphics2D.setPaint(Color.green);
        repaint();
    }
     //green paint
    
    public void yellow(){
        graphics2D.setPaint(Color.yellow);
        repaint();
    }
     //yellow paint
    public void grey(){
        graphics2D.setPaint(Color.gray);
        repaint();
         //grey paint
    }
    public void white(){
        graphics2D.setPaint(Color.white);
        repaint();
    }
     //white paint
    public void orange(){
        graphics2D.setPaint(Color.orange);
        repaint();
    }
     //orange paint
    public void cyan(){
        graphics2D.setPaint(Color.cyan);
        repaint();
    }
     //cyan paint
    public void pink(){
        graphics2D.setPaint(Color.pink);
        repaint();
    }
     //pink paint
     
     
}
//end of program............!!

 



This is my paint program
I want to add save and open option.
I have added the buttons in menubar but dont know how to open and save.
Please Help.
The code in other forums for open and save are not working in my program
 
 
 
 
Here is my code.
 
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;
 
 
public class paint{
    
    public static void main(String[] args){
        
        
        
        Icon iconB = new ImageIcon("blue.gif");
        Icon iconM = new ImageIcon("magenta.gif");
        Icon iconR = new ImageIcon("red.gif");
        Icon iconBl = new ImageIcon("black.gif");
        Icon iconW = new ImageIcon("white.gif");
        Icon iconGr = new ImageIcon("grey.gif");
        Icon iconY = new ImageIcon("yellow.gif");
        Icon iconG = new ImageIcon("green.gif");
        Icon iconO = new ImageIcon("orange.gif");
        Icon iconC = new ImageIcon("cyan.gif");
        Icon iconP = new ImageIcon("pink.gif");
        //image icons for different colors.
        Icon iconCl = new ImageIcon("close.gif");
        //image icons for close button
        
        
        JFrame frame = new JFrame("Paint It");
        //Creates a frame with a title of "Paint it"
        Container content = frame.getContentPane();
        //Creates a new container
        content.setLayout(new BorderLayout());
        //sets the layout
        final PadDraw drawPad= new PadDraw();
        //creates a new padDraw, which is pretty much the paint program
        content.add(drawPad, BorderLayout.CENTER);
        //sets the padDraw in the center
        
        JPanel panel = new JPanel();
        //creates a JPanel
        panel.setPreferredSize(new Dimension(150,300));
        panel.setMinimumSize(new Dimension(150,300));
        panel.setMaximumSize(new Dimension(150,300));
        
        JPanel panel2 =new JPanel();
        panel2.setPreferredSize(new Dimension(300, 30));
        panel2.setMinimumSize(new Dimension(300, 30));
        panel2.setMaximumSize(new Dimension(300, 30));
    
        JPanel panel3 = new JPanel();
        panel3.setPreferredSize(new Dimension(40,40 ));
        panel3.setMinimumSize(new Dimension(40, 40));
        panel3.setMaximumSize(new Dimension(40, 40));
            
        //This sets the size of the panels
        
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu file = new JMenu("File");
        menuBar.add(file);
        JMenuItem save = new JMenuItem("Save");
    file.add(save);
        save.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.save();
            }
        });
        
        JMenuItem open = new JMenuItem("Open");
        file.add(open);
        open.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.open();
 
            }
        });
        
        
        JButton clearButton = new JButton("Clear");
        //creates the clear button and sets the text as "Clear"
        clearButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.clear();
            }
        });
        //this is the clear button, which clears the screen.  This pretty
        //much attaches an action listener to the button and when the
        //button is pressed it calls the clear() method
        
        JButton eraseButton = new JButton("Eraser");
        //creates the eraser button and sets the text as "Eraser"
        eraseButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
        drawPad.erase();
            }
        });
          
        
        
JRadioButton thinButton = new JRadioButton("Thin Line");
    thinButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.thin();
            }
        });
    JRadioButton mediumButton = new JRadioButton("Medium Line");
    mediumButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.medium();
            }
        });
    JRadioButton thickButton = new JRadioButton("Thick Line");
    thickButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.thick();
            }
        });
        
        //the above three buttons are for the thickness
        
        
        
        JButton rectButton = new JButton("Hollow Rectange");
        rectButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){ 
               drawPad.rect();
              }
          });
          
          JButton rect2Button = new JButton("Filled Rectange");
        rect2Button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
               drawPad.rect2();
              }
          });
          
          JButton ovalButton = new JButton("Hollow Oval");
          ovalButton.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  drawPad.oval();
                  
              }
          });
          
          JButton oval2Button = new JButton("Filled Oval");
          oval2Button.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  drawPad.oval2();
                
              }
          });
            
          JButton penButton = new JButton("Pencil");
        penButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.pen();
            }
        });
          JButton lineButton = new JButton("Line");
        lineButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.line();
            }
        });
        
        JButton polygonButton = new JButton("Polygon");
        polygonButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.polygon();
            }
        });
        
        //the above buttons are for various operations in paint
        
        JButton closeButton = new JButton(iconCl);
        //creates the  close button
        closeButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
              
        
        
        
        // the buttons below are for various colors
        
        JButton redButton = new JButton(iconR);
        //creates the red button and sets the icon we created for red
        redButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.red();
            }
 
        });
        //when pressed it will call the red() method.  So on and so forth =]
        
        JButton blackButton = new JButton(iconBl);
        //same thing except this is the black button
        blackButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.black();
            }
        });
        
        JButton magentaButton = new JButton(iconM);
        //magenta button
        magentaButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.magenta();
            }
        });
        
        JButton blueButton = new JButton(iconB);
        //blue button
        blueButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.blue();
            }
        });
        
        JButton greenButton = new JButton(iconG);
        //green button
        greenButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.green();
            }
        });
        
        JButton greyButton = new JButton(iconGr);
        //grey button
        greyButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.grey();
            }
        });
        
        JButton whiteButton = new JButton(iconW);
        //white button
        whiteButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.white();
            }
        });
        
        JButton yellowButton = new JButton(iconY);
        //yellow button
        yellowButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.yellow();
            }
        });
        
    
        
        JButton orangeButton = new JButton(iconO);
        //orange button
        orangeButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.orange();
            }
        });
        
        
        JButton cyanButton = new JButton(iconC);
        //cyan button
        cyanButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.cyan();
            }
        });
        
        JButton pinkButton = new JButton(iconP);
        //pink button
        pinkButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.pink();
            }
        });
    
        
        blackButton.setPreferredSize(new Dimension(16,16));
        magentaButton.setPreferredSize(new Dimension(16, 16));
        redButton.setPreferredSize(new Dimension(16, 16));
        blueButton.setPreferredSize(new Dimension(16, 16));
        greenButton.setPreferredSize(new Dimension(16,16));
        greyButton.setPreferredSize(new Dimension(16,16));
        whiteButton.setPreferredSize(new Dimension(16,16));
        yellowButton.setPreferredSize(new Dimension(16,16));
        orangeButton.setPreferredSize(new Dimension(16,16));
        cyanButton.setPreferredSize(new Dimension(16,16));
        pinkButton.setPreferredSize(new Dimension(16,16));
        clearButton.setPreferredSize(new Dimension(140,25));
        eraseButton.setPreferredSize(new Dimension(140,25));
        lineButton.setPreferredSize(new Dimension(140,25));
        rectButton.setPreferredSize(new Dimension(140,25));
        rect2Button.setPreferredSize(new Dimension(140,25));
        penButton.setPreferredSize(new Dimension(140,25));
        ovalButton.setPreferredSize(new Dimension(140,25));
        oval2Button.setPreferredSize(new Dimension(140,25));
        polygonButton.setPreferredSize(new Dimension(140,25));
        
        closeButton.setPreferredSize(new Dimension(19,19));
        
        
        //sets the sizes of the buttons
        
    
    ButtonGroup lineOption = new ButtonGroup( );
    lineOption.add(thinButton);
    lineOption.add(mediumButton);
    lineOption.add(thickButton);
    
    panel2.add(blackButton);
    panel2.add(greyButton);
    panel2.add(whiteButton);
    panel2.add(redButton);
    panel2.add(greenButton);
    panel2.add(blueButton);
    panel2.add(cyanButton);
    panel2.add(magentaButton);
    panel2.add(pinkButton);
    panel2.add(orangeButton);
    panel2.add(yellowButton);
    
    
        panel.add(clearButton);
        panel.add(eraseButton);
        panel.add(penButton);
        panel.add(lineButton);
        panel.add(rectButton);
        panel.add(rect2Button);
        panel.add(ovalButton);
        panel.add(oval2Button);
         panel.add(polygonButton);
        panel.add(thickButton);
        panel.add(mediumButton);
        panel.add(thinButton);
        
        panel3.add(closeButton);
        
        //adds the buttons to the panels
        
        
        content.add(panel, BorderLayout.WEST);
        //sets the panel to the left
        content.add(panel2, BorderLayout.NORTH);
        
        content.add(panel3, BorderLayout.EAST);
        
        frame.setSize(1030, 735);
        //sets the size of the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //makes it so you can close
        frame.setVisible(true);
        //makes it so you can see it
    }
}
 
 
class PadDraw extends JComponent{
    Image image;
    //this is gonna be your image that you draw on
    Graphics2D graphics2D;
    //this is what we'll be using to draw on
    public int currentX, currentY, oldX, oldY,x1,y1,x2,y2,w,h,a1,a2,b1,b2,c1,d1,c2,d2,e1,f1,e2,f2,r1,s1,ch,m1,n1,fl,k1,k2=1,cc1,dd1,cc2,dd2,aa1,aa2,bb1,bb2;
    //these are gonna hold our mouse coordinates
//Now for the constructors
 
public void pen()
    {
        k1=1;
        setDoubleBuffered(false);
        cursorchange();
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                oldX = e.getX();
                oldY = e.getY();
            }
        });
        //if the mouse is pressed it sets the oldX & oldY
        //coordinates as the mouses x & y coordinates
        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                currentX = e.getX();
                currentY = e.getY();
                if(graphics2D != null && k1==1)
                    graphics2D.drawLine(oldX, oldY, currentX,currentY);
                    repaint();
                oldX = currentX;
                oldY = currentY;
                repaint();
            }
 
        });
}
        
public void line()
{
    k1=2;
setDoubleBuffered(false);
cursorchange();
        addMouseListener(new MouseAdapter()
        
          {
               public void mousePressed(MouseEvent m)
               {
               x1=m.getX();
               y1=m.getY();
               }
               public void mouseReleased(MouseEvent m)
               {
               x2=m.getX();
               y2=m.getY();               
                if(graphics2D != null && k1==2)
                graphics2D.drawLine(x1,y1, x2,y2);
                repaint();
             
            }
          });
}
 
public void rect()
        {
            k1=3;
            setDoubleBuffered(false);
            cursorchange();
                  addMouseListener(new MouseAdapter()
          {
             
               public void mousePressed(MouseEvent m)
               {
               a1=m.getX();
               b1=m.getY();
               repaint();
               }
        
               public void mouseReleased(MouseEvent m)
               {
               a2=m.getX();
               b2=m.getY();
                 if (a1>a2)
                 {
                int z=a1;
                 a1=a2;
                 a2=z;
                }
                if (b1>b2)
                 {
                int z=b1;
                 b1=b2;
                 b2=z;
                }
                w=a2-a1;
                h=b2-b1;
                if(graphics2D != null && k1==3)
                graphics2D.drawRect(a1,b1, w,h);
                    repaint();
                
}
          });
        
      }
 
      
      public void rect2()
        {
            k1=4;
            setDoubleBuffered(false);
            cursorchange();
                  addMouseListener(new MouseAdapter()
          {
               public void mousePressed(MouseEvent m)
               {
               aa1=m.getX();
               bb1=m.getY();
               repaint();
               }
               public void mouseReleased(MouseEvent m)
               {
               aa2=m.getX();
               bb2=m.getY();
                 if (aa1>aa2)
                 {
                int z=aa1;
                 aa1=aa2;
                 aa2=z;
                }
                if (bb1>bb2)
                 {
                int z=bb1;
                 bb1=bb2;
                 bb2=z;
                }
                w=aa2-aa1;
                h=bb2-bb1;
                if(graphics2D != null && k1==4)
                graphics2D.fillRect(aa1,bb1, w,h);
                    repaint();
                
}
          });
        
      }
 
      
      public void oval()
      {
          k1=5;
        setDoubleBuffered(false);
        cursorchange();
                  addMouseListener(new MouseAdapter()
          {
               public void mousePressed(MouseEvent e)
               {
               c1=e.getX();
               d1=e.getY();
               repaint();
               }
               public void mouseReleased(MouseEvent e)
               {
               c2=e.getX();
               d2=e.getY();
                 if (c1>c2)
                 {
                int z=c1;
                 c1=c2;
                 c2=z;
                }
                if (d1>d2)
                 {
                int z=d1;
                 d1=d2;
                 d2=z;
                }
                w=c2-c1;
                h=d2-d1;
                if(graphics2D != null && k1==5)
                    graphics2D.drawOval(c1,d1,w,h);
                repaint();
                }
          });
        }
        
        
        public void oval2()
      {
          k1=6;
        setDoubleBuffered(false);
        cursorchange();
                  addMouseListener(new MouseAdapter()
          {
               public void mousePressed(MouseEvent m)
               {
               cc1=m.getX();
               dd1=m.getY();
               repaint();
               }
               public void mouseReleased(MouseEvent m)
               {
               cc2=m.getX();
               dd2=m.getY();
                 if (cc1>cc2)
                 {
                int z=cc1;
                 cc1=cc2;
                 cc2=z;
                }
                if (dd1>dd2)
                 {
                int z=dd1;
                 dd1=dd2;
                 dd2=z;
                }
                w=cc2-cc1;
                h=dd2-dd1;
                if(graphics2D != null && k1==6)
                    graphics2D.fillOval(cc1,dd1, w,h);
                    repaint();
                }
          });
        }
        
        
        
        
        public void polygon()
{
    k1=8;
    k2=1;
setDoubleBuffered(false);
cursorchange();
        addMouseListener(new MouseAdapter()
        
          {
               public void mousePressed(MouseEvent m)
               {
               x1=m.getX();
               y1=m.getY();
               }
         final int r1=x1;
         final int s1=y1;
               public void mouseReleased(MouseEvent m)
               {
               x2=m.getX();
               y2=m.getY();
               if (x2==r1 && y2==s1)
               { graphics2D.drawLine(m1,n1, r1,s1);
               return;
            }
               else
               {
                   if(graphics2D != null && k1==8 && k2==1)
                    graphics2D.drawLine(x1,y1, x2,y2);
               
                if(graphics2D != null && k1==8 && k2!=1)
                graphics2D.drawLine(m1,n1, x2,y2);
                repaint();
                m1=x2;
                n1=y2;
                k2++;
                
            }
            
            }
          });
}
 
 
        //while the mouse is dragged it sets currentX & currentY as the mouses x and y
        //then it draws a line at the coordinates
        //it repaints it and sets oldX and oldY as currentX and currentY
        
    public void paintComponent(Graphics g){
        if(image == null){
            image = createImage(getSize().width, getSize().height);
            graphics2D = (Graphics2D)image.getGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            clear();
 
        }
        g.drawImage(image, 0, 0, null);
    }
    //this is the painting bit
    //if it has nothing on it then
    //it creates an image the size of the window
    //sets the value of Graphics as the image
    //sets the rendering
    //runs the clear() method
    //then it draws the image
 
 
    public void clear()
    {
        graphics2D.setPaint(Color.white);
        graphics2D.fillRect(0, 0, getSize().width, getSize().height);
        graphics2D.setPaint(Color.black);
        repaint();
    }
    //this is the clear
    //it sets the colors as white
    //then it fills the window with white
    //thin it sets the color back to black
    
    public void erase()
    {
        k1=7;
        
        cursorchange();
        graphics2D.setPaint(Color.white);
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                e1 = e.getX();
                f1 = e.getY();
            }
        });
        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                e2 = e.getX();
                f2 = e.getY();
                if(graphics2D != null && k1==7)
                graphics2D.fillRect(e1, f1, 27, 27);
                repaint();
                e1 = e2;
                f1 = f2;
            }
        });
 
}
 
 
public void cursorchange()
{
  if (k1==7)
        {
            
            Toolkit toolkit = Toolkit.getDefaultToolkit();  
        Image image = toolkit.getImage("eraser.gif"); 
        Point hotSpot = new Point(0,0);  
        Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Eraser");  
        setCursor(cursor);  
}
        
else 
setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
 
}
 
// changing the cursor icons
 
 
public void open()
{
 
}
 
public void save()
{
    
   
   
    
        }  
     
    
 
 
 
 
public void thick(){
        graphics2D.setStroke(new BasicStroke (7));
}
public void thin(){
        graphics2D.setStroke(new BasicStroke (1));
}
public void medium(){
        graphics2D.setStroke(new BasicStroke (4));
}
//thickness
 
 
// various colors
    public void red(){
        graphics2D.setPaint(Color.red);
        repaint();
    }
    //this is the red paint
    public void black(){
        graphics2D.setPaint(Color.black);
        repaint();
    }
    //black paint
    public void magenta(){
        graphics2D.setPaint(Color.magenta);
        repaint();
    }
    //magenta paint
    public void blue(){
        graphics2D.setPaint(Color.blue);
        repaint();
    }
    //blue paint
    public void green(){
        graphics2D.setPaint(Color.green);
        repaint();
    }
     //green paint
    
    public void yellow(){
        graphics2D.setPaint(Color.yellow);
        repaint();
    }
     //yellow paint
    public void grey(){
        graphics2D.setPaint(Color.gray);
        repaint();
         //grey paint
    }
    public void white(){
        graphics2D.setPaint(Color.white);
        repaint();
    }
     //white paint
    public void orange(){
        graphics2D.setPaint(Color.orange);
        repaint();
    }
     //orange paint
    public void cyan(){
        graphics2D.setPaint(Color.cyan);
        repaint();
    }
     //cyan paint
    public void pink(){
        graphics2D.setPaint(Color.pink);
        repaint();
    }
     //pink paint
     
     
}
//end of program............!!

 


  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download