2012-05-30 2 views
0

내 프로젝트에서 사용자 정의 모양의 버튼을 만들고 싶습니다. 원형 버튼을 만드는 코드를 가지고 있고 많은 연구 끝에 투명도가있는 PNG 이미지에서 모양 (영역)을 생성하는 데 사용할 수있는 코드를 찾았습니다. 코드를 사용하여 넣을 수 있습니다. 내 사용자 정의 버튼 프로그램으로 그러나 모양을 만드는 프로세스는 CPU를 소비하므로 각 모양을 만드는 데 꽤 오랜 시간이 걸립니다. 다음은 이미지에서 도형을 생성하는 코드입니다. 위 코드를 사용하면 NullPointerException 오류가 발생합니다. 여기에 스택 트레이스는 다음과 같습니다Java - PNG 이미지에서 도형 만들기 (NullPointerException)

여기
java.lang.NullPointerException 
at CreateShapeClass.createArea(CreateShapeClass.java:10) 
at CustomButton.initShape(CustomButton.java:95) 
at CustomButton.paintBorder(CustomButton.java:102) 
at javax.swing.JComponent.printBorder(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.print(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.printChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.print(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.printChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JLayeredPane.paint(Unknown Source) 
at javax.swing.JComponent.print(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.printChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.print(Unknown Source) 
at java.awt.GraphicsCallback$PrintCallback.run(Unknown Source) 
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) 
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) 

내 CustomButtonClass :

import java.awt.*; 
import java.awt.geom.*; 
import java.awt.image.BufferedImage; 
import java.io.*; 
import java.util.Iterator; 

import javax.imageio.ImageIO; 
import javax.imageio.ImageReadParam; 
import javax.imageio.ImageReader; 
import javax.imageio.stream.ImageInputStream; 
import javax.swing.*; 

public class CustomButton extends JButton { 
    protected Shape shape, base; 
    protected BufferedImage image; 
    protected String imagePath; 

    private static final long serialVersionUID = 1L; 
    public CustomButton() { 
     this(null, null); 
    } 
    //takes in an icon 
    public CustomButton(Icon icon) { 
     this(null, icon); 
    } 
    //takes in a text string for button 
    public CustomButton(String text) { 
     this(text, null); 
    } 

    //takes in a text string for button 
    public CustomButton(Icon icon, String imagePath, boolean useless) { 
     this(null, icon); 
     this.imagePath = imagePath; 
    } 
    //takes in an action for the button press event 
    public CustomButton(Action a) { 
     this(); 
     setAction(a); 
    } 
    //takes in text and icon image 
    public CustomButton(String text, Icon icon) { 
     setModel(new DefaultButtonModel()); 
     init(text, icon); 
     if(icon==null) { 
      return; 
     } 
     setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); 
     setBackground(Color.BLACK); 
     setContentAreaFilled(false); 
     setFocusPainted(false); 
     //setVerticalAlignment(SwingConstants.TOP); 
     setAlignmentY(Component.TOP_ALIGNMENT); 
     initShape(); 
    } 

    //creates a method for retrieving preferred size of the button (the image) 
    @Override public Dimension getPreferredSize() { 
     Icon icon = getIcon(); 
     Insets i = getInsets(); 
     if (icon == null){ 
      return super.getPreferredSize(); 
     } 
     else { 
      return new Dimension(icon.getIconWidth(), icon.getIconHeight()); 
     } 
    } 

    //creates the shape of the button from the image 
    protected void initShape() { 
     if(!getBounds().equals(base)) { 
      Dimension s = getPreferredSize(); 
      base = getBounds(); 
      if (image == null){ 
       try { 
        image = ImageIO.read(new File("Untitled1.png")); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      shape = CreateShapeClass.createArea(image, 25); 
      System.out.println(shape.getBounds()); 
     } 
    } 

    //creates the border of the button 
    @Override protected void paintBorder(Graphics g) { 
     initShape(); 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setColor(getBackground()); 
     g2.setStroke(new BasicStroke(1.0f)); 
     g2.draw(shape); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); 
    } 
    @Override public boolean contains(int x, int y) { 
     initShape(); 
     return shape.contains(x, y); 
    } 
} 

그래서, 누군가가 내가 코드를 잘못 않은 위치에 나를 인도 할 수 있습니까? 생성 된 모양을 파일의 일종으로 저장할 수 있으므로 프로그램을 실행할 때마다 항상 모양을 다시 생성 할 필요가 없습니다. 또는 형상을 희미하게 할 수있는 방법이 있습니다.

+0

'이미지'가 null인지 확인하십시오. –

+0

문제를 해결해 주셔서 감사합니다. D – csharpnewbie

답변

1

image 매개 변수가 null인지 확인하지 않아도됩니다. 호출 메서드를 보면 initShape "Untitled1.png"를 읽을 수없는 경우 널 포인터를 얻을 수 있습니다.

+0

감사합니다. 이미지가 null 인 경우 확인을 추가하여 문제를 해결합니다. – csharpnewbie