Draw line with Gradient

Hi,
I am trying to implement drawing on canvas with two colors, red and blue.
For example, to draw with the mouse "with two colors".
I do not need a filled rectangle.
I am looking into gradient but without any success.
Is there a better way to draw a line with two colors?
I also tried to use BasicStroke, but the line had an inner/outer strokes.
Please help with suggestions.

devboris wrote:
..For example, to draw with the mouse "with two colors".
import java.awt.*;
import javax.swing.*;
class GradientLine {
    public static void main(String[] args) {
        GradientPanel gp = new GradientPanel();
        gp.setPreferredSize( new Dimension(600,400) );
        JOptionPane.showMessageDialog(null, gp);
class GradientPanel extends JPanel {
    public void paintComponent(Graphics g) {
        // cast the Graphics objkect to a Graphics2D object
        // (G2D includes setPaint() method)
        Graphics2D g2 = (Graphics2D)g;
        GradientPaint bgPaint = new GradientPaint(
            0,
            0,
            Color.black,
            getWidth(),
            getHeight(),
            Color.white );
        g2.setPaint(bgPaint);
        g2.fillRect( 0, 0, getWidth(), getHeight() );
        GradientPaint fgPaint = new GradientPaint(
            0,
            0,
            Color.yellow,
            getWidth(),
            getHeight(),
            Color.red );
        // set the paint to a gradient.
        g2.setPaint(fgPaint);
        for (int ii=0; ii<getWidth()/10; ii++) {
            g2.drawLine( ii*10, 0, ii*10, getHeight() );
        for (int ii=0; ii<getHeight()/10; ii++) {
            g2.drawLine( 0, ii*15, getWidth(), ii*15 );
}

Similar Messages

  • Can j2me draw line with double values.

    Hi,
    Can any body know how to darw line in j2me with double values.
    I don't want use draw Line with int.
    Shall i use svg or j2me has solution.
    Thanks and regards,
    Rakesh.

    not possible
    graphics.drawLine(float,float,float,float);...there's no such method in MIDP API: [click here for javadoc of Graphics class methods|http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Graphics.html#drawLine(int,%20int,%20int,%20int)]

  • Draw line with float values possible

    Hi,
    Using Canvas drawing is possible to draw line with float values.
    graphics.drawLine(int,int,int,int);
    graphics.drawLine(float,float,float,float);Thanks and regards,
    Rakesh.

    not possible
    graphics.drawLine(float,float,float,float);...there's no such method in MIDP API: [click here for javadoc of Graphics class methods|http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Graphics.html#drawLine(int,%20int,%20int,%20int)]

  • Drawing lines with smooth curves

    Hi,
    I'm trying to make a map for an underground system in a PC game and I want something similar to the one Transport for London has:
    http://www.tfl.gov.uk/assets/downloads/standard-tube-map.gif
    I'm a total newbie with Adobe Illustrator, but I'm trying it as I've heard it's what Transport for London uses.
    My question is then, what would be the easiest way of accomplishing multiple lines with different colours and smooth curves, like the lines in my reference picture?
    Thanks in advance,
    Martin

    Martin,
    In addition to what Kurt said, you may draw paths with straight segments and then round the corners afterwards, using Effect>Stylise>Round Corners (you may Object>Expand Appearance to obtain actual roundings).

  • How to draw line with width at my will

    Dear frineds:
    I have following code to draw lines, but I was required:
    [1]. draw this line with some required width such as 0.2 or 0.9 or any width at my will
    [2]. each line after I draw, when I use mouse to click on it, it will be selected and then I can delete it,
    Please advice how to do it or any good example??
    Thanks
    sunny
    package com.draw;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class DrawingArea extends JPanel
         Vector angledLines;
         Point startPoint = null;
         Point endPoint = null;
         Graphics g;
         public DrawingArea()
              angledLines = new Vector();
              setPreferredSize(new Dimension(500,500));
              MyMouseListener ml = new MyMouseListener();
              addMouseListener(ml);
              addMouseMotionListener(ml);
              setBackground(Color.white);
         public void paintComponent(Graphics g)
              // automatically called when repaint
              super.paintComponent(g);
              g.setColor(Color.black);
              g.setFont(getFont());
              AngledLine line;
              if (startPoint != null && endPoint != null)
                   // draw the current dragged line
                   g.drawLine(startPoint.x, startPoint.y, endPoint.x,endPoint.y);
              for (Enumeration e = angledLines.elements(); e.hasMoreElements();)
                   // draw all the angled lines
                   line = (AngledLine)e.nextElement();
                   g.drawPolyline(line.xPoints, line.yPoints, line.n);
         class MyMouseListener extends MouseInputAdapter
              public void mousePressed(MouseEvent e)
                   if (SwingUtilities.isLeftMouseButton(e))
                        startPoint = e.getPoint();
              public void mouseReleased(MouseEvent e)
                   if (SwingUtilities.isLeftMouseButton(e))
                        if (startPoint != null)
                             AngledLine line = new AngledLine(startPoint, e.getPoint(), true);
                             angledLines.add(line);
                             startPoint = null;
                             repaint();
              public void mouseDragged(MouseEvent e)
                   if (SwingUtilities.isLeftMouseButton(e))
                        if (startPoint != null)
                             endPoint = e.getPoint();
                             repaint();
              public void mouseClicked( MouseEvent e )
                   if (g == null)
                        g = getGraphics();
                   g.drawRect(10,10,20,20);
         class AngledLine
              // inner class for angled lines
              public int[] xPoints, yPoints;
              public int n = 2;
              public AngledLine(Point startPoint, Point endPoint, boolean left)
                   xPoints = new int[n];
                   yPoints = new int[n];
                   xPoints[0] = startPoint.x;
                   xPoints[1] = endPoint.x;
                   yPoints[0] = startPoint.y;
                   yPoints[1] = endPoint.y;
                   /*if (left)
                        xPoints[1] = startPoint.x;
                        yPoints[1] = endPoint.y;
                   else
                        xPoints[1] = endPoint.x;
                        yPoints[1] = startPoint.y;
         public static void main(String[] args)
              JFrame frame = new JFrame("Test angled lines");
              frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              DrawingArea d = new DrawingArea();
              frame.getContentPane().add( d );
              frame.pack();
              frame.setVisible(true);
    }Message was edited by:
    sunnymanman

    sonudevgan wrote:
    dear piz tell me how i can read integer from user my email id is [email protected]
    foolish, foolish question

  • Drawing line with button??

    Hi all.
    Why won't this class draw a line when i click the mouse button. Nothing happens when i click on the panel.
    import java.util.*;
    import java.awt.*;
    import javax.swing.JPanel;
    import javax.swing.*;
    import javax.swing.Action.*;
    import java.awt.event.*;
    import java.awt.event.MouseAdapter;
    public class DrawingPanel extends JPanel implements Observer
      private ViewableEtch etch;
      private ClearPanel clear;
      public DrawingPanel(ViewableEtch etch, ClearPanel clear)
        this.etch = etch;
        this.clear = clear;
       etch.addView(this);
        clear.addView(this);
        setLayout(new FlowLayout());
        setLayout(new GridLayout(200,200));
          //etch.addActionListener(this);
            DrawingControl dc = new DrawingControl();
        public void update(Observable obs, Object obj)
         Views v = (Views)obs;
         if (v.getSource() == etch)
         //etch.getLineColor();
           getGraphics().setColor(etch.getLineColor() );
           getGraphics().drawLine(etch.getStartX(), etch.getStartY(), etch.getEndX(), etch.getEndY());
         else
           v.getSource().equals(clear);
    public void mouseClicked(MouseEvent e)
    //etch.setLine(new etch(getStartX(), getStartY(), e.getEndX(), e.getEndY()));
    getGraphics().drawLine(etch.getStartX(), etch.getStartY(), etch.getEndX(), etch.getEndY());
    }thanx

    This should work
    public class MyPanel extends JPanel implements MouseListener {
       public MyPanel() {
          addMouseListener(this);
       public void mouseClicked(MouseEvent e) {
          // Add code here
       public void mouseEntered(MouseEvent e) {}
       public void mouseExited(MouseEvent e) {}
       public void mousePressed(MouseEvent e) {}
       public void mouseReleased(MouseEvent e) {}

  • Draw Line With Arrow between containers in flex

    I need to connect multiple containers by drawing arrow . can any one provide me the idea how to do?
    Thanks in Advance,
    senthil
    [email protected]

    If your containers are within a spark container them self then you could use fxg to draw arrows based on the containers boundaries
    eg this code will draw a red arrow:
    <Graphic xmlns="http://ns.adobe.com/fxg/2008" version="2"> <!-- Use Use compact syntax with absolute coordinates. --> <Path data=" M 20 0 C 50 0 50 35 20 35 L 15 35 L 15 45 L 0 32 L 15 19 L 15 29 L 20 29 C 44 29 44 6 20 6"> <!-- Define the border color of the arrow. --> <stroke> <SolidColorStroke color="#888888"/> </stroke> <!-- Define the fill for the arrow. --> <fill> <LinearGradient rotation="90"> <GradientEntry color="#000000" alpha="0.8"/> <GradientEntry color="#FFFFFF" alpha="0.8"/> </LinearGradient> </fill> </Path> </Graphic>
    you will have to scale the arrow according to your needs

  • Draw lines with a specfic width and color

    how to do it?
    if given x arrays and y arrays, then connect points.
    public void drawLine(int x1, int y1, int x2, int y2)This function doesn't have width and color.

    thanks.
    I met a problem that the color is obtained by
    calculating RGB.
    color=a*R+b*G+c*B;
    a+b+c=1;
    How to get it?huh?

  • How to draw and modify double lines with Adobe Illustrator?

    I need tot draw roadmaps. This should be easy using the pen tool, but my problem is that I cannot find a way to draw double lines with this tool. I have seen many fancy border styles and patterns. Some do have double or even quadruple lines, but the problem is than that they have an offset from the vector line and the space in between the two lines (the edges of the road) cannot be filled. Somewere I also found some pens called 'Double line 1.1' to 'Double line 1.6' but they also had an offset from the vector and I could not chance the size of the lines and the interval in between them independently.
    What I am looking for is a way to draw two lines in parallel and have the option tot fill the space in between with a color or even a pattern.
    The color and size of the lines should be changeable to make a distinction between several types of road.
    Is there an existing set of pencils for this purpose? It would be nice to be able to draw not only roads, but also railways and rivers!
    I am surely not the first person who needs to do this?
    I use AI version 6

    Jacob,
    Thanks for the answer. I have been searching for a solutions for a long
    time, but today I found the solution on a forum (not on this one). That
    solution is exactly as you described below, i.e. using a coloured line and a
    narrower white line on top of the first one. My problem was that I did not
    know how to create a custom brush based on the two lines. Now I know how to
    do that. However, I still have the problem. I can now draw double lines and
    I can change the color of the background line (the coloured one) but I
    cannot change the white colour of the narrower line. I assume that I have to
    completely redefine a new brush when I need to change that colour too.
    Regards,
    Rob Kole
    Van: Jacob Bugge [email protected]
    Verzonden: donderdag 28 maart 2013 17:04
    Aan: RKOLE
    Onderwerp: How to draw and modify double lines with Adobe
    Illustrator?
    Re: How to draw and modify double lines with Adobe Illustrator?
    created by Jacob Bugge <http://forums.adobe.com/people/Jacob+Bugge>  in
    Illustrator - View the full discussion
    <http://forums.adobe.com/message/5186535#5186535

  • How to draw a bar with Gradient Color ?

    Hi,
    How to draw a bar with Gradient Color ? I see
    in Java Help about GradientPaint, maybe this
    can help ?
    Thanks in advance,
    Michael

    Try to use this in you paint method :
    public void paint(Graphics g)
    Graphics2D g2D = (Graphics2D) g;
    // Get the height & width
    int width = this.getWidth();
    int height = this.getHeight();
    // Create the gradient paint
    gradientPaint = new GradientPaint(0,0, Color.blue, width, height, Color.red);
    g2D.setPaint(gradientPaint);
    g2D.fillRect(0,0, width, height);

  • How to draw centre line with white background?

    Hi,
    I need to draw a centre line with white background on it. Now i'm doing it by pasting the main line in the background and giving it white so that it shows a white backgound. Is there a way to do just by drawing like normal lines. If we can, is it possible to control the white backgound only to show less than the original line in both ends. This is for illustrations i do for exploding views and i need to draw this line to show it as assembly drawing.
    Thanks
    Manoj

    1. Draw a white line segment of desired width (stroke weight) with round cap (Stroke panel) for the background line.
    2. Copy, Paste in Front
    3. Change to desired foreground color, butt cap, reduce stroke weight.
    4. Opt/Alt–drag foreground segment to overhang background segment by desired amount on both ends.
    5. Select both segments and drag to Brushes panel, choose Art Brush.
    6. In Art Brush Options choose Stretch Between Guides, drag guides somewhere to the inside of curve of round caps. (If in step 3 you chose black, set Colorization Method to Tints. Then you can select any color for the foreground segment by setting the stroke color.) Name the brush as you choose. Click OK.
    7. Draw any path and apply the brush.

  • How can i draw a straight line with a brush?

    i know the pencil tool can draw a straight line with anchor points. is there a setting for the pencil tool that can mimic a 30 point brush, low hardness (faded side to side) at about an 80 degree angle or is there a way to make a straight line with the brush tool using a mouse?
    i know this image is a bit blurry. what i want to do is replace the 2 white angled lines with the 30 point brush effect 230 pixels wide. the project on the left of the pic is a 2ft by 4ft canvas. any ideas? please help.

    Hello Mike, I wonder if you can help, no one else can. I asked this question on the forum in August Brush line fades on start with anchor points
    Only on Photoshop CC does this happen, drawing a diagonal line with shift key, instead of line starting dark & then fading out, it does the reverse, starts on nothing & then darkens towards anchor point. I am on trial versions & can't purchase until this is cleared up.

  • Flash IDE unable to draw pure black line with zero alpha

    As Title
    Use line or pancil tool in Flash CS 5.0 or 5.5.
    Choose pure black color #000000 and zero alpha.
    Try to draw line on the tage.
    result : stage is empty

    can u tell me what u are trying to achieve? this is very criptic

  • Problems with gradient tool bar not remaining on object applied in illustrator cc

    Hello,
    when applying a gradient to a rectangle i will get the gradient tool, click on the left half of the object i want it applied to and while holding down drag a line to thew right hand side of the object. the problem the arises when i release the mouse as the gradient bar that has been shown while holding down then disappears. this applies the gradient but the issue is that all gradient editing has to be done on the gradient window as a posed to live on the object slowing the work flow.
    I am not sure what I'm doing wrong but help would be much appreciated.
    Kind regards
    Joseph

    Just for the sake of learning how to use the tool i start with drawing a rectangle then click and drag the gradient tool across the box. i see the gradient line appear but when i release the mouse the gradient line disappears
    i end up witha box with a gradient but all my editing has to be done in the gradient box as shown in this screen shot:
    What I'm wanting to achieve is how the lades hat is edited in this video Working with gradients | Visual Design CS4 | Adobe TV

  • Draw line in illustrator cs2

    hi,
    i am new to adobe illustrator cs2.
    is there any other method to draw line other than s
    setEntirePath().
    thanks in advance,

    Hello David,
    It sounds to me like you're witnessing the double edged sword of an incredibly sharp display. The contrast and clarity of LCD displays makes it very easy to see razor-thin lines with ultra fine definition, but this comes at a cost.
    Font smoothing (a.k.a. font aliasing) is one example of where most LCDs are so sharp that most people prefer to have their text "fuzzed" up a little bit.
    A one pixel wide diagonal line is another example that you've discovered - the display is so sharp that there is no blurring around the "steps" of the line. You can see every change in gradient, because as sharp as your CRT might've been, it was blurring the line just slightly - enough to enhance the illusion that your diagonal line was a line rather than a series of adjacent "steps". No software package that I know of has bothered to implement "line smoothing" yet, probably because you need the detail for accuracy when putting lines together.
    Sort of related to this, big screen HDTV plasma and LCD monitors look amazing in the store when tuned to an HD station, but most people would be floored if they say how grainy and pixelated a standard cable signal looks on the same display. It's literally like watching an old VHS tape, because the display is so sharp it resolves detail that is invisible on a "lesser" display.
    Neat human trick: get used to working with your LCD, and then go back to the CRT if you have a chance. How does the CRT look? I thought I needed glasses the first time I switched back!
    -Shawn

Maybe you are looking for

  • Netgear PCI Wireless Card in a Power Mac G4

    I have a Netgear PCI Wireless Card MA311 and I'm trying to make it work in my Power Mac G4. Does anyone know of any drivers I can use or any way I can get online with it? An alternate option is the Linksys Wireless-B USB adapter (ver.2.8) that I have

  • Spark ButtonBar is it possible to add a "custom" button?

    Either is it possible to add custom ButtonBarButton components to a spark ButtonBar? For example: <s:ButtonBar> <s:ButtonBarButton /> <s:ButtonBarButtonWithImage /> <s:ButtonBarButtonWithChildren /> </s:ButtonBar> Or create a ButtonBarButton with chi

  • Manage multiple structure mapping

    Hello all, i have a scenario MAIL -> PI -> MAIL. I am working with standard XSD with multiple external references and namespaces for my outbound and inbound structureq. All messages have different structures that i cannot merge because of different n

  • Key Pattern Filter

    Hi, I use naming conventions for the keys, and would like to query based on a pattern in the key. e.g. all keys that start with "x" I'm using Strings for my keys. How do I write a filter to do this? I tried creating this Filter: import java.util.rege

  • Exp file default location?

    hi, I did an interactive export on main server (oracle 7 on Open server) it was the first screen with $ sign I don't know which directory I was in. Now I am searching /u/oracle7/ for exp.dmp file with my FTP client to move file but it is not here, I