Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在背景上绘制组件_Java_Swing_Paint_Graphics2d - Fatal编程技术网

Java 在背景上绘制组件

Java 在背景上绘制组件,java,swing,paint,graphics2d,Java,Swing,Paint,Graphics2d,我画一些图像有点问题。 我使用JDialog显示背景,使用单独的类显示卡片(使用精灵) 背景显示良好,但JPanel没有 这是我的密码: public Main(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); //Call the board to draw cards Board plateau = new Board(); this.a

我画一些图像有点问题。 我使用JDialog显示背景,使用单独的类显示卡片(使用精灵)

背景显示良好,但JPanel没有

这是我的密码:

public Main(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();

    //Call the board to draw cards
    Board plateau = new Board(); 

    this.add(plateau);
}

/**
 * Paint the background
 *
 * @param g
 */
@Override
public void paint(Graphics g) {  
    try {      
        Graphics2D g2 = (Graphics2D) g;

        this.background_image = ImageIO.read(new File(this.background));
        Graphics2D big = this.background_image.createGraphics();
        Rectangle rectangle = new Rectangle(0, 0, 20, 20);
        g2.setPaint(new TexturePaint(this.background_image, rectangle));

        Rectangle rect = new Rectangle(0, 0, this.getWidth(), this.getHeight());
        g2.fill(rect);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}
以及应该抽牌的班级:

@Override  
public void paint(Graphics g) {
    try {
        this.image = ImageIO.read(new File("Ressources/images/cardsprite.gif"));

        //4 lines
        for (int i = 0; i < 4; i++) {
            //13 rows
            for (int j = 0; j < 13; j++) {
                //Split one card
                BufferedImage temp = this.image.getSubimage(j * this.CARD_WIDTH,
                        i * this.CARD_HEIGHT, this.CARD_WIDTH, this.CARD_HEIGHT);

                g.drawImage(temp, j * this.CARD_WIDTH,
                        i * this.CARD_HEIGHT, this);
            }
        }


    } catch (IOException ex) {
        Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
    }
@覆盖
公共空间涂料(图g){
试一试{
this.image=ImageIO.read(新文件(“resources/images/cardsprite.gif”);
//4行
对于(int i=0;i<4;i++){
//13排
对于(int j=0;j<13;j++){
//拆分一张卡
BuffereImage temp=this.image.getSubimage(j*this.CARD_宽度,
i*this.CARD\u高度,this.CARD\u宽度,this.CARD\u高度);
g、 drawImage(温度,j*this.CARD_宽度,
i*this.CARD\u高度,this);
}
}
}捕获(IOEX异常){
Logger.getLogger(Board.class.getName()).log(Level.SEVERE,null,ex);
}
如果我将cards drawing类放在Main的paint方法中,效果会很好

我错过什么了吗


谢谢你

本质上,你正在破坏绘画链,这阻止了你的“主”类绘画它的任何孩子

首先看一看油漆工艺的概述

您也不应该重写
paint
,而是应该从扩展
JComponent
的内容重写
paintComponent

请查看以了解更多详细信息

基本上,您应该创建一个“背景”面板,负责绘制背景,然后在上面添加负责绘制卡片的组件,确保其透明(
setOpaque(false)
),以便背景显示出来

如果你没有做任何动态效果,你甚至可以在背景窗格中使用
JLabel

您应该避免在任何
paintXxx
方法中执行任何可能耗时的操作,如加载图像。应优化绘制过程,使其尽可能快地运行