Java 每次使用循环存储矩形大小

Java 每次使用循环存储矩形大小,java,swing,colors,actionlistener,event-listener,Java,Swing,Colors,Actionlistener,Event Listener,创建了一个类似于paint的程序,其中有一个矩形,其在屏幕内的移动可通过“w、a、s、d”键控制,其大小可使用鼠标上的滚动条增减。还有几个不同颜色的按钮,当按下时,这些按钮会用各自的颜色填充矩形。 我还试图用这个矩形来画画,即当我按下空格键时,我想填充在指定区域选择的颜色。现在即使我能做到。我做的下一个动作,即按下“w,a,s,d”键或滚动条,颜色消失。现在我知道必须以某种方式保存color或fillRect(),以便下一个操作不会影响它,但我尝试了几种方法,但都没有成功。我尝试使用循环来存储矩

创建了一个类似于paint的程序,其中有一个矩形,其在屏幕内的移动可通过“w、a、s、d”键控制,其大小可使用鼠标上的滚动条增减。还有几个不同颜色的按钮,当按下时,这些按钮会用各自的颜色填充矩形。 我还试图用这个矩形来画画,即当我按下空格键时,我想填充在指定区域选择的颜色。现在即使我能做到。我做的下一个动作,即按下“w,a,s,d”键或滚动条,颜色消失。现在我知道必须以某种方式保存color或fillRect(),以便下一个操作不会影响它,但我尝试了几种方法,但都没有成功。我尝试使用循环来存储矩形的值,这样每次重新绘制时,它都会重新绘制循环中包含的所有值。但这不起作用。如果由于某些计算错误而无法正常工作,请说明,如果没有,并且这种使用循环的方法是错误的,请建议其他简单的方法

所以基本上我想做的是,当按下空格键时,我想要那个颜色的矩形的印记,当我移开这个矩形的时候。我希望那个印记留在那里

注意:我知道我的代码很差,并且不是所有的代码都可行,但是我正在用我们所学的知识来做这个项目,我希望你能提供解决方案

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

public class Animation extends Frame implements KeyListener, MouseWheelListener, ActionListener {


    int x,y,a,b;
    char choice1;
    int draw=1;
    int defaultValue=0;
    int n=0;
    int color1,color2,color3;
    Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11;

    Animation() {
        setSize(1000, 1000);
        setVisible(true);
        x = 500;
        y = 500;
        a = 20;
        b = 50;

        JPanel panel = new JPanel();
        button1 = new Button("Black");
        button2 = new Button("Blue");
        button3 = new Button("Green");
        button4 = new Button("Orange");
        button5 = new Button("Red");
        button6 = new Button("Yellow");
        button7 = new Button("Gray");
        button8 = new Button("Cyan");
        button9 = new Button("Magenta");
        button10 = new Button("Pink");
        button11 = new Button("Default");

        panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);panel.add(button5);
        panel.add(button6);panel.add(button7);panel.add(button8);panel.add(button9);panel.add(button10);
        panel.add(button11);


        add(panel);

//        button1.setBounds(50,680,50,20); button2.setBounds(120,680,50,20);
//        button3.setBounds(190,680,50,20); button4.setBounds(260,680,50,20);
//        button5.setBounds(330,680,50,20); button6.setBounds(400,680,50,20);
//        button7.setBounds(470,680,50,20); button8.setBounds(540,680,50,20);
//        button9.setBounds(610,680,50,20); button10.setBounds(680,680,50,20);

        button1.setFocusable(false);
        button2.setFocusable(false);
        button3.setFocusable(false);
        button4.setFocusable(false);
        button5.setFocusable(false);
        button6.setFocusable(false);
        button7.setFocusable(false);
        button8.setFocusable(false);
        button9.setFocusable(false);
        button10.setFocusable(false);
        button11.setFocusable(false);

        button1.addActionListener(this);button2.addActionListener(this);
        button3.addActionListener(this);button4.addActionListener(this);
        button5.addActionListener(this);button6.addActionListener(this);
        button7.addActionListener(this);button8.addActionListener(this);
        button9.addActionListener(this);button10.addActionListener(this);
        button11.addActionListener(this);
        addKeyListener(this);
        addMouseWheelListener(this);
        addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            @Override
            public void windowClosed(WindowEvent e) {
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }
        });
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyPressed(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
        choice1 = e.getKeyChar();
        if (choice1 == 'w') {
            y = y - 10;
        }
        if (choice1 == 's') {
            y = y + 10;
        }
        if (choice1 == 'a') {
            x = x - 10;
        }
        if (choice1 == 'd') {
            x = x + 10;
        }
        if(choice1 == ' '){
            draw=2;
        }
        repaint();
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        double p = e.getPreciseWheelRotation();
        if(p>0){
            a=a+5;
            b=b+5;
        } else{
            a=a-5;
            b=b-5;
        }
        repaint();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        defaultValue = 0;
        if(e.getActionCommand().equals("Black")){
            color1 = 0;
            color2 = 0;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Blue")){
            color1 = 0;
            color2 = 0;
            color3 = 255;
        }
        if(e.getActionCommand().equals("Green")){
            color1 = 0;
            color2 = 255;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Orange")){
            color1 = 255;
            color2 = 165;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Red")){
            color1 = 255;
            color2 = 0;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Yellow")){
            color1 = 255;
            color2 = 255;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Gray")){
            color1 = 169;
            color2 = 169;
            color3 = 169;
       }
        if(e.getActionCommand().equals("Cyan")){
            color1 = 0;
            color2 = 255;
            color3 = 255;
        }
        if(e.getActionCommand().equals("Magenta")){
            color1 = 255;
            color2 = 0;
            color3 = 255;
        }
        if(e.getActionCommand().equals("Pink")){
            color1 = 255;
            color2 = 192;
            color3 = 203;
        }
        if(e.getActionCommand().equals("Default")){
            defaultValue = 1;
        }
        repaint();

    }

    public void paint(Graphics g) {
        super.paint(g);
        if(draw==1) {

            if(defaultValue == 1){
                g.setColor(new Color(color1,color2,color3));
                g.drawRect(x, y, a, b);
            }else{
                g.drawRect(x, y, a, b);
                g.setColor(new Color(color1,color2,color3));
                g.fillRect(x,y,a,b);
            }


        }
        if(draw==2){
            g.setColor(new Color(color1,color2,color3));
            int[] temp1 = new int[50];
            temp1[n] = x;
            int[] temp2 = new int[50];
            temp2[n] = y;
            int[] temp3 = new int[50];
            temp3[n] = a;
            int[] temp4 = new int[50];
            temp4[n] = b;


            n++;
            for (int i=0;i<n;i++){
                g.drawRect(x, y, a, b);
                g.fillRect(temp1[i],temp2[i],temp3[i],temp4[i]);
            }
            draw=1;
        }
    }

    public static void main(String[] args) {
        Animation animation = new Animation();

    }

}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类动画扩展框架实现KeyListener、MouseWheelListener、ActionListener{
int x,y,a,b;
字符选择1;
int-draw=1;
int defaultValue=0;
int n=0;
int color1、color2、color3;
按钮1、按钮2、按钮3、按钮4、按钮5、按钮6、按钮7、按钮8、按钮9、按钮10、按钮11;
动画(){
设置大小(10001000);
setVisible(真);
x=500;
y=500;
a=20;
b=50;
JPanel面板=新的JPanel();
按钮1=新按钮(“黑色”);
按钮2=新按钮(“蓝色”);
按钮3=新按钮(“绿色”);
按钮4=新按钮(“橙色”);
按钮5=新按钮(“红色”);
按钮6=新按钮(“黄色”);
按钮7=新按钮(“灰色”);
按钮8=新按钮(“青色”);
按钮9=新按钮(“洋红色”);
按钮10=新按钮(“粉色”);
button11=新按钮(“默认”);
panel.add(按钮1);panel.add(按钮2);panel.add(按钮3);panel.add(按钮4);panel.add(按钮5);
panel.add(按钮6);panel.add(按钮7);panel.add(按钮8);panel.add(按钮9);panel.add(按钮10);
面板。添加(按钮11);
添加(面板);
//按钮1.立根(50680,50,20);按钮2.立根(120680,50,20);
//按钮3.立根(190680,50,20);按钮4.立根(260680,50,20);
//按钮5.立根(330680,50,20);按钮6.立根(400680,50,20);
//按钮7.立根(470680,50,20);按钮8.立根(540680,50,20);
//按钮9.立根(610680,50,20);按钮10.立根(680680,50,20);
按钮1.设置聚焦(假);
按钮2.设置聚焦(假);
按钮3.设置聚焦(假);
按钮4.设置聚焦(假);
按钮5.设置聚焦(假);
按钮6.设置聚焦(假);
按钮7.设置聚焦(假);
按钮8.设置聚焦(假);
按钮9.设置聚焦(假);
按钮10。设置聚焦(假);
按钮11.设置聚焦(假);
按钮1.addActionListener(此);按钮2.addActionListener(此);
button3.addActionListener(此);button4.addActionListener(此);
按钮5.addActionListener(此);按钮6.addActionListener(此);
button7.addActionListener(此);button8.addActionListener(此);
按钮9.addActionListener(此);按钮10.addActionListener(此);
按钮11.addActionListener(此);
addKeyListener(此);
addMouseWheelListener(此);
addWindowListener(新建WindowListener(){
@凌驾
公共无效窗口已打开(WindowEvent e){
}
@凌驾
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
@凌驾
公共无效窗口关闭(WindowEvent e){
}
@凌驾
公共无效窗口图标化(WindowEvent e){
}
@凌驾
公共无效窗口取消确认(WindowEvent e){
}
@凌驾
公共无效窗口已激活(WindowEvent e){
}
@凌驾
公共无效窗口已停用(WindowEvent e){
}
});
}
@凌驾
public void keyTyped(KeyEvent e){
}
@凌驾
按下公共无效键(按键事件e){
}
@凌驾
公共无效密钥已释放(密钥事件e){
choice1=e.getKeyChar();
如果(选项1='w'){
y=y-10;
}
如果(选项1='s'){
y=y+10;
}
如果(选项1='a'){
x=x-10;
}
如果(选项1='d'){
x=x+10;
}
如果(选项1=''){
抽签=2;
}
重新油漆();
}
@凌驾
已移动公共无效鼠标滚轮(鼠标滚轮事件e){
双p=e.GetPreciseHeelRotation();
如果(p>0){
a=a+5;
b=b+5;
}否则{
a=a-5;
b=b-5;
}
重新油漆();
}
@凌驾
已执行的公共无效操作(操作事件e){
默认值=0;
如果(例如getActionCommand().equals(“黑色”)){
颜色1=0;
颜色2=0;
颜色3=0;
}
如果(例如getActionCommand().equals(“蓝色”)){
颜色1=0;
颜色2=0;
颜色3=255;
}
如果(例如getActionCommand().equals(“绿色”)){
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

public class Animation extends Frame implements KeyListener, MouseWheelListener,MouseListener, ActionListener {


    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    int x,y,a,b;
    char choice1;
    int defaultValue=0;
    int number=0;
    Color cursorColor = Color.BLACK;
    boolean isImprint = false;
    private Rectangle baseRect =  null;
    private List<Rectangle> lstImprints = new ArrayList<Rectangle>();
    Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11;
    

    Animation() {
        System.out.println("animation ");
        setSize(1000, 1000);
        setVisible(true);
        x = 500;
        y = 500;
        a = 20;
        b = 50;
        baseRect = new Rectangle(x, y, a, b, cursorColor);
        JPanel panel = new JPanel();
        button1 = new Button("Black");
        button2 = new Button("Blue");
        button3 = new Button("Green");
        button4 = new Button("Orange");
        button5 = new Button("Red");
        button6 = new Button("Yellow");
        button7 = new Button("Gray");
        button8 = new Button("Cyan");
        button9 = new Button("Magenta");
        button10 = new Button("Pink");
        button11 = new Button("Default");

        panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);panel.add(button5);
        panel.add(button6);panel.add(button7);panel.add(button8);panel.add(button9);panel.add(button10);
        panel.add(button11);


        add(panel);

        //        button1.setBounds(50,680,50,20); button2.setBounds(120,680,50,20);
        //        button3.setBounds(190,680,50,20); button4.setBounds(260,680,50,20);
        //        button5.setBounds(330,680,50,20); button6.setBounds(400,680,50,20);
        //        button7.setBounds(470,680,50,20); button8.setBounds(540,680,50,20);
        //        button9.setBounds(610,680,50,20); button10.setBounds(680,680,50,20);

        button1.setFocusable(false);
        button2.setFocusable(false);
        button3.setFocusable(false);
        button4.setFocusable(false);
        button5.setFocusable(false);
        button6.setFocusable(false);
        button7.setFocusable(false);
        button8.setFocusable(false);
        button9.setFocusable(false);
        button10.setFocusable(false);
        button11.setFocusable(false);

        button1.addActionListener(this);button2.addActionListener(this);
        button3.addActionListener(this);button4.addActionListener(this);
        button5.addActionListener(this);button6.addActionListener(this);
        button7.addActionListener(this);button8.addActionListener(this);
        button9.addActionListener(this);button10.addActionListener(this);
        button11.addActionListener(this);
        this.addMouseListener(this);
        addKeyListener(this);
        addMouseWheelListener(this);
        addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            @Override
            public void windowClosed(WindowEvent e) {
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }
        });
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyPressed(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
        choice1 = e.getKeyChar();
        if (choice1 == 'w') {
            //y = y - 10;
            baseRect.setY(baseRect.getY()-10);
        }
        if (choice1 == 's') {
            //y = y + 10;
            baseRect.setY(baseRect.getY()+10);
        }
        if (choice1 == 'a') {
            //x = x - 10;
            baseRect.setX(baseRect.getX()-10);
        }
        if (choice1 == 'd') {
            //x = x + 10;
            baseRect.setX(baseRect.getX()-10);
        }
        if(choice1 == ' '){
            isImprint = true;
        }

        System.out.println("choice 1"+choice1);
        repaint();
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        double p = e.getPreciseWheelRotation();
        if(p>0){
            baseRect.setHeight(baseRect.getHeight()+5);
            //b=b+5;
            baseRect.setWidth(baseRect.getWidth()+5);
        } else{
            baseRect.setHeight(baseRect.getHeight()-5);
            baseRect.setWidth(baseRect.getWidth()-5);
            //b=b-5;
        }
        repaint();
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        System.out.println("action performed");
        defaultValue = 0;
        if(e.getActionCommand().equals("Black")){
            cursorColor = Color.BLACK;
        }
        if(e.getActionCommand().equals("Blue")){
            cursorColor = Color.BLUE;
        }
        if(e.getActionCommand().equals("Green")){
            cursorColor = Color.GREEN;
        }
        if(e.getActionCommand().equals("Orange")){
            cursorColor = Color.ORANGE;
        }
        if(e.getActionCommand().equals("Red")){
            cursorColor = Color.RED;
        }
        if(e.getActionCommand().equals("Yellow")){
            cursorColor = Color.YELLOW;
        }
        if(e.getActionCommand().equals("Gray")){
            cursorColor = Color.GRAY;
        }
        if(e.getActionCommand().equals("Cyan")){
            cursorColor = Color.CYAN;
        }
        if(e.getActionCommand().equals("Magenta")){
            cursorColor = Color.MAGENTA;
        }
        if(e.getActionCommand().equals("Pink")){
            cursorColor = Color.PINK;
        }
        if(e.getActionCommand().equals("Default")){
            defaultValue = 1;
        }
        repaint();

    }

    public void paint(Graphics g) {

        System.out.println("paint ");
        super.paint(g);
            
        g.setColor(cursorColor);
        g.drawRect(baseRect.getX(),baseRect.getY(),baseRect.getHeight(),baseRect.getWidth());
        g.fillRect(baseRect.getX(),baseRect.getY(),baseRect.getHeight(),baseRect.getWidth());
        
        System.out.println(lstImprints.size());
        for(Rectangle rect : lstImprints){
            g.setColor(rect.getColor());
            g.drawRect(rect.getX(),rect.getY(),rect.getHeight(),rect.getWidth());
            g.fillRect(rect.getX(),rect.getY(),rect.getHeight(),rect.getWidth());
        }
/*      if(defaultValue == 1){
            g.setColor(cursorColor);
            g.drawRect(rect.getX(),rect.getY(),rect.getHeight(),rect.getWidth());

        }else{
            g.drawRect(x, y, a, b);
            g.setColor(cursorColor);                
            g.fillRect(x,y,a,b);

        }*/
    }

    public static void main(String[] args) {
        Animation animation = new Animation();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        if(isImprint){
            Point point = e.getLocationOnScreen();
            Rectangle rectangle = new Rectangle(Double.valueOf(point.getX()).intValue(), Double.valueOf(point.getY()).intValue(), baseRect.getHeight(), baseRect.getWidth(),cursorColor);
            lstImprints.add(rectangle);
            isImprint = false;
            repaint();
        }

    }

}
import java.awt.Color;

public class Rectangle {
    
    private int x;
    
    private int y;
    
    private int height;
    
    private int width;
    
    private Color color = null;

    public Rectangle(int x, int y, int height, int width, Color color) {
        super();
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
        this.color = color;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}