2013-11-14 2 views
0

콘텐츠 창을 투명하게 만들고 일반 어두운 파란색 색조의 파란색 스트립을 만들려고합니다.하지만 contentPane을 투명하게 만드는 과정에서이 스트립을 무딘 모양으로 만듭니다. (coz 그 위에 검은 색이 그려져있다) 우연히.내 작은 스윙 코드로 그림을 수정하는 데 도움이 필요합니다

enter image description here

내가 그것을 어떻게

을 를 해결할 수 있습니다

?

class Home extends JFrame 
{ 
    int width=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width; 
    int height=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height; 
    public Home() 
    { 
     super("WiND"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setUndecorated(true); 
     setSize(width,height); 
     setBackground(new Color(0,0,0,0)); 
     setUndecorated(true); 
     setVisible(true); 
     setLayout(new FlowLayout()); 

     JPanel p=new JPanel(); 
     p.setBackground(new Color(0x0D70E8)); 
     p.setPreferredSize(new Dimension(width,height/10)); 
     add(p); 
    } 
    public void paint(Graphics g) 
    { 
     super.paint(g); 
     Graphics2D g2=(Graphics2D)g; 
     LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)}); 
     g2.setPaint(p); 
     g2.fillRect(0, 0, width,height); 
    } 
} 

(나는 같은 일 a를 그랬어 :

여기

코드입니다 (페인트 방법을 주석하고 strip.This의 변화를 주목하는 것은 내가 최종 결과로 원하는 것입니다) 올해 다시,하지만 지금은 일 년 후에 나는

편집

내가로 변경 한) 것을 어떻게했는지 잊어 @ Sage에 따르는 paint() 메소드. 다음 출력을 얻습니다. 청색 스트립 수정. 이제 회색 반투명 ​​배경이 사라졌습니다. enter image description here

+0

페인트. 이후에 불투명 한 색상을 페인트합니다. – Cruncher

+3

1) 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오.2)'JFrame'과 같은 최상위 컨테이너에 그림을 그리는 대신'paintComponent (Graphics)'메서드에서'JPanel'과'do custom painting을 추가하십시오. 또한 레이아웃 관리자를 돕기 위해 맞춤 구성 요소에 적합한 적절한 크기를 반환하십시오. –

+0

@Cruncher : 어떻게해야합니까? 나는'paint()'가 항상 나중에 호출 될 것이라고 생각한다. –

답변

2
public void paint(Graphics g) 
    { 
     super.paint(g); 
     Graphics2D g2=(Graphics2D)g; 
     LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)}); 
     g2.setPaint(p); 
     g2.fillRect(0, 0, width,height); 
    } 

당신은 당신이 그려진 당신의 아이 컴퍼넌트 panel 후 그래픽 인스턴스 g 페인팅하는 paint() 기능 그림 때. super.paint(g) 함수의 소스로 가서 세 이후의 함수가 호출되는 것을 볼 수 있습니다 :

  • protected void paintComponent(Graphics g)이 하나의 구성 요소, E, G 페인트 :. 배경
  • protected void paintBorder(Graphics g) :이 하나가 경계를 그립니다 구성 요소
  • protected void paintChildren(Graphics g)의 :이 하나의 구성 요소의 자식을 페인트 그것을

그래서 당신은 t로 만든 모든 그림 위에 나타납니다 그릴이 super.paint(g) 전화, 아무것도 후 위의 3 가지 기능을 수행하십시오. 따라서 어린이 구성 요소 위에는 배경에 파란색 배경의 panel이 있습니다.

이제 해결책은 다음과 같습니다

public void paint(Graphics g) 
{ 

      Graphics2D g2 = (Graphics2D)g.create(); 
       // note, we are creating a graphics object here for safe painting 
      LinearGradientPaint p=new LinearGradientPaint(0,0,0,height,new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)}); 
      g2.setPaint(p); 
      g2.fillRect(0, 0, width,height); 
      g2.dispose(); // disposing the object which we created 

      super.paint(g); 

    } 

그러나 오히려이 일을보다, 당신이 MyCanvas extends JComponent와 같은 클래스를 사용하고 paintComponent(Graphics) 함수의 오버라이드 (override) 해, 그 안에립니다. 그런 다음 setContentPane(component) 기능을 사용하여 MyCanvas의 인스턴스를 JFrame의 콘텐츠 창으로 설정할 수 있습니다.

체크 아웃 A Closer Look at the Paint Mechanism


편집 사용 사례의 작은 데모 구현 : 투명 색상 첫째

class AMyContainer extends JComponent 
{ 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     Graphics2D g2 = (Graphics2D)g.create(); 
       // note, we are creating a graphics object here for safe painting 
      LinearGradientPaint p=new LinearGradientPaint(0, 0, 0, getHeight(),new float[]{0f,1f},new Color[]{new Color(0.2498f,0.2498f,0.2498f,0.3f),new Color(0.1598f,0.1598f,0.1598f,0.8f)}); 
      g2.setPaint(p); 
      g2.fillRect(0, 0, getWidth(), getHeight()); 
      g2.dispose(); // disposing the object which we created 
    } 

} 

class Home extends JFrame 
{ 
    int width=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width; 
    int height=GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height; 
    public Home() 
    { 
     super("WiND"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setUndecorated(true); 
     setSize(width,height); 
     setBackground(new Color(0,0,0,0)); 
     setUndecorated(true); 

     JComponent container = new AMyContainer(); 
     container.setLayout(new FlowLayout()); 
     add(container); 

     JPanel p=new JPanel(); 
     p.setBackground(new Color(0x0D70E8)); 
     p.setPreferredSize(new Dimension(width,height/10)); 
     container.add(p); 
    } 



    public static void main(String[] args) 
    { 
     new Home().setVisible(true); 

    } 
} 
+0

@Stage : 이것은 짙은 청색을 원하지만 이제 배경이 완전히 투명 해지며 초기 회색이었습니다. –

+0

@InsaneCoder, 아직 여기 있습니다. SSCCE를 게시하면 현재 문제를 이해하고 도움을받을 수 있습니다. 그러한 예제를 만들었 으면 바로 아래에 주석을 넣으십시오. – Sage

+0

EDIT –

관련 문제