Drawing colored bufferedImages on greyscale BufferedImages

I create a BufferedImage from a base black and white image like this:
image = new ImageIcon(image).getImage();
BufferedImage bimage =  new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);Image renders just fine in a JFrame
I then take a color image (a .png icon file) and run it through the same process (it retains its color when test drawn on a JFrame) and then draw the resulting BufferedImage from the icon file on the original image using the g.draw(BufferedImage) method. This resulting image has the icon on it, but the icon, which was originally in color, is now in greyscale. How do I draw color images on top of black and white images and retain the color in the overlay? I have tried using the ConvertColorOp on the overlay inside the draw call and that does not do anything. Any help is greatly appreciated

I then take a color image (a .png icon file) and run it through the same process (it retains its color when test drawn on a JFrame) and then draw the resulting BufferedImage from the icon file on the original image using the g.draw(BufferedImage) method. This resulting image has the icon on it, but the icon, which was originally in color, is now in greyscale.This requires an SSCCE. I don't really understand it: you draw colored image #1 onto #2. Then you draw #2 back onto #1, and #1 is now gray? Under normal circumstances this is impossible. But I'm probably just misunderstanding you. Hence the need for an SSCCE.
How do I draw color images on top of black and white images and retain the color in the overlay?This I can answer: you can't. A black and white image can only contain the colors black and white. You would need to create a new image and draw the black and white one and the colored one onto the new image.

Similar Messages

  • Converting a true color bufferedImage to a 256color?

    Hi,
    Does anyone know how to convert a true color bufferedImage to a 256 color?
    Any help would be appreciated. Thank you

    The simplest way is to choose an arbitrary lookup table then map each rgb pixel to its nearst value in the table. If the table is good for your type of image then the results can be reasonable.
    A differnt way is just to drop the insignificant bits from each of r, g and b then
    combine the reduced r,g and b into a single byte.
    A better way is to compute a lookup table that is optimal for you image (for various definitions of "optimal"). Below are references for three different approachs to do this (taken from the JAI documentation of ColorQuantizeDescriptor). Once you have the optimised lookup table you
    can find the nearest colour in it for each pixel (as in the first approach).
    Median-Cut      Color Image Quantization for Frame Buffer Display, Paul Heckbert, SIGGRAPH proceedings, 1982, pp. 297-307
    NeuQuant      Kohonen Neural Networks for Optimal Colour Quantization, Anthony Dekker, In Network: Computation in Neural Systems, Volume 5, Institute of Physics Publishing, 1994, pp 351-367.
    Oct-Tree      Interactive Computer Graphics: Functional, Procedural, and Device-Level Methods by Peter Burger and Duncan Gillis, Addison-Wesley, 1989, pp 345.
    Cheers
    matfud

  • Getting screen re-draw, color blotches on certain Macs using ARD 3

    Getting screen re-draw, color blotches on certain Macs using ARD 3
    Has anyone seen this. Serious re-draw issues when observing / controlling certain Macs with ARD 3, have seen this sometimes with ARD 2.
    Buttons, boxes get blotched and can not see what you are doing, disconnecting and re-connecting does not solve it.
    Power Mac G5 Dual 2.0   Mac OS X (10.4.7)  

    Have you tried reinstalling the ARD client? Are the systems that have this problem running 10.4.7?

  • Is this the fastest way to create a single color bufferedImage?

    Here is my code but it's slow sometimes. Is there a better way?
    BufferedImage bi = new BufferedImage(1600,1200, BufferedImage.TYPE_INT_RGB);
    int[] rgbs = new int[1600*1200];
    for (int j =0; j<1600*1200; j++)
       rgbs[j] = Color.RED;
    bi.setRGB(0,0,1600,1200,rgbs,0,1600);

    This will be even faster.
    int w = 1600;
    int h = 1200;
    int masks[] = new int[4];
    rv[0] = 0xff0000; // red
    rv[1] = 0x00ff00; // green
    rv[2] = 0x0000ff; // blue
    rv[3] = 0xff000000; // alpha
    SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT,
                                                      w, h, masks);
    final int rawData[] = new int[w * h];
    DataBufferInt db = new DataBufferInt(rawData, rawData.length);
    WritableRaster ras = Raster.createWritableRaster(sm, db, null);
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new DirectColorModel(cs, 32, masks[0], masks[1],
                                         masks[2], masks[3],
                                         false,
                                         DataBuffer.TYPE_INT);
    BufferedImage  img = new BufferedImage(cm, ras, false, null);
    int value = 0xffff0000; // opaque red.
    int l = rawData.length;
    // Set the image to solid color of the value
    for(int i = 0; i < rawData.length; i++)
        rawData[i] = value;

  • All of my colors have turned greyscale!

    Not sure what happened here, but when I draw a box or create
    text and try to fill in with any color, it gets filled with a shade
    of grey. I have no idea how I activated this "feature", but I need
    to get back to color mode. How do I do this?
    Mac OS X 10.4.8 ~ FH MX 11.0.2

    Are you sure they are greyscale and not just much lighter
    tints of what
    you are expecting? If you are working on a layer beneath the
    printing
    bar (in the Layer panel) then you will see a dimmed version
    of
    everything. By default there is a "Background" layer put
    under the
    printing bar. You can either drag the bar to include the
    layer above it
    or just move everything to another layer that is above the
    bar. What
    the Printing Bar is for is it allows you to have objects in
    your file
    that are visible but will not print. The dimmed effect is a
    visual clue
    that tells you it is on a non-printing layer.
    Rich
    Dorian_J wrote:
    > Are you referring to something that should be set in FH
    or in the System
    > Preferences? Nothing has been changed in either
    instance. Not intentionally
    > anyway. My Mac still has color to it. The color problem
    is with FH. There's no
    > color anymore. I can see colors in the palettes but not
    on the canvas/page.
    >

  • How to print color document in greyscale without it being really dark?

    Recently had a customer come in and want some holiday letters printed out. They had a bunch of color photos in the document and when we printed that from Adobe Acrobat to our black and white copier, they came up REALLY black. Everything was much darker that it should have been.
    Normally we work around this issue by placing the document into InDesign and printing it in Greyscale mode from there since you can tell it RGB, CMYK or Greyscale from InDesign. Problem is, if I wasn't here, nobody else would know how to do this and they would probably have to turn the customer away.
    Our counter machine is a WindowsXP machine with the newest version of Acrobat Reader. I saw this link elsewhere here:
    http://forums.adobe.com/thread/948555?tstart=0
    But that doesn't work. When clicking on Advanced button in the print window for Acrobat Reader, the Advanced window doesn't have anything about printing in greyscale. The copier is a Canon iR 7095.
    Any thoughts on what we can do? Basically, I want to be able to have the ladies that work the counter (and the counter PC) to be able to print color documents to the black and white machine without having a problem with the output and having to either come into my office and have me help with it, or having to turn the customer away.
    Any thoughts?

    go to Files --> Add Files to Upload

  • Convert 4-color pdf to greyscale

    I have a four-color pdf that I need to convert to greyscale. The pdf now is CMYK set for Press Quality. What are the best settings for high quality greyscale output in Advanced - Print Production - Convert Colors. I am using Acrobat Professional 8.0. Thanks

    As I said, I have not had much success that way either. Pro had the preflight conversion to Grayscale. Since you have Std, you may have to simply use separate graphic tools to convert to Grayscale and then recreate the PDF. If it is a scanned PDF, then you could simply save to a TIFF, use a graphic package to convert to grayscale (I use Irfanview for this type of simple task) and then move the TIFFs back into a PDF. If you have a mix of graphics and text, you may have to deal with a bit more. If the print converts the text, but not the graphics, you might try extracting the graphics (bitmaps I hope) under the tools menu. Convert those graphics to grayscale and then replace the color images with the new ones. (work on a copy of your PDF just in case something does not work)

  • Photoshop Elements 10 colored image became greyscale

    I had edited an image in a layer.  It is a full color image.  When I  dragged & droped it into a totally different photo, suddenly it appeared in greyscale.  The original image in its original layer is still full color. This has never happened to me before.  What went wrong, & how can I fix it?

    From the menu choose:
    Image >> Mode >> RGB Color

  • Can draw color be changed?

    Is there a way to change the color of the drawing tool to something besides red?

    Yes!  To change the color of an existing Freehand drawing
    Tap an existing Freehand drawing to bring up a menu.
    Tap "Color..." from the menu.
    Select a color from the color palette.
    To change the opacity (transparency) of an existing Freehand drawing, select "Opacity..." from the menu at Step 2 above.
    To change the thickness of an existing Freehand drawing, select "Thickness..." from the menu at Step 2 above.
    Similarly, you can change the color, opacity, and thickness of a handwritten signature and the color and opacity of text markup annotations (highlight, strikeout, underline).
    Adobe Reader will remember the color/opacity/thickness of your choice for each annotation type.
    Please let us know if you have additional questions.

  • How to apply a color to a greyscale linked image?

    I have an old file that I've been working on that another designer started. She had these black and white linked images in there with colors applied to them. Currently, you can click on them with the direct selection tool and see the color that is applied in the "fill" box on the toolbar. But when I tried to place that same image into the document myself, there is no option to apply a color. What did she do to make it possible to apply these colors? I'm using Indesign CS6, by the way.
    Anyone know?
    Thanks!
    ~ Sarah

    Hey Daniel & Mikey,
    Thanks for your responses, but I'm still stuck with my same problem. I should have explained better - what I really need to do most here is relink the exact same file that the old designer was already using. I've moved files around and now the linked needs to be relinked. When I relink the exact same file that was working fine with the color applied before, I run into the same problem again. I cannot direct-select it and change the color of it. It's just not an option. This also happens if I create a new InDesign file and place that file in and try to do it fresh. She did something to that file to make it take a color, but I can't figure out what it might be!
    Maybe some visuals will help me to explain...
    This is the problem layer below. Let's call it "background". It's at 100% opacity, layer effect set on multiply in BOTH examples.
    Here's how I want it to look. There are a few layers in here to create this effect. When I click on "background" with the selection tool, I do not see a color applied. However, if I click on it with the direct selection tool, you can see that a yellow color has been applied.
    Now, when I relink the file (or place it myself), this is the result I get. You can see that "background" has reverted back to its greyscale state. When I try to select it once more with the direct selection tool, all the colors are greyed out and I can't apply a color.
    Anyone know the answer to this puzzle?
    Thanks!

  • Printer Printing with color ink in greyscale mode

    Even you choose to print in black and white only, printer prints with color inks.
    Printer Model : HP C4580

    Hi GT501,
    Thank you for your response!
    Please go through the following:
    Reset the printing system. OS X Mavericks: Reset the printing system.
    Verify and repair disk permissions. Disk Utility 12.x: Repair disk permissions.
    Let's start with those two steps for now, and let me know what happens.
    Hope this is helpful, and have a nice day!
    RnRMusicMan
    I work on behalf of HP
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos Thumbs Up" to say “Thanks” for helping!

  • Tools to Create a Color Effect from Greyscale to Color

    Hi.
    I'm about to pursue shooting a short film. Most of the film will be in black and white. Around the end of the film, I need an effect that will be able to sweep away or dissolve the black and white from the footage or environment and turn the environment back into color again. Are there any tools in After Effects CS5 that can help me achieve this or do I need to use a different program?

    Did you shoot in color? If so, how are you converting to black and white? Just using the Black & White effect? Whatever technique you use, you can just disable that effect for the part that you want to be color again. Since you describe a "sweep" or "dissolve", you can probably just use a wipe transition on an adjustment layer.

  • Thread Safe BufferedImage Drawing

    Hi. Im doing some lengthy rendering into a BufferedImage in a spawned off thread.
    When the rendering reaches a certain point I want to paint the BI to a JPanel, wait
    for the repaint to happen, and repeat about 3 times at which point the thread dies.
    How do i wait for the JPanel to acutally draw the BufferedImage?
    Do i use SwingUtilities.invokeAndWait(new Runnable(){ public void run(){ jpanel.repaint(); }})?
    Ive never used SwingWorker but is this just the sort of task it was created for?
    Thanks.

    petes1234 wrote:
    ..... or is this something you already know? Sorry if it is.no problem. thanks for trying to help.
    i did know that and thats what im doing. in the background thread i am
    doing the rendering onto a bufferedimage. at about 3 "steps" i want a jpanel
    to display the BI.
    i guess its the timing that has me thrown off. like, what if someone resizes
    the jpanel in the middle of rendering? in the paintComponent(Graphics g) method
    how do i handle juggling the BI? am i setting myself up for deadlock?

  • Converting a BufferedImage to an Image

    I have a program with some BufferedImages that the user needs to be able to drag around the screen.
    1) The dragging process is very sluggish when I use BufferedImages, but not when I use normal Images. Is this normal, or is something wrong with my program?
    2) If using Images is the only way to fix this problem, how do I draw my BufferedImages into Images or convert them to Images? They have to start out as BufferedImages so that they can undergo a one-time rescaling operation.

    Where can I find out what a profiler is and how to use one?
    Here is an abbreviated version of the code. A non-buffered Image is available for dragging here, and it too drags sluggishly unless you eliminate the line of code that draws the BufferedImages to the screen. There is probably something else wrong with this program, because at one point dragging the BufferedImages did work well. When the problem appeared I tore my code apart trying to find out what I had messed up, but after reversing all the recent changes I had not eliminated the problem and was left mystified.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.*;
    import java.awt.image.*;
    import java.awt.geom.*;
    public class Hanoi2 extends JFrame implements MouseListener, MouseMotionListener, ActionListener{
         private GraphicsDevice device;
         Landscape panel=new Landscape();
         JMenuBar bar=new JMenuBar();
         JMenu menu1=new JMenu("Game");
         JMenuItem m11=new JMenuItem("New Game");
         JMenuItem m12=new JMenuItem("Save Game");
         JMenuItem m13=new JMenuItem("Load Game");
         JMenuItem m14=new JMenuItem("Exit");
         JMenu menu2=new JMenu("Options");
         JMenu m21=new JMenu("Disk Number");
         JRadioButtonMenuItem sm11=new JRadioButtonMenuItem("4");
         JRadioButtonMenuItem sm12=new JRadioButtonMenuItem("5");
         JRadioButtonMenuItem sm13=new JRadioButtonMenuItem("6");
         JRadioButtonMenuItem sm14=new JRadioButtonMenuItem("7");
         JRadioButtonMenuItem sm15=new JRadioButtonMenuItem("8");
         JRadioButtonMenuItem sm16=new JRadioButtonMenuItem("9");
         JRadioButtonMenuItem sm17=new JRadioButtonMenuItem("10");
         JMenu m22=new JMenu("Disk Set");
         JRadioButtonMenuItem sm21=new JRadioButtonMenuItem("Stone");
         JRadioButtonMenuItem sm22=new JRadioButtonMenuItem("Glass");
         JRadioButtonMenuItem sm23=new JRadioButtonMenuItem("Pattern");
         JMenu m23=new JMenu("Backdrop");
         JRadioButtonMenuItem sm31=new JRadioButtonMenuItem("Spirals");
         JRadioButtonMenuItem sm32=new JRadioButtonMenuItem("Stars");
         JRadioButtonMenuItem sm33=new JRadioButtonMenuItem("Dots and Flowers");
         JRadioButtonMenuItem sm34=new JRadioButtonMenuItem("Psychedelic");
         JRadioButtonMenuItem sm35=new JRadioButtonMenuItem("Smart Sky");
         JMenu menu3=new JMenu("Help");
         JMenuItem m31=new JMenuItem("Instructions");
         JMenuItem m32=new JMenuItem("About");
         int numberdisks=4;
         int numberdisksthisgame=0;
         int diskset=0;
         int backdrop=0;
         boolean isgame=false;
         int[] diskstack={-1,-1,-1};
         int[][] poledisks=new int[3][10];
         Image[] backs=new Image[5];
         Image[][] disks=new Image[3][10];
         Image[] poles=new Image[3];
         Image cloth;
         Image company;
         Image title;
         MediaTracker tracker=new MediaTracker(this);
         int[][] diskspot=new int[2][10];
         BufferedImage[][] disksr=new BufferedImage[3][10];
         Graphics2D[][] disksg=new Graphics2D[3][10];
         BufferedImage[] polesr=new BufferedImage[3];
         Graphics2D[] polesg=new Graphics2D[3];
         boolean goer=false;
         public Hanoi2(GraphicsDevice device){
         super(device.getDefaultConfiguration());
         this.device=device;
         setTitle("Tower of Hanoi");
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         title=Toolkit.getDefaultToolkit().getImage("Hanoi/Title.gif");
         company=Toolkit.getDefaultToolkit().getImage("Hanoi/Company.gif");
         cloth=Toolkit.getDefaultToolkit().getImage("Hanoi/clothspread2.gif");
         poles[0]=Toolkit.getDefaultToolkit().getImage("Hanoi/woodpole1.gif");
         poles[1]=Toolkit.getDefaultToolkit().getImage("Hanoi/woodpole2.gif");
         poles[2]=Toolkit.getDefaultToolkit().getImage("Hanoi/woodpole3.gif");
         backs[0]=Toolkit.getDefaultToolkit().getImage("Hanoi/background1.gif");
         backs[1]=Toolkit.getDefaultToolkit().getImage("Hanoi/background2.gif");
         backs[2]=Toolkit.getDefaultToolkit().getImage("Hanoi/background3.gif");
         backs[3]=Toolkit.getDefaultToolkit().getImage("Hanoi/background4.gif");
         backs[4]=Toolkit.getDefaultToolkit().getImage("Hanoi/background5.gif");
         disks[0][0]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone1.gif");
         disks[0][1]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone2.gif");
         disks[0][2]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone3.gif");
         disks[0][3]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone4.gif");
         disks[0][4]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone5.gif");
         disks[0][5]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone6.gif");
         disks[0][6]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone7.gif");
         disks[0][7]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone8.gif");
         disks[0][8]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone9.gif");
         disks[0][9]=Toolkit.getDefaultToolkit().getImage("Hanoi/stone10.gif");
         disks[1][0]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass1.gif");
         disks[1][1]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass2.gif");
         disks[1][2]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass3.gif");
         disks[1][3]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass4.gif");
         disks[1][4]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass5.gif");
         disks[1][5]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass6.gif");
         disks[1][6]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass7.gif");
         disks[1][7]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass8.gif");
         disks[1][8]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass9.gif");
         disks[1][9]=Toolkit.getDefaultToolkit().getImage("Hanoi/glass10.gif");
         disks[2][0]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve1.gif");
         disks[2][1]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve2.gif");
         disks[2][2]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve3.gif");
         disks[2][3]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve4.gif");
         disks[2][4]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve5.gif");
         disks[2][5]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve6.gif");
         disks[2][6]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve7.gif");
         disks[2][7]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve8.gif");
         disks[2][8]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve9.gif");
         disks[2][9]=Toolkit.getDefaultToolkit().getImage("Hanoi/carve10.gif");
         for(int i=0; i<10; i++){
         tracker.addImage(disks[0], 0);
         tracker.addImage(disks[1][i], 0);
         tracker.addImage(disks[2][i], 0);}
         tracker.addImage(poles[0], 0);
         tracker.addImage(poles[2], 0);
         tracker.addImage(poles[1], 0);
         try{
         tracker.waitForID(0);
         }catch(Exception e){}
         tracker.addImage(title, 0);
         tracker.addImage(company, 0);
         tracker.addImage(backs[0], 0);
         tracker.addImage(backs[1], 0);
         tracker.addImage(backs[2], 0);
         tracker.addImage(backs[3], 0);
         tracker.addImage(backs[4], 0);
         tracker.addImage(cloth, 0);
         for(int i=0; i<3; i++){
         polesr[i]=new BufferedImage(11,350,BufferedImage.TYPE_INT_RGB);
         polesg[i]=polesr[i].createGraphics();}
         for(int v=0; v<3; v++){
         for(int i=0; i<10; i++){
         disksr[v][i]=new BufferedImage(65+i*15,35,BufferedImage.TYPE_INT_RGB);
         disksg[v][i]=disksr[v][i].createGraphics();
         int y=0;
         int z=-75;
         int q=0;
         for(int x=0; x<65+i*15+2; x+=2){
         if(65+i*15<81) q=7;
         if((65+i*15>80)&&(65+i*15<111)) q=6;
         if((65+i*15>110)&&(65+i*15<141)) q=4;
         if((65+i*15>140)&&(65+i*15<186)) q=3;
         if(65+i*15>185) q=2;
         if((x<(65+i*15)/8)||(x>(65+i*15)-((65+i*15)/8))) {q=q+3;}
         else if((x<(65+i*15)/4)||(x>(65+i*15)-((65+i*15)/4))) {q=q+2;}
         else if((x<(65+i*15)/3)||(x>(65+i*15)-((65+i*15)/3))) {q=q+1;}
         if(x<(65+i*15)/4) z+=q;
         if(x>=(65+i*15)/4) z-=q;
         BufferedImage bi=new BufferedImage(65+i*15,35,BufferedImage.TYPE_INT_RGB);
         BufferedImage bi2=new BufferedImage(65+i*15,35,BufferedImage.TYPE_INT_RGB);
         Graphics2D big;
         big=bi.createGraphics();
         big.drawImage(disks[v][i],0,0,this);
         RescaleOp rop = new RescaleOp(1.1f,(float)(z), null);
    rop.filter(bi,bi2);
         Rectangle2D.Double rect=new Rectangle2D.Double(65+i*15-x,0,4,35);
         disksg[v][i].setClip(rect);
         disksg[v][i].drawImage(bi2,0,0,this);}
         if((v==2)&&(i==9))
         goer=true;}}
         setJMenuBar(bar);
         bar.add(menu1);
         bar.add(menu2);
         bar.add(menu3);
         menu1.add(m11);
         menu1.add(m12);
         menu1.add(m13);
         menu1.add(m14);
         menu2.add(m21);
         menu2.add(m22);
         menu2.add(m23);
         m11.addActionListener(this);
         m12.addActionListener(this);
         m13.addActionListener(this);
         m14.addActionListener(this);
         ButtonGroup group1 = new ButtonGroup();
         m21.add(sm11);
         m21.add(sm12);
         m21.add(sm13);
         m21.add(sm14);
         m21.add(sm15);
         m21.add(sm16);
         m21.add(sm17);
         group1.add(sm11);
         group1.add(sm12);
         group1.add(sm13);
         group1.add(sm14);
         group1.add(sm15);
         group1.add(sm16);
         group1.add(sm17);
         sm11.addActionListener(this);
         sm12.addActionListener(this);
         sm13.addActionListener(this);
         sm14.addActionListener(this);
         sm15.addActionListener(this);
         sm16.addActionListener(this);
         sm17.addActionListener(this);
         ButtonGroup group2 = new ButtonGroup();
         m22.add(sm21);
         m22.add(sm22);
         m22.add(sm23);
         group2.add(sm21);
         group2.add(sm22);
         group2.add(sm23);
         sm21.addActionListener(this);
         sm22.addActionListener(this);
         sm23.addActionListener(this);
         ButtonGroup group3 = new ButtonGroup();
         m23.add(sm31);
         m23.add(sm32);
         m23.add(sm33);
         m23.add(sm34);
         m23.add(sm35);
         group3.add(sm31);
         group3.add(sm32);
         group3.add(sm33);
         group3.add(sm34);
         group3.add(sm35);
         sm31.addActionListener(this);
         sm32.addActionListener(this);
         sm33.addActionListener(this);
         sm34.addActionListener(this);
         sm35.addActionListener(this);
         menu3.add(m31);
         menu3.add(m32);
         m31.addActionListener(this);
         m32.addActionListener(this);}
         int placex=0;
         int placey=0;
         class Landscape extends JPanel{
         public void paintComponent(Graphics g){
         super.paintComponent(g);
         Graphics2D g2 = (Graphics2D) g;
         try{
         tracker.waitForAll();
         }catch (InterruptedException exc){}
         if((tracker.checkAll())&&(goer==true)){
         if(isgame==false){
         g.drawImage(title,68,75,this);
         g.drawImage(company,77,250,this);}
         if(isgame==true){
         g.drawImage(backs[backdrop],0,0,this);
         g.drawImage(cloth,0,462,this);
         g.drawImage(disks[0][0],placex,placey,this);
         for(int i=0; i<10; i++){
         g2.drawImage(disksr[diskset][i],diskspot[0][i],diskspot[1][i],this);}}}}}
         public static void main(String[] args){
         try {
    UIManager.setLookAndFeel(
    UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) { }
         GraphicsEnvironment env = GraphicsEnvironment.
    getLocalGraphicsEnvironment();
    GraphicsDevice[] devices = env.getScreenDevices();
    for (int i = 0; i < 1 /* devices.length */; i++) {
    Hanoi2 box = new Hanoi2(devices[i]);
    box.initComponents(box.getContentPane());
    box.begin();}}
         private void initComponents(Container c) {
         panel.setBackground(new java.awt.Color(201%256, 27%256, 27%256));
         panel.setPreferredSize(new Dimension(696,500));
         setContentPane(panel);}
         public void setVisible(boolean isVis) {
    super.setVisible(isVis);}
         public void begin() {
         addMouseListener(this);
         addMouseMotionListener(this);
         pack();
         setVisible(true);
         setLocationRelativeTo(null);}
         public void actionPerformed(ActionEvent e) {
         if(e.getSource()==m11){
         isgame=true;
         numberdisksthisgame=numberdisks;
         for(int i=0; i<10; i++){
         diskspot[0][i]=25+(9-i)*7;
         diskspot[1][i]=462-numberdisks*35+i*35;}
         repaint();}
    if(e.getSource()==m14){
         System.exit(0);}}
         boolean[] dragdisk={false,false,false,false,false,false,false,false,false,false};
         boolean[] istop={true,false,false,false,false,false,false,false,false,false};
         int xcheck=0;
         int ycheck=0;
         boolean dragother=false;
         int currentpole=0;
         public void mousePressed(MouseEvent e){
         int y=e.getY()-60;
         if(isgame==true){
         if((e.getX()>placex)&&(e.getX()<placex+65)&&(y>placey)&&(y<placey+35)){
         dragother=true;}
         for(int i=0; i<numberdisksthisgame; i++){
         if((e.getX()>diskspot[0][i])&&(e.getX()<diskspot[0][i]+(65+i*15))&&(y>diskspot[1][i])&&
         (y<diskspot[1][i]+35)){
         dragdisk[i]=true;
         xcheck=e.getX()-diskspot[0][i];
         ycheck=y-diskspot[1][i];}}}}
         boolean down=true;
         public void mouseDragged(MouseEvent e){
         int y=e.getY()-60;
         if(dragother==true){
         placex=e.getX();
         placey=y;
         repaint();}
         for(int i=0; i<numberdisksthisgame; i++){
         if((isgame==true)&&(dragdisk[i]==true)){
         diskspot[1][i]=y-ycheck;
         diskspot[0][i]=e.getX()-xcheck;
         repaint();}}}
         int currentpole2=0;
         int[] diskloca={25,250,475};
         public void mouseReleased(MouseEvent e){
         for(int i=0; i<numberdisksthisgame; i++){
         if(dragdisk[i]==true){
         dragdisk[i]=false;}}}
         public void mouseMoved(MouseEvent e){}
         public void mouseEntered(MouseEvent e){}
         public void mouseExited(MouseEvent e){}
         public void mouseClicked(MouseEvent e){}}

  • Painting JComponent on BufferedImage

    Hi!
    My problem concerns painting component derived from JComponent class onto a BufferedImage.To be more precise I've got a JPanel onto which I draw a BufferedImage and then I add my component to the panel. Everything is OK when the component is opaqued but when it is not it doesn't show at all (BufferedImage displays every time). I've been playing with types of BufferedImage (BufferedImage.TYPE_INT_ARGB etc.),double buffering of my component and the panel but with no result. I've also discovered that it's meaningless whether I paint component before drawing Image or after. Here's a piece of code that should help in better understanding what's on my mind:
    public class A extends JPanel {
        public void paint(Graphics g) {
            removeAll();
            g.drawImage(bi,0,0,null); //bi - BufferedImage
            TrojkatnyButton t = new TrojkatnyButton();
            t.setSize(20,20);
            t.setLocation(30,30);
            add(t);
            t.setVisible(true);
            t.repaint();
    public class TrojkatnyButton extends JComponent {
        public boolean isOpaque() {
            return false;
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.ORANGE);
            g.fillOval(0, 0, getWidth(), getHeight());
    }Thanks in advance for answers!

    strannik wrote:
    public class A extends JPanel {
    public void paint(Graphics g) {
    removeAll();
    g.drawImage(bi,0,0,null); //bi - BufferedImage
    TrojkatnyButton t = new TrojkatnyButton();
    t.setSize(20,20);
    t.setLocation(30,30);
    add(t);
    t.setVisible(true);
    t.repaint();
    In this code you are adding a component to another component from within a paint method, and this is something you should never do. Remember that the logic part of the program should not be placed into the paint portion of the program. I'd fix this first and then see if this fixes your problem. If not, I recommend that you post an SSCCE (Short, Self Contained, Correct (Compilable), Example). For more info on SSCCEs please look here:
    [http://homepage1.nifty.com/algafield/sscce.html|http://homepage1.nifty.com/algafield/sscce.html]
    Again, if the code is compilable and runnable more people will be able to help you.
    Best of luck.
    edit: also, since you are coding in Swing, you should not override paint at all 99% of the time. You are far better off overriding paintComponent of your JPanels and other JComponents. Also, don't forget to call super.paintComponent(g) as the first line of your paintComponent override.
    HTH.
    Edited by: Encephalopathic on Jan 3, 2009 6:52 AM

Maybe you are looking for