Hello,
I need help with java.
I need to create an application that is similar with paint but slightly different.

Here is the description of this project that i need to work on.

First, we need to display a menu that is consist of two choices which is 'custom animation' and 'custom brush'.

For example, if we click on 'custom brush', a variety of brushes will appear, and we can try to use them on the drawpad.
And if we click on 'custom animation', we can animate the image or shapes that we have chosen. We can bounce the shape or etc.

For now, I have just finished the coding for the menu.

May I ask you your help to assist me to finish the project please? Thank you

ps : i have no idea to create the interface (or whatever it is called hehe) for the custom brushes and the animation one

package project;

import java.awt.event.*;

import javax.swing.*;

class Menu {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                final JButton b = new JButton("PLEASE SELECT CUSTOM DOCUMENT");

                final JPopupMenu menu = new JPopupMenu("Menu");
                menu.add("CUSTOM DOCUMENT BRUSHES");
                menu.add("CUSTOM DOCUMENT SHAPES");
                menu.add("CUSTOM DOCUMENT ANIMATION");
                b.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        menu.show(b, b.getWidth()/2, b.getHeight()/2);
                    }
                } );
                JOptionPane.showMessageDialog(null,b);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

So, i have this code as the menu and this code below as the brush code (but not complete yet) which i got from the internet. I have modified the code to help me understand the use of it. And luckily, i do understand the code.

This is the code :

package project;

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.*;
import javax.swing.JLabel;



public class Testing{

    public static void main(String[] args){

        //IMAGE ICONS FOR COLORS
        Icon iconBl = new ImageIcon("black.gif");
        Icon iconGr = new ImageIcon("grey.gif");
        Icon iconW = new ImageIcon("white.gif");
        Icon iconR = new ImageIcon("red.gif");
        Icon iconG = new ImageIcon("green.gif");
        Icon iconB = new ImageIcon("blue.gif");
        Icon iconC = new ImageIcon("cyan.gif");
        Icon iconM = new ImageIcon("magenta.gif");
        Icon iconP = new ImageIcon("pink.gif");
        Icon iconO = new ImageIcon("orange.gif");
        Icon iconY = new ImageIcon("yellow.gif");


        //IMAGE ICONS FOR OPERATIONS
        Icon iconPencil = new ImageIcon("pen.gif");
        Icon iconLine = new ImageIcon("line.gif");
        Icon iconErase = new ImageIcon("erase.gif");
        Icon icon1 = new ImageIcon("pen.gif");
        Icon icon2 = new ImageIcon("line.gif");
        Icon icon3 = new ImageIcon("erase.gif");





        JFrame frame = new JFrame("CHOOSE YOUR BRUSH AND START DRAWING !");
        //Creating a frame with the title "CHOOSE YOUR BRUSH AND START DRAWING !"

        Container content = frame.getContentPane();
        //Creating 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(130,600));
        panel.setMinimumSize(new Dimension(130,600));
        panel.setMaximumSize(new Dimension(130,600));

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

            }
        });


    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


        // CALL METHOD FOR THE SIDE BUTTONS
        JButton pencilButton = new JButton(iconPencil);

        pencilButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.pencil();
            }
        });

        JButton lineButton = new JButton(iconLine);

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

        JButton eraseButton = new JButton(iconErase);

        eraseButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.erase();
            }
        });

        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();
            }
        });



        //BUTTON FOR COLORS

        //1 WHITE
        JButton whiteButton = new JButton(iconW);
        //white button
        whiteButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.white();
            }
        });

        //2 BLACK
        JButton blackButton = new JButton(iconBl);
        //same thing except this is the black button
        blackButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.black();
            }
        });

        //3 GREY
        JButton greyButton = new JButton(iconGr);
        //grey button
        greyButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.grey();
            }
        });

        //4 RED
        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();
            }

        });

        //5 PINK
        JButton pinkButton = new JButton(iconP);
        //pink button
        pinkButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.pink();
            }
        });

        //6 ORANGE
        JButton orangeButton = new JButton(iconO);
        //orange button
        orangeButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.orange();
            }
        });

        //7 YELLOW
        JButton yellowButton = new JButton(iconY);
        //yellow button
        yellowButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.yellow();
            }
        });

        //8 GREEN
        JButton greenButton = new JButton(iconG);
        //green button
        greenButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.green();
            }
        });

        //9 BLUE
        JButton blueButton = new JButton(iconB);
        //blue button
        blueButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.blue();
            }
        });

        //10 CYAN
        JButton cyanButton = new JButton(iconC);
        //cyan button
        cyanButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.cyan();
            }
        });

        //11 MAGENTA
        JButton magentaButton = new JButton(iconM);
        //magenta button
        magentaButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                drawPad.magenta();
            }
        });

       //DIMENSION FOR COLOR BUTTON
        whiteButton.setPreferredSize(new Dimension(20,20));
        blackButton.setPreferredSize(new Dimension(20,20));
        greyButton.setPreferredSize(new Dimension(20,20));
        redButton.setPreferredSize(new Dimension(20,20));
        pinkButton.setPreferredSize(new Dimension(20,20));
        orangeButton.setPreferredSize(new Dimension(20,20));
        yellowButton.setPreferredSize(new Dimension(20,20));
        greenButton.setPreferredSize(new Dimension(20,20));
        blueButton.setPreferredSize(new Dimension(20,20));
        cyanButton.setPreferredSize(new Dimension(20,20));
        magentaButton.setPreferredSize(new Dimension(20,20));

        //DIMENSION FOR THE SIDE BUTTONS
        eraseButton.setPreferredSize(new Dimension(50,50));
        lineButton.setPreferredSize(new Dimension(50,50));
        pencilButton.setPreferredSize(new Dimension(50,50));
        clearButton.setPreferredSize(new Dimension(60,25));

        //ADD BUTTONS TO PANEL
        ButtonGroup lineOption = new ButtonGroup( );
        lineOption.add(thinButton);
        lineOption.add(mediumButton);
        lineOption.add(thickButton);

        panel2.add(whiteButton);
        panel2.add(blackButton);
        panel2.add(greyButton);
        panel2.add(redButton);
        panel2.add(pinkButton);
        panel2.add(orangeButton);
        panel2.add(yellowButton);
        panel2.add(greenButton);
        panel2.add(blueButton);
        panel2.add(cyanButton);
        panel2.add(magentaButton);



        panel.add(eraseButton);
        panel.add(pencilButton);
        panel.add(lineButton);
        panel.add(thickButton);
        panel.add(mediumButton);
        panel.add(thinButton);
        panel.add(clearButton);


        content.add(panel, BorderLayout.WEST);
        //sets the panel to the left
        content.add(panel2, BorderLayout.NORTH);

        content.add(panel3, BorderLayout.EAST);

        frame.setSize(1500, 800);
        //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 pencil()
    {
        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();

            }
          });
}





        //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()
{
}  



//PROPERTIES 
public void thick(){
        graphics2D.setStroke(new BasicStroke (10));
}
public void thin(){
        graphics2D.setStroke(new BasicStroke (1));
}
public void medium(){
        graphics2D.setStroke(new BasicStroke (5));
}
//thickness


// various colors
    //WHITE
    public void white(){
        graphics2D.setPaint(Color.white);
        repaint();
    }

    //BLACK
    public void black(){
        graphics2D.setPaint(Color.black);
        repaint();
    }

    //GREY
    public void grey(){
        graphics2D.setPaint(Color.gray);
        repaint();
    }

    //RED
    public void red(){
        graphics2D.setPaint(Color.red);
        repaint();
    }

    //PINK
    public void pink(){
        graphics2D.setPaint(Color.pink);
        repaint();
    }

    //ORANGE
    public void orange(){
        graphics2D.setPaint(Color.orange);
        repaint();
    }

    //YELLOW
    public void yellow(){
        graphics2D.setPaint(Color.yellow);
        repaint();
    }

  //GREEN
    public void green(){
        graphics2D.setPaint(Color.green);
        repaint();
    }

    //BLUE
    public void blue(){
        graphics2D.setPaint(Color.blue);
        repaint();
    }

    //CYAN
    public void cyan(){
        graphics2D.setPaint(Color.cyan);
        repaint();
    }

    //MAGENTA
    public void magenta(){
        graphics2D.setPaint(Color.magenta);
        repaint();
    }


}

So, my question now is, how do i link the codes so that if i click on the 'custom document brush' on the menu, the brush app will appear? thank you.

This article has been dead for over six months. Start a new discussion instead.