Fill shape with 2 solid colors

Hi,
I was wondering if there is an easy way to fill a roundRectangle2D with 2 solid colors. For example that 1/3 is blue and the other 2/3 is green? I've looked in the java graphics2D api but can't find anything. GradientPaint isn't what I'm looking for, so is there any other easy option.
Or is it the hard way and you need to draw several shapes on top of each other?
class EventPanel extends JComponent{
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        Double widthRect = (double) this.getWidth();
        Double heightRect = (double) this.getHeight();
        Double arc = widthRect / 20.0;
        Float strokeSize = 1.0f;
        Double offset = strokeSize / 2.0;
        Double adj = offset * 2;
        RoundRectangle2D.Double roundRect = new RoundRectangle2D.Double(offset, offset, widthRect - adj, heightRect - adj, arc, arc);
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHints(rh);
        g2.setPaint(new Color(30, 144, 255, 80));
        g2.fill(roundRect);
        g2.setPaint(Color.BLACK);
        g2.setStroke(new BasicStroke(strokeSize));
        g2.draw(roundRect);
        g2.dispose();
    }Similar to this: Example Don't mind the quality. Not good at art.
Edited by: Kligham on 26-aug-2010 22:55
Edited by: Kligham on 26-aug-2010 22:58
Edited by: Kligham on 26-aug-2010 23:01

Or is it the hard way and you need to draw several shapes on top of each other?What's so hard about that? But if you're looking for an alternative approach, you can do it with Graphicd$setClip(...)
GradientPaint isn't what I'm looking forWhat about LinearGradientPaint?import java.awt.*;
import javax.swing.*;
public class StepGradientPaint {
  float[] fractions = {0F, 0.5F, 0.50001F, 1F};
  Color[] colors = {Color.RED, Color.RED, Color.GREEN, Color.GREEN};
  Paint paint = new LinearGradientPaint(0F, 0F, 200F, 0F, fractions, colors);
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new StepGradientPaint().makeUI();
  public void makeUI() {
    JPanel panel = new JPanel() {
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setPaint(paint);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.dispose();
    panel.setPreferredSize(new Dimension(200, 200));
    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}db

Similar Messages

  • Fill shape with a pattern

    Hi,
    I am drawing some shapes using java.awt.Shape, java.awt.geom.Area and java.awt.geom.GeneralPath and would like to know whether it's possible to fill these closed shapes with a color - or even better - with a pattern.
    Thanks!

    er, yes, why not?
    http://java.sun.com/docs/books/tutorial/2d/geometry/strokeandfill.html
    -Puce

  • Iphoto 9.6.1 how do I leave pix space blank, filled in with background color

    I'm making a photo book using iPhoto 9.6.1  How do I leave pix space blank, filled in with background color?   I want half the page taken up with a picture and the other half colored in.

    There's actually an option to iPhoto Library Manager that makes this all almost work flawlessly (and work well enough for me). With the exception of Photo Albums that the pictures are part of, if you select "Library > Rebuild Library..." and then select "Scavange Orphaned Photos" check box, then it will put all the Photos and Movies into the "Scavanged Photos" album. It matches the modified pictures to their originals. It preserves all the event names and photo groupings. All the meta-data I could spot check it got right (although with 44k pictures, I only checked a few). If you fail to select this option, however, it will find no pictures at all when it goes to rebuild the library (this was true of the small test libraries I tried as well as the big, monster 280GB library I just finished converting). The conversion process took 5 hours to run, but in the end produced a library my new iPhoto can read and that I can interact with. I'll be suggesting to the iPhoto Library Manager folks a notation that would estimate the number of Orphaned Photos and display that number as a hint to the user that the option would be useful.
    Edited: Thanks to Old Toad and LarryHN for their help in finding which of the many options to transition this library to 9.5.1 I needed to use. Sorry if I took my frustration of the past several months out on you guys for generously trying to help me.

  • Fill with solid color problem

    Please, kindly  help to find out why Photoshop fills a selection with transparent color while opacity and fill of a layer  are set to 100% and blending modes are “normal”
    Thank  you!

    Olga,
    In that case I have no idea, sorry.
    Only thing I can think off is to reset the preferences. When Photoshop is acting up this will sometimes make Photoshop behave.
    But before you do this make sure you backup your actions, gradients, shapes, brushes etc or you will lose the ones you made yourself.

  • Fill Shape With Picture? student

    Hello,
    Is there a way to draw a shape and then fill it with a picture from a file?
    Also, I'm not sure if that is the best way to put pictures into the program I am making... any advice?
    - Jason

    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    public class ShapeImage {
        private JScrollPane getContent(BufferedImage source) {
            BufferedImage framed = getFramedImage(source);
            Box box = Box.createHorizontalBox();
            box.add(Box.createHorizontalGlue());
            box.add(new JLabel(new ImageIcon(source)));
            box.add(Box.createHorizontalGlue());
            box.add(new JLabel(new ImageIcon(framed)));
            box.add(Box.createHorizontalGlue());
            return new JScrollPane(box);
        private BufferedImage getFramedImage(BufferedImage image) {
            int w = 160, h = 160;                   // shape/frame size
            int type = BufferedImage.TYPE_INT_RGB;  // other options
            BufferedImage out = new BufferedImage(w, h, type);
            Graphics2D g2 = out.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            Ellipse2D circle = new Ellipse2D.Double(0,0,w-1,h-1);
            // Center image in circle frame.
            double x = (w - image.getWidth())/2;
            double y = (h - image.getHeight())/2;
            AffineTransform at = AffineTransform.getTranslateInstance(x,y);
            g2.drawRenderedImage(image, at);
            // Mask out image pixels that lie outside of the circle disk.
            Area mask = new Area(new Rectangle(0,0,w,h));
            mask.subtract(new Area(circle));
            // Use the desired background color.
            g2.setPaint(UIManager.getColor("Panel.background"));
            g2.fill(mask);
            // Draw the circle/shape frame.
            g2.setPaint(Color.red);
            g2.draw(circle);
            g2.dispose();
            return out;
        public static void main(String[] args) throws IOException {
            BufferedImage image = ImageIO.read(new File("images/cougar.jpg"));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new ShapeImage().getContent(image));
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
    }

  • How to fill shape with pattern... and cut outline?

    I have an old plotter that works great for making drawings. I can fill a shape with a pattern, convert the pattern to outlines (or strokes) BUT my problem is that the outline of the original shape remains, and this will be plotted. I do not want an outline of the original shape, I want the pattern to create the shape. Any ideas? Also, shape stroke remains part of the art (even if stroke = none before object/expand/ungroup/divide)

    aha!
    made scribble
    expanded scribble
    put shape in front
    select all, ctrl+7 (masked scribble to shape)
    pathfinder/crop
    solved. thanks! sishamDSS!

  • Fill shape with pen tool

    Hi there,
    this might be a very simple question, but couldn't figure it out ..
    I made a Title in Premiere and I want to draw a shape with the pen tool.
    However this only results into a shape with line instead of it being filled.
    So my question: how to draw a filled shape in Premiere Pro with the pen tool?
    Thanks in advance

    Set Graphic Type to Filled Bezier, that will fill the shape.

  • Fill shape with image

    Hi,
    I have drawn a curved box and wish to fill it with and image. I'm having trouble with this as the image is a rectangle and the box has curved edges. Is there any way which you can fill the box with the image. It will not matter if edges of the image are cut out.
    Thanks in advance
    Euan

    > I'm having problems copying layers.
    I take it this is a separate question? (no need to copy layers for what I suggested before)
    Highlight a layer in the layers palette, then press CTRL+J (windows) or Command+J (Mac) if you want a duplicate.
    Otherwise, highlight the layer, press CTRL+A (windows) or Command+A(Mac) , then CTRL+C (windows) or Command+C (Mac) and then you can paste it where you want.

  • REQUEST: Crop Beyond Image and Fill "Background" with Choosable Color

    One of the features I'm missing most is the ability to extend the crop tool rectangle beyond the image and have the "new" area filled with some choosable color (like the background color in the crop tool in Photoshop). This is especially useful when turning the image at an angle. Actually I like the usability of the Lightroom approach - turning the image behind the rectangle - better than in Photoshop where the rectangle is turned.

    Lens corrections - manual - scale, set to less than 100 and try again.  Background color isn't selectable.

  • Photoshop 6.0 - Fill arrow with black color

    Hello,
    I've added an arrow to a jpeg image but it is white. How do I make the arrow solid black? Can you provide details (which menu to use, etc.) b/c I am a beginner.
    (using Photoshop 6.0)
    Thank you for your help!

    Robert...
    Are you talking about Photoshop
    Elements
    ? Some of what you've written sounds like you are.
    Yes, the Pen Tool is available in Photoshop 6 and newer (but not in Elements 6); Custom Shapes are available in PS
    Elements
    6 and Photoshop 6 and newer versions of both. From what I can discover through the online documentation Elements 6 & 7 will allow you to edit anchor points of a Custom Shape (I don't know to what extent, though), but it doesn't have the tools and function to allow you to create your own vector-based objects.

  • Fill Shape with text

    I'm trying to Fill Number 9 with text but I always get this :
    I want to have the circle inside without text !
    Someone Can help me ?
    Sorry Im newbie  :/

    I know you know a lot more about the internals than I do, Chris, and I hate to be argumentative, but I'm just not seeing the difficulty...
    Consider that the text placement code as written today can deal with the following (hastily drawn path).
    And as Paulo points out, Photoshop clearly already knows what's "inside" and what's "outside" a complex path.
    Emil makes a good point about the readbility suffering if one has to follow it across breaks in the figure...
    Still, who's to say whether someone wouldn't come up with a good use for this.  Bottom line is this - here you have a user expecting Photoshop to do something that's expected, and it's falling short because of the implementation and not allowing him to express his creativity the way he wants....
    This exercise DID bring one new idea to mind possibly for hhtun...  Try using a stencil font!
    -Noel

  • How can i paint a solid color from the color pallet to a new art board

    Hi
    Can any one teach me how i can just paint or pour a solid color in an blank art board.
    Steve

    Draw a box with your Rectangle Tool the size of your art board and fill that with the color you need.

  • How do I get a solid color border?

    I have made a seperate layer for a border.  When I fill it with a color, it has blurred edges and is not a solid color.  I have checked the opacity settings and they are at 100%.  What am I doing wrong?

    If you have several layers, combine the layers. To do that, open a blank layer at the top of the stack, and press CTRL+SHIFT+ALT+E
    Go to Select>all
    Open a blank layer above the layer created in step #1
    Go to Edit>Stroke(outline)selection. Select color, width, and position inside

  • Create Layer with BG Color

    function layer() {
    Application.activeDocument
    Application.backgroundColor (blue = 34, green = 12, red = 1)
    layer()
    I want to create a NewLayer, with a specific background color, the code above is not working.

    You really need to read the scripting guide.
    Application is a Photoshop Class. To reference that class you use app. So the first line in the function should be app.activeDocument. Even then that line does not do anything except return a document object. If you do not store the returned object in a variable you might as well delete that line.
    app.backgroundColor is an app property, not a method. So the () are not used. That property either returns a SolidColor object that is the current background color or you can assign it a new color something like this
    var myColor = new SolidColor();
    myColor.rgb.red = 1;
    myColor.rgb.green = 12;
    myColor.rgb.blue - 34;
    app.backgroundColor = myColor;
    But that does not create a new layer and I am not sure what kind of layer you want. Do you want to create a new artLayer then fill it with your color or do you want to create a new SolidFill adjustment layer using that color.
    To create a new artLayer you do something like
    app.activeDocument.artLayers.add();
    To create a new adjustment layer you would need to use Action Manager( scriptlistener ).

  • Cannot fill a rectangular shape with Color

    HI,
    I cannot fill a rectangular shape with color. I know it was working at some point. I checked the ID help, and did a search here with no results.
    I have the tool bar set correctly I think (see below). The object isn't behind other objects either, - that I can see. I do see it when I use the scroll bar so it's there somewhere. (see Pink box below).
    Any ideas profound or otherwise?
    Thenk Yew.

    You haven't got 100% transparency applied to it?  Then it might appear while scrolling because of a slightly delayed display.  That's all I can think of at the moment...
    The other thing to try if you suspect an individual document is playing up is to export it to inx or idml and open that.  Then another thing is to try replacing your preferences.

Maybe you are looking for

  • I have a problem with my phone n73

    i have a problem with my phone n73 my memory work good and i can see it from gallery but when i want to save some thing it save directelly in memory dont give me choise and when i want to sign ring tone for alert or ringtone or massege i dont see any

  • I cant get my ipod to sync my computer

    i cant get my ipod to sync with my itunes on my laptops

  • Report is not showing a prompt for a Charactorstic Filter Drill Down..!!

    Hello Experts, Recently my project was enhanced with some more new project and mixed it with my project. Now my issue is before my new project was mixed into this project. We have the option..when a material drill down we come to see the Filter windo

  • How can I examine FLV properties?

    I downloaded a FLV file and want to know what size, bitrate, framerate, etc that is was recorded in. If I open the file in Flash 8 Pro it just wants to build a player around it and create a new file. I can't figure out how to directly examine the pro

  • N97 not charging, not turning on, blank screen, fo...

    Ok this is not a conventional way but had the same problem as many others, trailed the internet, tried everything and live some 2hrs away from a nokia service centre. my n97 wouldn't charge nor turn on whatever I just see the little led charge light