java - Set gradient direction in swing -


I am trying to draw a line in the swing and I am getting some laser-like effects using the gradient. I want to apply the gradient to the width of the line (ex: a red line core is fading for orange on the edges) The problem is that when I come to an angle, then I would like to apply the same angle to the gradient I am Public Zero Paint (Main Graphics) {Graphics2D en = (Graphics2D) g; GradientPaint GP = New GradientPaint (25, 25, Color, 15, 25, Color. Oere, True); en.setPaint (GP); Nststroot (new basicstroke (4.0f)); Andrline (10, 10, 800, 600); }

An easy move to get away with this, call setTransform () To do graphics 2D this will be for you both for the rotation of the line and gradient at once. If you do not want to do this, then you will rotate the gradient manually (that is basically the gradient of PT1 and PT2, i.e., you have to calculate according to the rotation applied to your line). / P>

While explaining my first thoughts this is a small example. Just slide the line (and shield) around the center of the panel, just slide the ticker below:

  import java.awt.BasicStroke; Import java.awt.borderLayout; Import java.awt.color; Import java.awt.Dimension; Import java.awt.GradientPaint; Import java.awt.Graphics; Import java.awt.Graphics2D; Import java.awt.geom.AffineTransform; Import javax.swing.JFrame; Import javax.swing.JPanel; Import javax.swing.JSlider; Import javax.swing.SwingUtilities; Import javax.swing.event.ChangeEvent; Import javax.swing.event.ChangeListener; Public Square TestPanel JPanel {Private Double Angle = 0; @ Override Public Empty Paint Comonant (Graphics G) {SuperPaintConant (G); Graphics 2DN = (Graphics 2D) G; AffineTransform TF = AffineTransform.getTranslateInstance (-getWidth () / 2, -getHeight () / 2); Tf.preConcatenate (AffineTransform.getRotateInstance (Math.toRadians (angle))); tf.preConcatenate (AffineTransform.getTranslateInstance (getWidth) / 2, getHeight () / 2)); en.setTransform (tf); GradientPaint GP = New GradientPaint (25, 25, Color, 15, 25, Color. Oere, True); en.setPaint (GP); NSTTrot (New Basicstroke (4.0F)); Andrline (400, 400, 600, 600); } @ Override get public dimensioncert () {new dimension back (800, 800); } Public Zero Set Angle (double angle) {this.angle = angle; Repaint (); } Public Static Zero Main (string [] Args) {SwingUtilities.invokeLater (New Runnabal) {@ Override Public Wired Run (Jeffre Frame) = New GeFram ()) Final Test Panel Panel = New Test Pinnacle (); Last JSL slider = New JSLider (0, 360); Slider.Edchange Listener (New ChangeListener) {@ Override Public Wide State Changed (Change) (panel.set angel (slider.gate value)}}}); Slider .set value (0); Frame .add (panel); Frame.ed (slider, border layout.SOHT); Frame.pack (); Frame.Setlinescentlet (empty); Frame.Set default close operation (JFRAM.EXIT_ON_CLOSE), frame.Serviceable (true);}}); }}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -