Setting the fg/bg color of a 1 bit image

Is there a simple way to set the foreground and background colors of a one bit image? What I need to do is draw a bitmap on a Canvas component where all black pixels in the loaded (gif) Image paint as my custom foreground color and all white pixels paint as my custom background color. I'm thinking maybe something like drawing a filled rectangle using the bitmap as a mask?? I am porting code from Windows, and this is something that is kind of built in to MFC.
Currently I have this working in Windows running Java 1.4.0, but it wont work on a Macintosh running Java 1.1 (or 1.2?). I'm wondering if I stumbled upon a quirk that only works in Windows?
This is the code from my Canvas object paint() method:
g.drawImage(bitmapUp, 0, 0, getForeground(), this);
g.setXORMode(getForeground());
g.drawImage(bitmapUp, 0, 0, getBackground(), this);
'bitmapUp' is a one color gif image with transparency. Basically anything that is not transparent gets filled with my foreground color. It seems like this is not the best way to go about things (even if it worked on all platforms).
Test it here if you want:
http://www.wizardmaster.com/test/muzakaster/
- Thanks, Markus P.Spanglestein

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
public class TwoTone {
     static final int SIZE = 200;
     static BufferedImage image = new BufferedImage(SIZE,SIZE,
               BufferedImage.TYPE_BYTE_BINARY); //Black&White
     public static void main(String[] args) {
          JFrame f = new JFrame("TwoTone");
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Graphics2D g2 = image.createGraphics();
          g2.setPaint(Color.WHITE);
          g2.setStroke(new BasicStroke(6f));
          g2.draw(new Ellipse2D.Float(0,0,SIZE, SIZE));
          g2.dispose();
          final JLabel label = new JLabel(new ImageIcon(image));
          label.addMouseListener(new MouseAdapter(){
               public void mousePressed(MouseEvent evt) {
                    IndexColorModel icm = (IndexColorModel) image.getColorModel();
                    int[] rgbs = new int[2];
                    icm.getRGBs(rgbs);
                    Color c0 = JColorChooser.showDialog(label,
                         "Choose background", new Color(rgbs[0]));
                    Color c1 = JColorChooser.showDialog(label,
                         "Choose foreground", new Color(rgbs[1]));
                    rgbs[0] = c0.getRGB();
                    rgbs[1] = c1.getRGB();
                    icm = new IndexColorModel(1, 2, rgbs, 0, false, -1, DataBuffer.TYPE_BYTE);
                    image = new BufferedImage(icm, image.getRaster(), false, null);
                    label.setIcon(new ImageIcon(image));
          f.getContentPane().add(label);
          f.pack();
          f.show();
}

Similar Messages

  • Setting the Border background color

    Post Author: gravy
    CA Forum: .NET
    Hi,
    Using the API I'm trying to set the Border background color to transparent but I can't. I want to set the background color to match what the designer does when you uncheck the Background option on the Border tab of a TextObjects properties.
    Can anyone help?

    What release are your using?
    do you want to set this dynamically, for one portlet or all. you may what to use
    'Themes'
    "Jorgeana" <[email protected]> wrote:
    >
    Does someone have a sample of setting the portlet border and background
    color ?
    I´m having problems using "setPortletBGColor" !! Tks

  • How to set the default component color

    Hi
    I have a JFormattedTextField which is part of a form. Based on user input, if there's something wrong in it, then I colour it pink. I also make it editable(true) or editable(false) based on the mode that I am.
    Problem I have is that once I set the color of it once (to pink), then it won't change the colour of the Textfield based on mode when I call setEditable(true/false) anymore. I want it to "unset" the colour that I did and not have to colour it everytime. I don't want to have to manually change the colour after I set the colour once.
    Can anyone help me with this?
    Thanks!

    Thanks camickr !
    That's exactly what I was looking for. (let the UIManager take care of colors of the textfield whether it's editable or not)
    as for 2nd reply my code is in several files. IF i have to shorten it, it would be:
    it's just a pseudo code and incomplete but it gets the idea across
    * this function automatically changes colors of textfield based on mode which can change any time
    if(mode == NEW_MODE)
    textField.setEditable(true);
    else
    textField.setEditable(false);
    * This function sets the color during save time
    public void save()
    clear();
    if(textField.getText() == null)
    textField.setBackGround(Color.PINK);
    public void clear()
    //reset the color if it's pink or to the original color
    //the color must update once we change mode which is independent of this
    public void open(Patient p)
           clear();
           displayPatient(p);
    }

  • Setting the global Font color for an application

    Hi,
    Can anyone tell me how I can set the default font colour for an application. I understand it involves using the UIManager class to somehow get/set the defaults but im stumped after that.
    Thanks in advance

    Since you really need to change foreground and etc. Use the following code to list all the resources that may set the font colors you want (the example only excludes background color resource, so it may set more than what you want, you need to pin point specific color resouces from the print out to suit your own need).
    UIDefaults defaults = UIManager.getLookAndFeelDefaults();
    for(Iterator i = defaults.keySet().iterator(); i.hasNext();) {
    String name = (String)i.next();
    Object value = defaults.get(name);
    if (value instanceof ColorUIResource && !name.endsWith("Background")
    && !name.endsWith("background")) {
    System.out.println(name);
    UIManager.put(name, new ColorUIResource(Color.red));
    }

  • Setting the tab text color in JTabbedPane

    How do I set the text color of tabs? I have to make the text color of one tab to blue and the other tab text color to red. Thanks.

    I tried giving the below but text color is the default it did not change. 'this' is the JTabbedPane. Any other suggessions? Thanks.
    this.setForegroundAt(0,Color.blue);
    this.setForegroundAt(1,Color.red);

  • Setting the TabbedPane background color

    Hello everyone,
    The following code does not set the tabbedpane's background color:
    class LIB
    public static void main (String ... args)
    javax.swing.JTabbedPane t=new javax.swing.JTabbedPane();
    t.setOpaque(true);
    t.setBackground(java.awt.Color.RED);
    t.setForeground(java.awt.Color.GREEN);
    t.add(new javax.swing.JLabel("HELLO"));
    t.setTitleAt(0,"Tab 1");
    javax.swing.JFrame f=new javax.swing.JFrame();
    f.getContentPane().add(t);
    f.pack();
    f.setVisible(true);
    }What is wrong with thid code ??
    Thank you

    You had an answer with in the first 10 minutes. And your post was on one of the three most popular forums.10 minutes, that is true. But then I sent a reply on Dec 6, 11:43 AM and did not get an answer until Dec 8, 5:44 AM (that is after I had already posted the same problem on another forum).
    Also, if you ever do need to post the same question on another forum at least put a link between the two.Since I am new to the forum, I don't know how to do that. Would you be kind enough to tell me how.
    Thank you

  • How can I manually set the Camera Roll to start at an arbitrary image number, i.e., IMG_0553.JPG?

    I have an iPhone 4s, currently with iOS 5.1. I use it with my desktop computer which runs Windows Vista (and has the latest iTunes installed 10.6), and my laptop, which has Windows 7 and the latest iTunes. I use iCloud and Photo Stream primarily with the desktop computer.
    Since receiving the iPhone on launch day, (Nov...14?, 2011), I've had to "Set [it] up as new" twice, most recently this morning.
    After setting it up as new, the Camera Roll starts over with pictures at IMG_0001.JPG. That means, that, after my first picture after this latest reset, I'll have three pictures all initially named IMG_0001.JPG, and instead, I'll have IMG_0001.JPG, IMG_0001 (1).JPG, and IMG_0001 (3).JPG in my photo library. This robs me of the ability view my photos sorted by name, which is often the default sort order in my photo folders, and still see them chronologically. I can, of course, sort them by Date Created, but it would be more flexible if I could do either one.
    Therefore, I'd like to set Camera Roll to start numbering its photos starting with whatever the next number higher than all the rest in my library would be, in this case, IMG_0553.JPG.
    I can't just use Windows to go into the iPhone's Camera Roll and rename the first photo I take to be IMG_0533.JPG. The iPhone just doesn't allow tampering with the filename that way. Can I even count on Camera Roll to note the presence of the highest numbered file in its folder and then just name the next picture taken the next higher number?
    Since I have no reason to believe that I won't have to reset the phone again, I'd like for whatever method I find to be one that I can use to set the Camera Roll to start at whatever other number I need when the time comes.
    Suggestions welcome...
    I have an idea that I'm about to try...I'll report back if it succeeds...
    UPDATE: So what I tried to do was, with an empty Camera Roll, I uploaded a picture with the filename and number that I wanted (IMG_0533.JPG) into my Photo Stream, then saved that picture to my Camera Roll, hoping it would retain the file name, and then Camera Roll would just use the next number. That didn't work. It saved into Camera Roll as a brand spanking new IMG_0001.JPG.
    Message was edited by: Shadowhawk kTreva

    To bring the solution into this thread, here are the consolidated solution bits from the other thread:
    I installed i-Funbox and found the file referenced in that other thread ( iPHone / Raw File System / PhotoData / MISC / DCIM_APPLE.plist ).
    That file has a section that I'm replicating below that is the part that that original poster edited, and is the part that needs to be edited for my problem also:
              <key>DCIMLastFileNumber</key>
              <integer>5</integer>
    That other thread's original poster wanted his photos to start with 0001, so he set his "DCIMLastFileNumber" to 0.
    I wanted my next picture to be IMG_0553.JPG, so I edited my file's 5 to 552.
    I did that editing by first copying the file to my laptop (using the Copy To PC command), then opening it up in a text editor. I did the edit, then saved it.
    After that, I used i-Funbox' "Copy From PC" command to copy the edited file back to the same location, hoping that the program would take care of any permissions, etc.
    I had to do the Copy From PC twice.
    On the first try, it seemed to copy just fine, but, for whatever reason, the copied file was empty when I opened it for verification. But I tried again, and that seemed to work (opened for verification, and it was as expected).
    I then unplugged it, copied a picture from the Photo Stream to the Camera Roll, then plugged it into my laptop to verify the filename. It was exactly as I wanted: IMG_0533.JPG. Then, for fuller verification, I unplugged it, took a new picture, then replugged it in, and confirmed that the next pictures (I had HDR on) were IMG_0534.JPG and IMG_0535.JPG.
    ...and that's where I am. Nothing has crashed yet, too, which is all to the good ('course, I haven't done anything else with the phone yet).
    Fingers crossed!

  • Set the default border color for text fields?

    My InfoPath 2010 text field borders are defaulting to white for some reason (which basically means invisible on a white form).  Do y'all know if there's a way to set this somewhere?  Drop-downs (for example) are still showing up as the light-grey.
     Maybe I accidentally tweaked it but now I can't see where.

    Hi
    if you want the change color of all the field in new form,edit form and view form then share point designer can help you in that case.
    Or you can user page layout in info-path form
    https://support.office.com/en-in/article/Introduction-to-designing-a-form-in-InfoPath-2010-d78eef0a-e44b-4919-8032-b4a021dc30ea
    Please mark the Answer and Vote me if you think it will help you

  • Guide to set the cell background color and foreground color

    hi all,
    I am using a tool called POI for excel from java. IF any body know POI please tell me the various colors --------> equivalent Short number. I need the color and equavlent number for better usage.
    please respond as early as possible.

    Hi
    Use the class HSSFColor at org.apache.poi.hssf.util and the inner classes defined, for example (sample form jakarta.poi):
    style.setFillForegroundColor(HSSFColor.ORANGE.index);
    or for a cell
    cell.setFillForegroundColor(HSSFColor.ORANGE.index);
    If you need know what index for a color may be at source code you can see the implementation.
    Hope this helps

  • How can I set the foreground/background color after using CSPickColor()?

    Hello all,
    Is there any one who has experience on changing foreground or background color using Photoshop Plug-in SDK?
    Currently I can get a returned color from CSPickColor(), but have no idea how to use it.
    Thanks!

    You can try a linear-gradient that starts at gray 0% and ends at white <whatever the percentage the thumb is at>.
            final Node track = slider.lookup(".track");
            slider.valueProperty().addListener(new ChangeListener<Number>() {
                public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                    double pct = (t1.doubleValue()-slider.getMin())/(slider.getMax()-slider.getMin())*100;
                    track.setStyle("-fx-background-color: linear-gradient(to right, gray, gray " + pct + "%, white" + pct + "%, white);");
            });Edited by: dgrieve on Apr 12, 2012 12:19 PM

  • How can i set the alternating colors for a table rows

    Dear All,
    Please any one help me how can i set the Alternating colors for Table Rows.
    i created a theam there i set the background alternating color to brown and i set the table design properity to alternating. but it is not reflecting.

    Hi,
    The design property in Table properties should work for your requirement. Select "alternating" value for design.
    Please see the API below:
    design
    Determines the appearance of the table. The property design can take the following values and is represented by enumeration type WDTableDesign.
    alternating - The table rows are displayed alternately in a different color.
    standard - The table background has one color. The individual table rows are displayed with grid net lines.
    transparent - The table background is transparent. The individual table rows are displayed without grid net lines.
    Check whether you have changed the right property or not? Also table should contain more than one rows to test this scenario.
    Regards,
    Jaya.
    Edited by: VJR on Jun 17, 2009 6:43 PM

  • Setting the font style and color for FileChooser labels

    Hi Friends,
    I have a certain standard Font style and color set for my application GUI. Now, I want to set the style and color for Swing components like FileChooser. Is it possible ?
    Also is it possible to localize JOptionPane ?
    Please advise.
    Best regards,
    Harilal.

    Does anybody knows how to do that?

  • How to set the default the highlight/shawdow colors for BevelBorder?

    Is there a way of setting/changing the default highlightOuter/Inner and shawdowOuter/Inner colors of a BevelBorder?
    I assume that these values are defaulted from the LookAndFeel settings and that there most be a way of changing these like one does for other defaulted values. For example:
    UIManager.getDefaults().put("TabbedPane.shadow",new Color(190,220,255));
    to set the TabbedPane shadow color.

    This is the JDK1.4.1 source code. So it doesn't look like there is any way to change the default since it is based on the components background color:
        public Color getHighlightOuterColor(Component c)   {
            Color highlight = getHighlightOuterColor();
            return highlight != null? highlight :
                                           c.getBackground().brighter().brighter();
        public Color getHighlightInnerColor(Component c)   {
            Color highlight = getHighlightInnerColor();
            return highlight != null? highlight :
                                           c.getBackground().brighter();
        public Color getShadowInnerColor(Component c)      {
            Color shadow = getShadowInnerColor();
            return shadow != null? shadow :
                                        c.getBackground().darker();
        public Color getShadowOuterColor(Component c)      {
            Color shadow = getShadowOuterColor();
            return shadow != null? shadow :
                                        c.getBackground().darker().darker();
        }Of course you can specifiy the color in the BevelBorder constructor.

  • How to set the background for all components?

    hi
    Does anybody know how to set the DEFAULT background color in an application.. I need it because in jdk 1.3 the default bgcolor is grey and in 1.5 it is white.. and I wish white as well.. so to make it also white in jdk 1.3 I need to use the (a bit annoying) anyContainer.setBackground( Color.white ); and these are lots in my app.. So my question is: is there such an overall class with a function (say UIManager.setComponentsBackground( Color color ) ) for such a purpose?
    any tip or link would be greatly appreciated

    Does anybody know how to set the DEFAULT background color in an applicationthis might get you close
    import java.awt.*;
    import javax.swing.*;
    import java.util.Enumeration;
    class ApplicationColor extends JFrame
      public ApplicationColor()
        setApplicationColor(Color.RED);
        setLocation(400,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel jp = new JPanel(new BorderLayout());
        jp.add(new JTextField("I'm a textfield"),BorderLayout.NORTH);
        jp.add(new JComboBox(new String[]{"abc","123"}),BorderLayout.CENTER);
        jp.add(new JButton("I'm a button"),BorderLayout.SOUTH);
        getContentPane().add(jp);
        pack();
      public void setApplicationColor(Color color)
        Enumeration enum = UIManager.getDefaults().keys();
        while(enum.hasMoreElements())
          Object key = enum.nextElement();
          Object value = UIManager.get(key);
          if (value instanceof Color)
            if(((String)key).indexOf("background") > -1)
              UIManager.put(key, color);
      public static void main(String[] args){new ApplicationColor().setVisible(true);}
    }

  • Set current row background color

    during post-query process, if record meets condition,
    i would like to set the color of entire line.
    thank you
    [email protected]

    Hi Jim,
    create a visual_attribute named e.g. PQR and set the desired background color.
    In post-query trigger:
    declare
         currec number := Get_block_property('block1', current_record);
    begin     
    if "condition" then
         set_item_instance_property('item1',     currec,visual_attribute,'PQR');
         set_item_instance_property('item2',     currec,visual_attribute,'PQR');
         set_item_instance_property('item3',     currec,visual_attribute,'PQR');
    end if;
    end;
    Good luck,
    Monica

Maybe you are looking for