如何优化?/类拆分

发布于 2024-12-07 04:07:37 字数 4365 浏览 1 评论 0原文

不确定它是否实际上会更加优化,但是我想做的是获取所有这些代码并可能将其拆分到不同的类中?到目前为止,这只是一门课程,但我们越来越倾向于多个课程项目,所以我试图弄清楚如何分割它,并真正得到它的帮助,而不是只是将东西分割开来。任何帮助都会很棒!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神爱温柔 2024-12-14 04:07:37

我会这样做:

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

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();
    }
}
倾`听者〃 2024-12-14 04:07:37

您可以做的第一件事是观察每个可选选项都具有以下共同特征:

  1. 它们都有一个带有自定义文本的按钮,它们都添加到主面板并将其注册为侦听器。
  2. 他们都画一些东西,但他们的做法略有不同。
  3. 它们都有助于 actionPerformed()

使用此观察结果,您可以创建一个执行 1 和 3 以及 2 的 check()/repaint() 部分的抽象 DrawOperation 类。还应该为绘制操作的变量部分定义一个抽象方法。然后,您可以扩展此类,为每个操作提供正确的 draw() 方法。 (高级课程:了解匿名内部类如何工作并创建 AIC 操作。)

The very first thing you can do is to observe that every selectable option has the following common traits:

  1. They all have a button with a custom text, they;re all added to the main panel and register it as a listener.
  2. They all draw something, but they do it slightly differently.
  3. They all contribute to actionPerformed()

Using this observation you can create an abstract DrawOperation class that performs 1 and 3 and the check()/repaint() part of 2. It should also define an abstract method for the variable part of the draw operation. Then you can extend this class to provide the correct draw() method for each operation. (Advanced lesson: look up how anonymous inner classes work and create your operations as AICs.)

似最初 2024-12-14 04:07:37

我可能会尝试首先将该 case 语句分解到类层次结构中。

I'd probably try to bust out that case statement into a class hierarchy first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文