添加更改图形颜色的按钮

时间:2020-10-30 12:24:40

标签: java swing graphics jframe jcomponent

该图基本上是一个带有天空和阳光的两叶窗,就像您正在看窗外一样。 我需要添加按钮白天和黑夜。通过单击每个按钮,天空的颜色变为深蓝色和浅蓝色,太阳的颜色相应地变为月亮的颜色。我已经添加了按钮,它们出现了,但是我似乎无法将我的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();
    }
}

1 个答案:

答案 0 :(得分:0)

我通过您的GUI制作了夜空。

Night Sky GUI

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

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

  2. 我必须在main类中移动DrawingOne方法,以便启动该应用程序。

  3. 我通过调用SwingUtilities invokeLater方法来启动Java应用程序。此方法可确保在Event Dispatch Thread上创建并执行Swing组件。

  4. 我在DrawingOne构造函数中重新排列了语句的顺序,因此它们更有意义。我将绘图面板设置为您的首选尺寸,而不是JFrame。绘图面板的大小很重要,而不是JFrame大小。

  5. 我设置了天空和太阳颜色DrawingOne类变量,因此可以对其进行更改。我写了几种改变颜色的方法。

  6. 我编写了一个ActionListener类,该类根据您的左键单击来更改颜色。

这是完整的可运行代码。

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

    }

}