在jpanel中paint一些东西,把jpanel加到一个jframe中,如何设置显示出来的背景颜色(急)

时间:2022-08-27 22:08:33
多谢
import java.awt.Graphics2D;
import java.awt.*;
//import javax.swing.*;
public class BackGround extends javax.swing.JPanel{
    static int bx=90; 
    static int by=90;
    static int OvalW=60;
    static int OvalH=60;
    static int r=30;
    static int blank=5;
    static int lLength=20;
    static int moveL=(int)(1.4*blank);
    static int moveC=(int)(0.7*lLength/2);
    /** Creates a new instance of BackGround */
    public BackGround() {
    }
    public BackGround(int x,int y) {
        bx=x;
        by=y;
    }
    public void paint(java.awt.Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setBackground(Color.white);
        g2.setColor(Color.red);
        g2.fillOval(bx,by,OvalW,OvalH);
        g2.setColor(Color.yellow);
        g2.drawLine((bx-moveL)-moveC,(by-moveL)+moveC,(bx-moveL)+moveC,(by-moveL)-moveC);//左上45度
        g2.drawLine((bx-moveL)+moveC+lLength,(by-moveL)-moveC,(bx-moveL)+moveC+lLength+lLength,(by-moveL)-moveC);//上横线
        g2.drawLine((bx+2*r+moveL)+moveC,(by-moveL)+moveC,(bx+2*r+moveL)-moveC,(by-moveL)-moveC);//右上45度
        g2.drawLine((bx-moveL)+moveC+lLength,(by+2*r+moveL)+moveC,(bx-moveL)+moveC+lLength+lLength,(by+2*r+moveL)+moveC);//下横线
        g2.drawLine((bx-moveL)-moveC,(by+2*r+moveL)-moveC,(bx-moveL)+moveC,(by+2*r+moveL)+moveC);//左下45度
        g2.drawLine((bx+2*r+moveL)+moveC,(by+2*r+moveL)-moveC,(bx+2*r+moveL)-moveC,(by+2*r+moveL)+moveC);//右下45度
        g2.drawLine((bx-moveL)-moveC,(by-moveL)+moveC+lLength,(bx-moveL)-moveC,(by-moveL)+moveC+lLength+lLength);//左竖线
        g2.drawLine((bx+2*r+moveL)+moveC,(by-moveL)+moveC+lLength,(bx+2*r+moveL)+moveC,(by-moveL)+moveC+lLength+lLength);//右竖线
        /*g.drawLine(bx-blank,by+r,bx-lLength-blank,by+r);//左边横线
        g.drawLine(bx-blank+blank+2*r+lLength,by+r,bx-lLength-blank+blank+2*r+lLength+blank,by+r);//右边横线
        g.drawLine(bx,by-(int)(blank*0.8),bx-blank+(int)(r*0.5)-(int)(lLength*0.5)-blank*2,by);//左边60度线
        //g.drawLine(bx-(int)(blank*0.5)+(int)(r*0.5)-blank*2,by-(int)(blank*0.8),bx-blank+(int)(r*0.5)-(int)(lLength*0.5)-blank*2,by-(int)(blank*0.8)-(int)(lLength*0.8))
        g.drawLine(bx-((int)(blank*0.5))+((int)(r*0.5))+2*r-lLength+2*blank,by-((int)(blank*0.8)),bx-blank+((int)(r*0.5))-((int)(lLength*0.5))+2*r+2*blank,by-((int)(blank*0.8))-((int)(lLength*0.8)));//右边近60度线
        g.drawLine(bx+r,by-blank-lLength,bx+r,by-blank-lLength+lLength);//垂直上面的线
        g.drawLine(bx+r,by-blank-lLength+blank+lLength+blank+2*r+lLength,bx+r,by-blank-lLength+lLength+blank+2*r+blank);//垂直下面的线
        g.drawLine(bx-(int)(blank*0.5)+(int)(r*0.5)-blank*2+(int)((2*blank)*0.5+2*r),by-(int)(blank*0.8)+(int)((2*blank)*0.8)+2*r,bx-blank+(int)(r*0.5)-(int)(lLength*0.5)-blank*2+(int)((2*blank+2*lLength)*0.5)+2*r,by-(int)(blank*0.8)-(int)(lLength*0.8)+(int)((2*blank+2*lLength)*0.8)+2*r);//**/
        //g.drawLine(bx-(int)(blank*0.5)+(int)(r*0.5)-blank*2+2*r+())
        /*g.drawLine(75,65,95,85);
        g.drawLine(60,120,85,120);
        g.drawLine(152,120,172,120);
        g.drawLine(152,142,172,162);
        g.drawLine(145,60,130,85);
        g.drawLine(100,125,85,150);*/
        //第一个三角形的顶点
        int treeTopX1=(bx-moveL)+moveC+lLength+lLength/2;
        int treeTopY1=(by+2*r+moveL)+moveC+2*5*lLength;
        int i=1;
        int triangleL=(int)(0.7*2*i*lLength);
        //第一个三角形的点
        int xPoints[]={treeTopX1,treeTopX1-triangleL,treeTopX1+triangleL};
        int yPoints[]={treeTopY1,treeTopY1+triangleL,treeTopY1+triangleL};
        g2.setColor(Color.green);
        g2.fillPolygon(xPoints, yPoints, 3);
        //第二个三角形的顶点
        int treeTopX2=treeTopX1;
        int treeTopY2=treeTopY1+triangleL;
        i++;
        triangleL=(int)(0.7*2*i*lLength);
        //第二个三角形的点
        int xPoints1[]={treeTopX2,treeTopX2-triangleL,treeTopX2+triangleL};
        int yPoints1[]={treeTopY2,treeTopY2+triangleL,treeTopY2+triangleL};
        g2.fillPolygon(xPoints1, yPoints1, 3);
        //第三个三角形的顶点
        int treeTopX3=treeTopX2;
        int treeTopY3=treeTopY2+triangleL;
        i++;
        triangleL=(int)(0.7*2*i*lLength);
        //第三个三角形的点
        int xPoints2[]={treeTopX3,treeTopX3-triangleL,treeTopX3+triangleL};
        int yPoints2[]={treeTopY3,treeTopY3+triangleL,treeTopY3+triangleL};
        g2.fillPolygon(xPoints2, yPoints2, 3);
        g2.setColor(Color.GRAY);
        //树根
        g2.fillRect(treeTopX3-triangleL+triangleL/4*3,treeTopY3+triangleL,triangleL/2, triangleL/2);
    }
    /*public static void main(String args[]) {
        new BackGround();
    }*/
}
import java.awt.*;
public class Back extends javax.swing.JFrame {
    
    /** Creates new form Back */
    public Back() {
        initComponents();
        BackGround background =new BackGround();
        background.setOpaque(true);
        this.getContentPane().add(background);
    }
    public void paint(java.awt.Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        //g2.setBackground(Color.white);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        
        pack();
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        Back back=new Back();
        //back.setBackground(Color.white);
        //back.setVisible(true);
        back.show();
    }
    
    // Variables declaration - do not modify
    // End of variables declaration
    
}

7 个解决方案

#1




#2


怎么没人看吗,顶

#3


把setBackground(Color.white);写在最后就可以了!
     。。。
        int xPoints2[]={treeTopX3,treeTopX3-triangleL,treeTopX3+triangleL};
        int yPoints2[]={treeTopY3,treeTopY3+triangleL,treeTopY3+triangleL};
        g2.fillPolygon(xPoints2, yPoints2, 3);
        g2.setColor(Color.GRAY);
        //树根
        g2.fillRect(treeTopX3-triangleL+triangleL/4*3,treeTopY3+triangleL,triangleL/2, triangleL/2);
        setBackground(Color.white);
    }
    /*public static void main(String args[]) {
        new BackGround();
    }*/
}

#4


谢谢。现在我需要在jframe中设置背景颜色(因为我所画的东西都做成了jpanel,把它们都加在jframe上),但是现在在jframe中的paint方法中setBackground(Color.white)之后整个全变白了,我那些panel上画的东西也不见了,我把panel设成不透明也不行。

#5


现在我把所有的东西都直接画在jframe上,其上有一个jlabel,怎么才能把jframe的背景设成想要的颜色,直接在paint方法中setBackround不好使。

#6


i know,需要在构造函数中,getContentPane().setBackground()设置颜色,谢谢 Albert_1030()

#7


不错 谢谢!

#1




#2


怎么没人看吗,顶

#3


把setBackground(Color.white);写在最后就可以了!
     。。。
        int xPoints2[]={treeTopX3,treeTopX3-triangleL,treeTopX3+triangleL};
        int yPoints2[]={treeTopY3,treeTopY3+triangleL,treeTopY3+triangleL};
        g2.fillPolygon(xPoints2, yPoints2, 3);
        g2.setColor(Color.GRAY);
        //树根
        g2.fillRect(treeTopX3-triangleL+triangleL/4*3,treeTopY3+triangleL,triangleL/2, triangleL/2);
        setBackground(Color.white);
    }
    /*public static void main(String args[]) {
        new BackGround();
    }*/
}

#4


谢谢。现在我需要在jframe中设置背景颜色(因为我所画的东西都做成了jpanel,把它们都加在jframe上),但是现在在jframe中的paint方法中setBackground(Color.white)之后整个全变白了,我那些panel上画的东西也不见了,我把panel设成不透明也不行。

#5


现在我把所有的东西都直接画在jframe上,其上有一个jlabel,怎么才能把jframe的背景设成想要的颜色,直接在paint方法中setBackround不好使。

#6


i know,需要在构造函数中,getContentPane().setBackground()设置颜色,谢谢 Albert_1030()

#7


不错 谢谢!