Home Ask Login Register

Developers Planet

Your answer is one click away!

Cristian Gonzalez Herrada February 2016

Draw in java like windows paint

I made a java app to draw, like Windows Paint, I do not know if it's better to do it in two classes, or left unattended in a class, and on the other side. Do I can help make it more efficient?


Here is my code:

    import java.awt.Graphics;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class MyPaint{
        public static void main(String[] args){     

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

    Container content = frame.getContentPane();

    content.setLayout(new BorderLayout());      

    final PadDraw drawPad = new PadDraw();      

    content.add(drawPad, BorderLayout.CENTER);

    JPanel panel = new JPanel();

    panel.setPreferredSize(new Dimension(100, 68));
    panel.setMinimumSize(new Dimension(100, 68));
    panel.setMaximumSize(new Dimension(100, 68));

            JButton twoX = new JButton ("2");
            twoX.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e){

    JButton yellowButton = new JButton("YELLOW");
    yellowButton.setForeground(new Color(204, 204, 0));

    yellowButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){


    JButton greenButton = new JButton("GREEN");
    greenButton.setForeground(new Color(0, 204, 0));

    greenButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){

    JButton redButton = new JButton("RED");
    redButton.setForeground(new Color(255, 0, 0));

    redButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){



Davide Lorenzo MARINO February 2016

Try to follow the Single Responsibility Principle.

It means that your class should change only for one reason.

Keeping all the code you wrote in only one class is breaking this principle. So split it in different classes each one doing only one thing.

Post Status

Asked in February 2016
Viewed 2,386 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life