更改 java 中已绘制图形对象的颜色 AWT 不工作

change color of already drawn Graphics object in java AWT not working

我在 window 中绘制对象后尝试更改颜色,这是我想出的,我是初学者所以这很乱。

import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.List;
public class TestGraphic extends Figure {

TestGraphic(){
    setSize(600,600);
    closeFrame();
}

public static void main(String[] args) {
    TestGraphic test = new TestGraphic();
    test.setVisible(true);

}
public void paint(Graphics gui) {
    drawComponent(gui);
    
}
public void drawComponent(Graphics gui) {
     gui.drawOval(108,110,200,200);
     gui.drawOval(160,150,20,20);
     gui.drawOval(240,150,20,20);
     gui.drawRect(160,220,100,40);
     Button btn = new Button("Change color");
     btn.setBounds(30,100,80,30);
     add(btn);
     changeColor(gui,btn);
    
     
}
@Override
void draw(Graphics gui) {
    // TODO Auto-generated method stub
    
}
@Override
public boolean contains(int x, int y) {
      
      return false;
}
 private void closeFrame(){
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
 }
 

 void changeColor(Graphics gui,Button btn) {
     List<Color> colorList;
     colorList = Arrays.asList(Color.black, Color.blue , Color.cyan, Color.red, Color.green, Color.magenta, Color.orange, Color.yellow);
      
      btn.addActionListener(new ActionListener() { 
             public void actionPerformed (ActionEvent e) {  
                 for(int i = 0;i < colorList.size(); i++) {
                    gui.setColor(colorList.get(i));
                }
                  }    
              }); 
     
    
}

}

class是一个抽象class,继承自java.awt.Frame 任何有关如何使此代码更清晰的评论将不胜感激,谢谢。

在开始像自定义绘画这样复杂的事情之前,请确保您对语言和 API 的基础知识有深入的了解

有关详细信息,请参阅 Creating a GUI With Swing, Performing Custom Painting and Painting in AWT and Swing

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Color desiredColor = Color.BLACK;
        private List<Color> avaliableColors = new ArrayList<>(16);

        public TestPane() {
            setLayout(new BorderLayout());
            avaliableColors.add(Color.BLACK);
            avaliableColors.add(Color.BLUE);
            avaliableColors.add(Color.CYAN);
            avaliableColors.add(Color.DARK_GRAY);
            avaliableColors.add(Color.GRAY);
            avaliableColors.add(Color.GREEN);
            avaliableColors.add(Color.LIGHT_GRAY);
            avaliableColors.add(Color.MAGENTA);
            avaliableColors.add(Color.ORANGE);
            avaliableColors.add(Color.PINK);
            avaliableColors.add(Color.RED);
            avaliableColors.add(Color.WHITE);
            avaliableColors.add(Color.YELLOW);

            JButton btn = new JButton("Change color");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Randomise the colors
                    Collections.shuffle(avaliableColors);
                    desiredColor = avaliableColors.get(0);
                    repaint();
                }
            });
            add(btn, BorderLayout.SOUTH);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(desiredColor);
            g2d.drawOval(108, 110, 200, 200);
            g2d.drawOval(160, 150, 20, 20);
            g2d.drawOval(240, 150, 20, 20);
            g2d.drawRect(160, 220, 100, 40);
            g2d.dispose();
        }

    }
}

由于您刚刚起步,我可能建议您从 JavaFX 开始,而不是

AWT

!!警告!!

没有人再使用 AWT,而且您不太可能在这里获得太多支持。这是从前面的示例转换而来的“基本”概念,请注意,默认情况下 AWT 不是双缓冲的

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame frame = new Frame();
                frame.add(new TestCanvas());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestCanvas extends Container {

        private Color desiredColor = Color.BLACK;
        private List<Color> avaliableColors = new ArrayList<>(16);

        public TestCanvas() {
            setLayout(new BorderLayout());
            avaliableColors.add(Color.BLACK);
            avaliableColors.add(Color.BLUE);
            avaliableColors.add(Color.CYAN);
            avaliableColors.add(Color.DARK_GRAY);
            avaliableColors.add(Color.GRAY);
            avaliableColors.add(Color.GREEN);
            avaliableColors.add(Color.LIGHT_GRAY);
            avaliableColors.add(Color.MAGENTA);
            avaliableColors.add(Color.ORANGE);
            avaliableColors.add(Color.PINK);
            avaliableColors.add(Color.RED);
            avaliableColors.add(Color.WHITE);
            avaliableColors.add(Color.YELLOW);

            JButton btn = new JButton("Change color");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Randomise the colors
                    Collections.shuffle(avaliableColors);
                    desiredColor = avaliableColors.get(0);
                    repaint();
                }
            });
            add(btn, BorderLayout.SOUTH);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(desiredColor);
            g2d.drawOval(108, 110, 200, 200);
            g2d.drawOval(160, 150, 20, 20);
            g2d.drawOval(240, 150, 20, 20);
            g2d.drawRect(160, 220, 100, 40);
            g2d.dispose();
        }

    }
}