添加改变绘图颜色的按钮

Add buttons that change color of drawing

这幅画基本上是两页纸 window,上面有天空和太阳,就好像您正在眺望 window。 我需要在白天和晚上添加按钮。通过单击每个按钮,天空的颜色变为深蓝色和浅蓝色,太阳的颜色相应地变为月亮的颜色。我已经添加了按钮并且它们出现了,但我似乎无法理解我应该放置 actionListener 的方式以及如何访问我的 g2 变量以更改这些颜色。

public class DrawingOne extends JFrame{
    public DrawingOne() {
        Container container = getContentPane();
        container.setBackground(new Color(224, 196, 214));
        container.setLayout(new BorderLayout(20, 20));
        JButton j1 = new JButton("Day");
        JButton j2 = new JButton("Night");
        container.add(j1, BorderLayout.SOUTH);
        container.add(j2, BorderLayout.NORTH);
        setSize(DEFAULT_WITDH, DEFAULT_HEIGHT);
        DrawComponent c = new DrawComponent();
        add(c);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static final int DEFAULT_WITDH = 700;
    public static final int DEFAULT_HEIGHT = 500;
}
class DrawComponent extends JComponent {

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        //the base for window
        Rectangle2D.Double rectangle2Drect2 = new Rectangle2D.Double(37, 26, 458, 280);
        g2.setStroke(new BasicStroke(3));
        g2.setColor(Color.black);
        g2.draw(rectangle2Drect2);
        g2.setColor(Color.white);
        g2.fill(rectangle2Drect2);

        //left half-window
        rectangle2Drect2 = new Rectangle2D.Double(51, 40, 160, 250);
        g2.setStroke(new BasicStroke(3));
        g2.setColor(Color.black);
        g2.draw(rectangle2Drect2);
        g2.setColor(new Color(135, 206, 235));
        g2.fill(rectangle2Drect2);

        //right half-window
        rectangle2Drect2 = new Rectangle2D.Double(230, 40, 250, 250);
        g2.setStroke(new BasicStroke(3));
        g2.setColor(Color.black);
        g2.draw(rectangle2Drect2);
        g2.setColor(new Color(135, 206, 235));
        g2.fill(rectangle2Drect2);


        //основа ручки
        rectangle2Drect2 = new Rectangle2D.Double(215, 140, 10, 10);
        g2.setStroke(new BasicStroke(2));
        g2.setColor(Color.black);
        g2.draw(rectangle2Drect2);
        g2.setColor(Color.white);
        g2.fill(rectangle2Drect2);

        //ручка
        rectangle2Drect2 = new Rectangle2D.Double(217, 142, 4, 30);
        g2.setStroke(new BasicStroke(2));
        g2.setColor(Color.black);
        g2.draw(rectangle2Drect2);
        g2.setColor(Color.white);
        g2.fill(rectangle2Drect2);

        //the sun
        g2.setColor(Color.getHSBColor(197.4F, 42.6F, 92.2F));
        g2.fillOval(100, 60, 80, 80);

        //горшок
        int[] xpoints = {230 + 60, 300 + 60, 285 + 61, 244 + 60};
        int[] ypoints = {240, 240, 290, 290};
        int npoints = 4;
        g2.setColor(new Color(172, 86, 30));
        g2.fillPolygon(xpoints, ypoints, npoints);

        //ствол цветка
        g2.setStroke(new BasicStroke(5));
        g2.setColor(new Color(0, 128, 0));
        g2.drawLine(325, 200, 325, 238);

        //лепестки
        g2.setColor(new Color(255, 192, 203));

        g2.fillOval(317, 200, 22, 22);
        g2.fillOval(317, 175, 22, 22);
        g2.fillOval(317 - 15, 190, 22, 22);
        g2.fillOval(317 + 13, 190, 22, 22);
        //  g2.fillOval(317,190,22,22);

        //центр цветка
        g2.setColor(new Color(255, 165, 0));
        g2.fillOval(317, 190, 15, 15);
    }
    public static void main(String[] args) {
        DrawingOne frame = new DrawingOne();
    }
}

我用你的 GUI 做了一个夜空。

我必须对您的代码进行大量更改才能使其正常工作。

  1. 我必须将 JComponent class 设为内部 class,以便它可以引用 DrawingOne class 变量。

  2. 我不得不在 DrawingOne class 中移动 main 方法,这样我才能启动应用程序。

  3. 我通过调用 SwingUtilities invokeLater 方法启动了 Java 应用程序。此方法确保 Swing 组件在 Event Dispatch Thread.

    上创建和执行
  4. 我在 DrawingOne 构造函数中重新排列了您的语句的顺序,因此它们更有意义。我将绘图面板设为您喜欢的尺寸,而不是 JFrame。重要的是绘图面板的大小,而不是 JFrame 大小。

  5. 我制作了天空和太阳的颜色 DrawingOne class 变量,所以它们可以改变。我写了几个方法来改变颜色。

  6. 我写了一个 ActionListener class 可以根据您选择的按钮改变颜色 left-click。

这是完整的可运行代码。

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class DrawingOne extends JFrame {

    private static final long serialVersionUID = 1L;

    public static final int DEFAULT_WIDTH = 700;
    public static final int DEFAULT_HEIGHT = 500;

    public Color sunColor;
    public Color skyColor;
    
    public DrawComponent c;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DrawingOne();
            }
        });
    }

    public DrawingOne() {
        setDaySky();

        Container container = getContentPane();
        container.setBackground(new Color(224, 196, 214));
        container.setLayout(new BorderLayout(20, 20));
        container.setPreferredSize(
                new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
        
        ButtonListener listener = new ButtonListener();

        JButton j1 = new JButton("Day");
        j1.addActionListener(listener);
        JButton j2 = new JButton("Night");
        j2.addActionListener(listener);
        container.add(j1, BorderLayout.SOUTH);
        container.add(j2, BorderLayout.NORTH);

        c = new DrawComponent();
        container.add(c, BorderLayout.CENTER);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void setNightSky() {
        this.sunColor = Color.getHSBColor(197.4F, 42.6F, 92.2F);
        this.skyColor = new Color(7, 78, 107);
    }

    public void setDaySky() {
        this.sunColor = Color.getHSBColor(197.4F, 42.6F, 92.2F);
        this.skyColor = new Color(135, 206, 235);
    }

    public class DrawComponent extends JComponent {

        private static final long serialVersionUID = 1L;

        @Override
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            // the base for window
            Rectangle2D.Double rectangle2Drect2 = 
                    new Rectangle2D.Double(37, 26, 458, 280);
            g2.setStroke(new BasicStroke(3));
            g2.setColor(Color.black);
            g2.draw(rectangle2Drect2);
            g2.setColor(Color.white);
            g2.fill(rectangle2Drect2);

            // left half-window
            rectangle2Drect2 = 
                    new Rectangle2D.Double(51, 40, 160, 250);
            g2.setStroke(new BasicStroke(3));
            g2.setColor(Color.black);
            g2.draw(rectangle2Drect2);
            g2.setColor(skyColor);
            g2.fill(rectangle2Drect2);

            // right half-window
            rectangle2Drect2 = 
                    new Rectangle2D.Double(230, 40, 250, 250);
            g2.setStroke(new BasicStroke(3));
            g2.setColor(Color.black);
            g2.draw(rectangle2Drect2);
            g2.setColor(skyColor);
            g2.fill(rectangle2Drect2);

            // основа ручки
            rectangle2Drect2 = 
                    new Rectangle2D.Double(215, 140, 10, 10);
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.black);
            g2.draw(rectangle2Drect2);
            g2.setColor(Color.white);
            g2.fill(rectangle2Drect2);

            // ручка
            rectangle2Drect2 = 
                    new Rectangle2D.Double(217, 142, 4, 30);
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.black);
            g2.draw(rectangle2Drect2);
            g2.setColor(Color.white);
            g2.fill(rectangle2Drect2);

            // the sun
            g2.setColor(sunColor);
            g2.fillOval(100, 60, 80, 80);

            // горшок
            int[] xpoints = { 230 + 60, 300 + 60, 285 + 61, 244 + 60 };
            int[] ypoints = { 240, 240, 290, 290 };
            int npoints = 4;
            g2.setColor(new Color(172, 86, 30));
            g2.fillPolygon(xpoints, ypoints, npoints);

            // ствол цветка
            g2.setStroke(new BasicStroke(5));
            g2.setColor(new Color(0, 128, 0));
            g2.drawLine(325, 200, 325, 238);

            // лепестки
            g2.setColor(new Color(255, 192, 203));

            g2.fillOval(317, 200, 22, 22);
            g2.fillOval(317, 175, 22, 22);
            g2.fillOval(317 - 15, 190, 22, 22);
            g2.fillOval(317 + 13, 190, 22, 22);
            // g2.fillOval(317,190,22,22);

            // центр цветка
            g2.setColor(new Color(255, 165, 0));
            g2.fillOval(317, 190, 15, 15);
        }

    }

    public class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            if (button.getText().equals("Night")) {
                setNightSky();
            } else {
                setDaySky();
            }
            c.repaint();
        }

    }

}