개발

JPanel 에 그라디언트 주기

에드몽단테스 2009. 8. 19. 12:47

public class GPanel extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        Graphics2D g2 = (Graphics2D)g;
       
        Point2D start = new Point2D.Float(getWidth()/2, 1);
        Point2D end = new Point2D.Float(getWidth(), getHeight()/2);
        System.out.println(start);
        System.out.println(end);
        GradientPaint gp = new GradientPaint(start, Color.RED, end, Color.BLUE, false);
       
        g2.setPaint(gp);
        g2.fillRect(1, 1, getWidth()-2, getHeight()-2);
        g2.dispose();
    }
   
    public static void main(String[] args){
        JFrame f = new JFrame("그라이데이션 테스트");
        GPanel g = new GPanel();
        f.setContentPane(g);
       
        f.setSize(200, 200);
        f.setVisible(true);
    }
}

 

그라디어트 그리기 

그래픽 객체(g)에서 Graphics2D 를 얻어오고, 그라디언트 객체를 구해 setPaint() 메소드로 영역을 그려준다.

 

 

GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2, boolean cyclic)
          Constructs either a cyclic or acyclic GradientPaint object depending on the boolean parameter.

 

마지막 파라미터인 cyclic 은 패턴반복을 설정한다.

 

            [cyclic == false]                      [cyclic == true]

 

GradientPaint 외에도 LinearGradientPaint, MultipleGradientPaint 등 여러가지 클래스를 제공한다.

다른점은 좌표를 설정할 수 있는 부분이 추가되었다는 것이다.

 

반응형