2014-07-12 3 views
3

내 사용자 인터페이스에서 뭔가를 구현하려고합니다. draw(Graphics2D g) 메서드입니다. 그것은 스윙 구성 요소가 아니며, 직접 드로잉을하고 있습니다. 어떻게 내가 그 비슷한 그라데이션을 그릴 수있는이 이미지사각형 그라디언트

gradient border

같은 그라데이션 테두리를 만들려면 노력하고 있어요?

+0

당신이 흰색에 페이드 아웃 점차적으로 다음 빨간색으로 시작하려고 있나요? –

+1

필자가 본 유일한 그라디언트는 직선형 또는 방사형 이었지만 직사각형 모양으로 바깥쪽으로 나가는 것을 본 적이 없으므로이를 수행하는 방법을 완전히 모릅니다. – Kyranstar

+0

이 작업을 수행 할 때 항상 자신의 Paint 파생 클래스를 만들 수 있습니다. 이것이 원하는 경로라면 Paint, GradientPaint 및 유사한 클래스 및 인터페이스에 대한 Java의 소스 코드를 확인하십시오. –

답변

3

당신은 정말 당신이 원하는를 지정하지 않은,하지만 나는 그것이 그 생각 어떤 경우, 그것은 내가 my answer to "How to make gradient border of an image using java?"에 쓴 것과 다소 비슷하다.

트릭은 하나의 그라데이션 페인트를 만들지 않고 하나의 도형을 채우지 않는 것입니다. 대신 그라디언트로 채워야하는 영역은 네 부분과 네 모서리의 8 부분으로 나뉩니다. 가장자리는 간단한 그라디언트 페인트로 채워집니다. 모서리는 방사형 그래디언트 페인트로 채워집니다. 그런 다음 좌표만으로 주변을 둘러 보는 것이지만 내부 모양이 Rectangle2D으로 주어질 때 일반적인 방법으로 수행 할 수 있습니다.

그러나, 결과는, 예를 들어, 다음과 같이 보일 수 있습니다 :

enter image description here

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GradientPaint; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.MultipleGradientPaint.CycleMethod; 
import java.awt.RadialGradientPaint; 
import java.awt.Shape; 
import java.awt.geom.Point2D; 
import java.awt.geom.Rectangle2D; 
import java.awt.geom.RoundRectangle2D; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class RectangularGradientTest 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(new RectangularGradientTestPanel()); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 
} 


class RectangularGradientTestPanel extends JPanel 
{ 
    @Override 
    protected void paintComponent(Graphics gr) 
    { 
     super.paintComponent(gr); 
     Graphics2D g = (Graphics2D)gr; 
     Rectangle2D r = new Rectangle2D.Double(100,100,200,100); 
     draw(g, r, 75); 

     Shape rr = new RoundRectangle2D.Double(80,80,240,140,20,20); 
     g.setColor(Color.BLACK); 
     g.fill(rr); 
    } 

    @Override 
    public Dimension getPreferredSize() 
    { 
     if (isPreferredSizeSet()) 
     { 
      return super.getPreferredSize(); 
     } 
     return new Dimension(400,300); 
    } 


    private static void draw(Graphics2D g, Rectangle2D r, double s) 
    { 
     Color c0 = new Color(255,0,0); 
     Color c1 = new Color(255,0,0,0); 

     double x0 = r.getMinX(); 
     double y0 = r.getMinY(); 
     double x1 = r.getMaxX(); 
     double y1 = r.getMaxY(); 
     double w = r.getWidth(); 
     double h = r.getHeight(); 

     // Left 
     g.setPaint(new GradientPaint(
      new Point2D.Double(x0, y0), c0, 
      new Point2D.Double(x0 - s, y0), c1)); 
     g.fill(new Rectangle2D.Double(x0 - s, y0, s, h)); 

     // Right 
     g.setPaint(new GradientPaint(
      new Point2D.Double(x1, y0), c0, 
      new Point2D.Double(x1 + s, y0), c1)); 
     g.fill(new Rectangle2D.Double(x1, y0, s, h)); 

     // Top 
     g.setPaint(new GradientPaint(
      new Point2D.Double(x0, y0), c0, 
      new Point2D.Double(x0, y0 - s), c1)); 
     g.fill(new Rectangle2D.Double(x0, y0 - s, w, s)); 

     // Bottom 
     g.setPaint(new GradientPaint(
      new Point2D.Double(x0, y1), c0, 
      new Point2D.Double(x0, y1 + s), c1)); 
     g.fill(new Rectangle2D.Double(x0, y1, w, s)); 

     float fractions[] = new float[] { 0.0f, 1.0f }; 
     Color colors[] = new Color[] { c0, c1 }; 

     // Top Left 
     g.setPaint(new RadialGradientPaint(
      new Rectangle2D.Double(x0 - s, y0 - s, s + s, s + s), 
      fractions, colors, CycleMethod.NO_CYCLE)); 
     g.fill(new Rectangle2D.Double(x0 - s, y0 - s, s, s)); 

     // Top Right 
     g.setPaint(new RadialGradientPaint(
      new Rectangle2D.Double(x1 - s, y0 - s, s + s, s + s), 
      fractions, colors, CycleMethod.NO_CYCLE)); 
     g.fill(new Rectangle2D.Double(x1, y0 - s, s, s)); 

     // Bottom Left 
     g.setPaint(new RadialGradientPaint(
      new Rectangle2D.Double(x0 - s, y1 - s, s + s, s + s), 
      fractions, colors, CycleMethod.NO_CYCLE)); 
     g.fill(new Rectangle2D.Double(x0 - s, y1, s, s)); 

     // Bottom Right 
     g.setPaint(new RadialGradientPaint(
      new Rectangle2D.Double(x1 - s, y1 - s, s + s, s + s), 
      fractions, colors, CycleMethod.NO_CYCLE)); 
     g.fill(new Rectangle2D.Double(x1, y1, s, s)); 
    } 

}