Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6629685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:17:44+00:00 2026-05-25T22:17:44+00:00

Not sure if it will actually be more optimized, however What I am trying

  • 0

Not sure if it will actually be more optimized, however What I am trying to do is take all of this code and possibly split it up within different classes? So far it is all just one class but We have been leaning more and more to multiple class projects so I am trying to figure out how I can split this and actually have it help rather then just splitting stuff up to split it up. Any help would be great!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;


public class PaintProgram extends JPanel implements MouseListener,ActionListener 
{
  private int xX1, yY1 , xX2, yY2, choice ;

  public static void main(String [] args)
  {
      new PaintProgram();
  }

  PaintProgram()
  {


    JFrame frame = new JFrame("Paint Program");
    frame.setSize(1200, 800);
    frame.getContentPane().add(this);

    JButton button1 = new JButton("Clear");
    button1.addActionListener(this);
    JButton button2 = new JButton("Filled rectangle");
    button2.addActionListener(this);
    JButton button3 = new JButton("Filled oval");
    button3.addActionListener(this);
    JButton button4 = new JButton("Empty rectangle");
    button4.addActionListener(this);
    JButton button5 = new JButton("Empty oval");
    button5.addActionListener(this);
    JButton button6 = new JButton("Line");
    button6.addActionListener(this);


    this.add(button1); 
    this.add(button2);
    this.add(button3);
    this.add(button4);
    this.add(button5);
    this.add(button6);
    addMouseListener(this);

    frame.setVisible(true);     
}

public void paintComponent(Graphics g)
{ 
     super.paintComponent(g);  
     Graphics2D g2 = (Graphics2D)g;
     if(grid == null){
        int w = this.getWidth();
        int h = this.getHeight();
        grid = (BufferedImage)(this.createImage(w,h));
        gc = grid.createGraphics();
     }
     g2.drawImage(grid, null, 0, 0);
     check();
}
BufferedImage grid;
Graphics2D gc;


public void draw()
{
    Graphics2D g = (Graphics2D)getGraphics();
     int w = xX2 - xX1;
        if (w<0)
        w = w *(-1);

   int h = yY2-yY1;
        if (h<0)
        h=  h*(-1);

     switch(choice)
    {
        case 1:
            check();
            gc.setColor(Color.YELLOW);
            gc.drawRect(xX1, yY1, w, h);
            repaint();
            break;

        case 2:
            check();
            gc.setColor(Color.CYAN);
            gc.drawOval(xX1, yY1, w, h);
            repaint();
            break;

        case 3:
            check();
            gc.setColor(Color.ORANGE);
            gc.drawRect(xX1, yY1, w, h);
            gc.fillRect(xX1, yY1, w, h);
            repaint();
            break;

        case 4:
            check();
            gc.drawOval(xX1, yY1, w, h);
            gc.setColor(Color.PINK);
            gc.fillOval(xX1, yY1, w, h);
            repaint();
            break;  

        case 5:
            check();
            gc.setColor(Color.MAGENTA);
            gc.drawLine(xX1, yY1, xX2, yY2);
            repaint();
            break;

        case 6:
            //Acquire clear screen or gay
            break;   
    }
}

public void check()
{
    if (xX1 > xX2)
    {
        int z = 0;
        z = xX1;
        xX1 = xX2;
        xX2 =z;
    }
    if (yY1 > yY2)
    {
        int z = 0;
        z = yY1;
        yY1 = yY2;
        yY2 = z;
    }
}



public void actionPerformed(ActionEvent e)
{

if (e.getActionCommand().equals("Empty rectangle")) 
{         
  System.out.println("Empty Rectangle Has Been Selected~");
   choice = 1;

  }

if (e.getActionCommand().equals("Empty oval")) 
{         
 System.out.println("Empty Oval Has Been Selected!");
   choice = 2;
  }


if (e.getActionCommand().equals("Filled rectangle"))
{         
  System.out.println("Filled Rectangle Has Been Selected");
   choice = 3;
  }


if (e.getActionCommand().equals("Filled oval")) 
{         
 System.out.println("Filled Oval Has Been Selected");
   choice = 4;
  }


if (e.getActionCommand().equals("Line"))
{
    System.out.println("Draw Line Has Been Selected");
    choice = 5;
}

if (e.getActionCommand().equals("Clear"))
{         
 System.out.println("Clear All The Things!!!");
   choice = 6;
   repaint();
  }

 }

 public void mouseExited(MouseEvent evt){}
 public void mouseEntered(MouseEvent evt){}
 public void mouseClicked(MouseEvent evt){}
 public void mousePressed(MouseEvent evt)
 {

     xX1 = evt.getX();
     yY1= evt.getY();

   }
 public void mouseReleased(MouseEvent evt)
 {
     xX2 =evt.getX();
     yY2=evt.getY();
     draw();
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:17:44+00:00Added an answer on May 25, 2026 at 10:17 pm

    I would do it like this:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    
    
    public class PaintProgram extends JPanel implements MouseListener 
    {
        private int xX1, yY1 , xX2, yY2;
        private PaintOperation currentOperation;
        private void setCurrentOperation(PaintOperation po) { currentOperation = po; }
    
        private class PaintOperation {
            private int choice;
            public PaintOperation(final PaintProgram context, String text, int choice) {
                this.choice = choice;
                final JButton button = new JButton(text);
                button.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        context.setCurrentOperation(PaintOperation.this);
                        context.repaint();
                        System.out.println("PaintOperation: "+button.getText());
                    }
                });
                context.add(button);
            }
        }
    
        public static void main(String [] args)
        {
            new PaintProgram();
        }
    
        PaintProgram()
        {
            JFrame frame = new JFrame("Paint Program");
            frame.setSize(1200, 800);
            frame.getContentPane().add(this);
    
            new PaintOperation(this, "Empty rectangle", 1);
            new PaintOperation(this, "Empty oval", 2);
            new PaintOperation(this, "Filled rectangle", 3);
            new PaintOperation(this, "Filled oval", 4);
            new PaintOperation(this, "Line", 5);
            setCurrentOperation(new PaintOperation(this, "Clear", 6));
    
            addMouseListener(this);
    
            frame.setVisible(true);     
        }
    
        public void paintComponent(Graphics g)
        { 
            super.paintComponent(g);  
            Graphics2D g2 = (Graphics2D)g;
            if (grid == null) {
                int w = this.getWidth();
                int h = this.getHeight();
                grid = (BufferedImage)(this.createImage(w,h));
                gc = grid.createGraphics();
            }
            g2.drawImage(grid, null, 0, 0);
            check();
        }
        BufferedImage grid;
        Graphics2D gc;
    
    
        public void draw()
        {
            Graphics2D g = (Graphics2D)getGraphics();
            int w = xX2 - xX1;
            if (w<0)
                w = w *(-1);
    
            int h = yY2-yY1;
            if (h<0)
                h=  h*(-1);
    
            switch(currentOperation.choice)
            {
            case 1:
                check();
                gc.setColor(Color.YELLOW);
                gc.drawRect(xX1, yY1, w, h);
                repaint();
                break;
    
            case 2:
                check();
                gc.setColor(Color.CYAN);
                gc.drawOval(xX1, yY1, w, h);
                repaint();
                break;
    
            case 3:
                check();
                gc.setColor(Color.ORANGE);
                gc.drawRect(xX1, yY1, w, h);
                gc.fillRect(xX1, yY1, w, h);
                repaint();
                break;
    
            case 4:
                check();
                gc.drawOval(xX1, yY1, w, h);
                gc.setColor(Color.PINK);
                gc.fillOval(xX1, yY1, w, h);
                repaint();
                break;  
    
            case 5:
                check();
                gc.setColor(Color.MAGENTA);
                gc.drawLine(xX1, yY1, xX2, yY2);
                repaint();
                break;
    
            case 6:
                //Acquire clear screen or gay
                break;   
            }
        }
    
        public void check()
        {
            if (xX1 > xX2)
            {
                int z = 0;
                z = xX1;
                xX1 = xX2;
                xX2 =z;
            }
            if (yY1 > yY2)
            {
                int z = 0;
                z = yY1;
                yY1 = yY2;
                yY2 = z;
            }
        }
    
        public void mouseExited(MouseEvent evt){}
        public void mouseEntered(MouseEvent evt){}
        public void mouseClicked(MouseEvent evt){}
        public void mousePressed(MouseEvent evt)
        {
            xX1 = evt.getX();
            yY1= evt.getY();
        }
        public void mouseReleased(MouseEvent evt)
        {
            xX2 =evt.getX();
            yY2=evt.getY();
            draw();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not sure if this will be right place to ask the question,
I'm not sure how to approach this: I want a TreeView that will display
I'm not sure why, but this condition will never evaluate True for me. I'm
I'm not sure how password hashing works (will be implementing it later), but need
I'm not sure, will the visual c ++ compiler express edition work for compiling
I'm not sure really how to ask that question.. hopefully my explanation will clarify:
Not sure if this is possible or if I'm expressing correctly what I'm looking
Not sure how to ask a followup on SO, but this is in reference
I'm not sure whether this belongs on StackOverflow or on ServerFault, so I've picked
I'm trying to put together a plugins system with .NET, and I'm not sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.