2012-07-17 3 views
2

사용자 지정 화살표 클래스가 있는데이 클래스는 BasicArrowButton을 확장합니다. 그것은 필요한 방식으로 생성되고 표시됩니다. 그러나 다시 그리기 (마우스 오버, 다른 탭 클릭 등) 화살표가 사라집니다. 어떻게 해결할 수 있습니까?사용자 지정 Swing 화살표 단추 repaint() 사용자 지정 내용을 지움

public class CustomArrow extends BasicArrowButton { 
private transient Color shadow = new Color(241, 241, 241); 
private transient Color dark = new Color(150, 150, 150); 
private static int defaultSize = 10; 

/** The Polygon that points up. */ 
private static Polygon upIcon = new Polygon(new int[] { 0, 5, 9 }, 
              new int[] { 7, 2, 7 }, 3); 

/** The Polygon that points down. */ 
private static Polygon downIcon = new Polygon(new int[] { 1, 8, 19 }, 
              new int[] { 3, 13, 3 }, 3); 

/** The Polygon that points left. */ 
private static Polygon leftIcon = new Polygon(new int[] { 7, 3, 7 }, 
              new int[] { 1, 5, 9 }, 3); 

/** The Polygon that points right. */ 
private static Polygon rightIcon = new Polygon(new int[] { 3, 7, 3 }, 
              new int[] { 1, 5, 9 }, 3); 

private transient Border buttonBorder = new Border() 
{ 
    public Insets getBorderInsets(Component c) 
    { 
    return new Insets(2, 2, 2, 2); 
    } 

    public boolean isBorderOpaque() 
    { 
return true; 
    } 

    public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) 
    { 
    Color saved = g.getColor(); 
    g.setColor(shadow); 

    g.drawLine(x + 1, y, x + w - 1, y); 

     g.setColor(dark); 
    g.drawLine(x, y, x, y + h + 2); 

     g.setColor(shadow); 
    g.drawLine(x + w - 1, y + 1, x + w - 1, y + h - 1); 

    g.setColor(shadow); 
     g.drawLine(x + 1, y + h - 1, x + w, y + h - 1); 

    g.setColor(saved); 
    } 
    }; 


@Override 
public synchronized void addKeyListener(KeyListener l) { 
    super.addKeyListener(l); 
} 

@Override 
public void addActionListener(ActionListener l) { 
    super.addActionListener(l); 
} 
public CustomArrow(int direction) 
{ 
    super(direction); 
    setBorder(buttonBorder); 
    setDirection(direction); 
    this.setRolloverEnabled(false); 
} 

public CustomArrow(int direction, Color background, Color shadow, Color darkShadow, Color highlight) 
{ 
    this(direction); 
    setBackground(background); 
} 

@Override 
public void paintTriangle(Graphics g, int x, int y, int size, int direction, boolean isEnabled) 
{ 
    Polygon arrow = null; 
    switch (direction) 
    { 
    case NORTH: 
    arrow = upIcon; 
    break; 
    case SOUTH: 
    arrow = downIcon; 
    break; 
    case EAST: 
    case RIGHT: 
    arrow = rightIcon; 
    break; 
    case WEST: 
    case LEFT: 
    arrow = leftIcon; 
    break; 
    } 

    int[] xPoints = arrow.xpoints; 
    int[] yPoints = arrow.ypoints; 
    int x1; 
    int y1; 
    int x2; 
    int y2; 
    x1 = y1 = x2 = y2 = 0; 

    x = x - 1; 
    if (size != defaultSize) 
    { 
float scale = size * 1f/defaultSize; 
for (int i = 0; i < 3; i++) 
    { 
    xPoints[i] *= scale; 
    yPoints[i] *= scale; 
    } 
    } 
    g.translate(x, y); 

    switch (direction) 
    { 
    case NORTH: 
    x1 = xPoints[0] + 2; 
    y1 = yPoints[0]; 
    y2 = y1; 
    x2 = xPoints[2] - 1; 
    break; 
    case SOUTH: 
    x1 = xPoints[1]; 
    y1 = yPoints[1] + 1; 
    x2 = xPoints[2] - 1; 
    y2 = yPoints[2]; 
    break; 
    case LEFT: 
    case WEST: 
    x1 = xPoints[0] + 1; 
    y1 = yPoints[0] + 1; 
    x2 = x1; 
    y2 = yPoints[2] + 1; 
    break; 
    case RIGHT: 
    case EAST: 
    x1 = xPoints[2]; 
    y1 = yPoints[2] + 1; 
    x2 = xPoints[1] - 1; 
    y2 = yPoints[1] + 1; 
    break; 
    } 
    Color saved = g.getColor(); 
g.setColor(dark); 

if (arrow != null) { 
    g.fillPolygon(xPoints, yPoints, 3); 
    } 

    g.setColor(saved); 
    g.translate(-x, -y); 
    } 

    public static void main(String[] args) { 
    // Resize the frame to reproduce 
    JFrame frame = new JFrame("Test"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(new CustomArrow(SwingConstants.NORTH)); 
    frame.setSize(400, 400); 
    frame.setVisible(true); 
    } 
} 
+2

빨리 [SSCCE] (http://sscce.org/)를 게시하십시오. –

+0

참고 사항 [대답] (http://stackoverflow.com/a/3008587/230513). – trashgod

답변

4

배열 변수는 배열 변수가 아니라 배열 자체가 아닌 배열에 대한 참조를 포함합니다. 당신은 당신이 기준이 아닌 데이터를 복사 xPoints 및 arrow.xpoints 여전히 같은 데이터를 가리키는 것을 의미하고, 다른 영향을 미칠 것 중 하나를 수정하는

int[] xPoints = arrow.xpoints; 
int[] yPoints = arrow.ypoints; 

같은 작업을 수행 할 때. 나중에 이러한 점의 크기를 조정할 때 화살표가 그려 질 때마다 어떻게 보이는지를 변경합니다. 당신이 이것을 방지하기 위해 배열 데이터를 복사 할 경우 , 당신은 System.arraycopy를 사용할 수 있습니다

int[] xPoints = new int[3]; //arrow.xpoints; 
int[] yPoints = new int[3]; //arrow.ypoints; 
System.arraycopy(arrow.xpoints, 0, xPoints, 0, 3); 
System.arraycopy(arrow.ypoints, 0, yPoints, 0, 3); 

을하지만, 당신은 단순히 그래픽 대신 참조 점 스케일링의 개체를 확장 할 수 있습니다 : 더 나은 도움이 필요한

Graphics2D g2 = (Graphics2D) g; 
float scale = size * 1f/defaultSize; 
g2.scale(scale, scale); 
+0

대단히 감사합니다! 그것은 그저 단순한 생각이었습니다. BasicArrowButton에서 코드를 복사했습니다. – Natalia

관련 문제