创建了一个类似于paint的程序,其中有一个矩形,其在屏幕内的移动可以通过“w,a,s,d”键控制,其大小可以使用鼠标上的滚动条增减。还有几个不同颜色的按钮,当按下时,这些按钮会用各自的颜色填充矩形形状。我也试着用这个矩形来画画,当我按下空格键时,我想填充指定区域所选的颜色。即使我能做到。我做的下一个动作是:要么按“w,a,s,d”键,要么按滚动条,颜色消失。现在我知道必须以某种方式保存colour或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();
}
}
1条答案
按热度按时间6rvt4ljy1#
我不是一个swingMaven,但我试图修复你的代码,我认为它的工作。
进行了以下更改:
存储所有压印图像的列表(通过输入“空格键”),这将删除代码中的整数数组。
java,值对象(添加在此代码底部)
为moue-pressed动作添加了mouselistener。
对颜色组件使用颜色常量而不是int值。
基本上,点击空格键会将当前矩形添加到列表中,稍后可以重新绘制。