JFrame错误中的透明背景

时间:2019-01-11 21:50:57

标签: java swing jframe paint

我正在制作一个简单的Java程序。现在,它只制作了一个8x8的Checkers字段。但是,启动时,我的JFrame程序背景就像在屏幕上拍张照片一样,并且不会改变。但是,我将其设置为白色。代码有问题还是其他错误?

我将背景设置为白色,但完全没有设置。都没有帮助。

package mainpackage;

import com.sun.prism.paint.*;

import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.io.File;

class Field extends JFrame {

    Field() //little constructor with no arguments
    {
        this(800, 800);
    }

    Field(int a, int b) //the most big constructor
    {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(Color.white);
        setLayout(new FlowLayout());
        setResizable(false);

        setSize(a, b);
    }

    void makeVisible() //making frame visible
    {
        setVisible(true);
    }

    public void makeInvisible() //making frame invisible
    {
        setVisible(false);
    }

    public void paint(Graphics g) //painting the board
    {
        for (int y = 0; y < 10; y++) {
            for (int x = 0; x < 10; x++) {

                g.setColor(getCurrentColor(x,y)); // get and set color of current square
                {
                    if (x % 2 == 0) // if x even
                    {
                        if (y % 2 == 0) {
                            g.setColor(Color.lightGray); // if x even y even make gray
                        } else {
                            g.setColor(Color.pink); // if x even and y odd make pink
                        }
                    } else { // if odd even
                        if (y % 2 == 0) {
                            g.setColor(Color.pink); // if x odd and y even make pink
                        } else {
                            g.setColor(Color.lightGray); // if x odd and y odd make gray
                        }
                    }
                }
                if(x>0&&x<9&&y>0&&y<9) { // only if 0 < x < 9 and 0 < y < 9
                    g.fillRect(50 + 60 * x, 50 + 60 * y, 60, 60); // fill current rectangle
                }
                else if(y==0&&x>0&&x<9)
                {

                    g.setColor(Color.black);
                    g.drawString(Integer.toString(x),50 + 60 * x, 50 + 60 * y);
                }
              /*  if (y == 2&& x== 1) {
                   *//* JLabel label = new JLabel();
                    label.setLocation(x*60, y*60);
                    label.setSize(60, 60);
                    label.setLayout(new GridBagLayout());
                    label.setIcon(new ImageIcon("C:/Users/Asus/Desktop/My Work/checkers/res/images/checkerimage.png"));
                    add(label);*//*
                }*/
            }
        }

    }
    private Color getCurrentColor(int x, int y) //set the color of current square
    {
        if (x % 2 == 0) // if x even
        {
            if (y % 2 == 0) {
                return Color.red;
            } else {
                return Color.blue;// if x even and y odd make blue
            }
        } else { // if odd even
            if (y % 2 == 0) {
                return Color.blue; // if x odd and y even make blue
            } else {
                return Color.red; // if x odd and y odd make red
            }
        }
    }

}

package mainpackage;

public class MainClass {

    public static void main(String[] args) throws InterruptedException {
        Field f1 = new Field();
        f1.makeVisible();

    }
}

Picture of the programm

The second picture

1 个答案:

答案 0 :(得分:1)

请先通读Painting in AWT and SwingPerforming Custom Painting,以更好地了解Swing中的绘画工作原理以及应该如何使用它。

两个基本问题:

  1. 覆盖JFrame的油漆
  2. 破坏绘画链(无法调用paint方法的super方法)

JFrame是一个复合组件,也就是说,它已经添加了许多组成其功能的组件,这些组件可能会干扰通过paint进行的绘制。

Lots of stuff here

绘画是一个复杂的过程,涉及许多步骤,这些步骤是通过一系列子方法委派的。除非您要承担所有责任,否则应在执行任何自定义绘画之前致电super.paintXxx

那么,答案是什么?两倍...

  • 以用户JPanel代替JFrame作为组件的基础
  • 覆盖paintComponent而不是paint(并在进行任何自定义绘画之前调用super.paintComponent

例如...

import java.awt.*;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test extends JFrame {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.getContentPane().setBackground(Color.RED);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Field());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Field extends JPanel {

        public Field() {
            setOpaque(false);
        }

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

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (int y = 0; y < 10; y++) {
                for (int x = 0; x < 10; x++) {

                    g2d.setColor(getCurrentColor(x, y)); // get and set color of current square
                    {
                        if (x % 2 == 0) // if x even
                        {
                            if (y % 2 == 0) {
                                g2d.setColor(Color.lightGray); // if x even y even make gray
                            } else {
                                g2d.setColor(Color.pink); // if x even and y odd make pink
                            }
                        } else { // if odd even
                            if (y % 2 == 0) {
                                g2d.setColor(Color.pink); // if x odd and y even make pink
                            } else {
                                g2d.setColor(Color.lightGray); // if x odd and y odd make gray
                            }
                        }
                    }
                    if (x > 0 && x < 9 && y > 0 && y < 9) { // only if 0 < x < 9 and 0 < y < 9
                        g2d.fillRect(50 + 60 * x, 50 + 60 * y, 60, 60); // fill current rectangle
                    } else if (y == 0 && x > 0 && x < 9) {

                        g2d.setColor(Color.black);
                        g2d.drawString(Integer.toString(x), 50 + 60 * x, 50 + 60 * y);
                    }
                    /*  if (y == 2&& x== 1) {
                     *//* JLabel label = new JLabel();
                                        label.setLocation(x*60, y*60);
                                        label.setSize(60, 60);
                                        label.setLayout(new GridBagLayout());
                                        label.setIcon(new ImageIcon("C:/Users/Asus/Desktop/My Work/checkers/res/images/checkerimage.png"));
                                        add(label);*//*
                                }*/
                }
            }
            g2d.dispose();
        }

        private Color getCurrentColor(int x, int y) //set the color of current square
        {
            if (x % 2 == 0) // if x even
            {
                if (y % 2 == 0) {
                    return Color.red;
                } else {
                    return Color.blue;// if x even and y odd make blue
                }
            } else { // if odd even
                if (y % 2 == 0) {
                    return Color.blue; // if x odd and y even make blue
                } else {
                    return Color.red; // if x odd and y odd make red
                }
            }
        }

    }
}

如果您确实想要透明的窗口,则应查看How to Create Translucent and Shaped Windows