How do I paint a TextArea or a Button over an Image

I've started coding a game on Java recently, and I suck when it comes to GUI, so I've been having a lot of trouble.
The thing is:
1 - I have a thread object that calls repaint( ) at a 60 fps rate.
2 - I have a SimpleSprite object that can be created from this:
new SimpleSprite ( "image.png" );
3 - This same object has the following method:
public void paint ( Graphics g , int atX , int atY ) {
     if ( image != null )
          g.drawImage( image , atX , atY , (ImageObserver) this );
}4 - I have a JFrame object as my main program, and it has this code:
public Sprite background = new SimpleSprite( "img_bg.png" );
public void paint ( Graphics g ) {
     background.paint( g , 0, 0);
}Then, I can't seem to find a way to paint a TextArea or a Button OVER this background image on the paint method. And I seriously don't want to recode the TextArea class. It bores me to death just to think of how much would I have to validate at text-selecting when the font characters don't all have the same width.
Is there any way this could be done?
Edited by: KaoroSorane on Aug 3, 2009 7:46 AM
Edited by: KaoroSorane on Aug 3, 2009 7:47 AM

KaoroSorane wrote:
4 - I have a JFrame object as my main program, and it has this code:
public Sprite background = new SimpleSprite( "img_bg.png" );
public void paint ( Graphics g ) {
     background.paint( g , 0, 0);
With this code you've just broken JFrame painting. I think it is better to create class-descendant of JComponent and do whatever you want in it's paintComponent() method.
Also, calling repaint() simply schedules call to paint() method. If you want 60 fps, may be " [Active rendering|http://java.sun.com/docs/books/tutorial/extra/fullscreen/rendering.html] " mechanism will help.

Similar Messages

  • How can I eliminate the mesh screen which appears over any image I open in photoshop CS6?

    How can I eliminate the mesh screen which appears over any image I open in photoshop CS6?

    It depends, but it could either be the Grid or Pixel Grid under View>Show.

  • How do I overlap text in a Div Tag over an Image

    Hi,
    I have a large image which I would like to set as as border around the page.
    Please see www.touchw1.com, it is the grey border with the 3 archways on it that I would like as a border.  At present, everything on this screen apart from the contact details at the bottom are saved as one JPEG.  I saved it like this as I don't know how to set that border as a border and then insert other div tags over, or in it?  Can anyone help??
    Thanks

    Create a 'wrapper" and 'footer1' <div>
    <div id="wrapper">
    </div><!-- end wrapper -->
    <div id="footer1">
    </div><!-- end footer1 -->
    Create some css:
    #wrapper {
    border: 1px solid #333;
    border-bottom: none;
    width: XXXpx /* width you require */
    margin: 0 auto; /* centers wrapper horizontally in browser window */
    #footer1 {
    background-image: url(archway.gif); /*archway image plus borders */
    background-repeat: no-repeat;
    height: XXpx /* height of background archway image */
    width: XXXpx /* width you require */
    margin: 0 auto; /* centers footer1 horizontally in browser window */
    Then start adding content to the 'wrapper' <div>. Insert your logo into a <h1> header.
    <div id="wrapper">
    <h1><img src="images/logo.jpg" width="XXX" height="XXX" alt="TouchW1 Sales and Press Agency - Central London" /></h1>
    <p>TouchW1 is a newly created niche agency that specialises in all aspects of sales management, press and licensing, based in the heart of Central London</p>
    </div><!-- end wrapper -->
    <div id="footer1"></div><!-- end footer1 -->
    Move h1 header into position in the 'wrapper" <div> with css:
    h1 {
    margin: XXpx XXpx XXpx XXpx; /* top, right, bottom, left */
    padding: XXpx XXpx XXpx XXpx;
    Do the same with the paragraph tag
    #wrapper p {
    margin: XXpx XXpx XXpx XXpx; /* top, right, bottom, left */
    padding: XXpx XXpx XXpx XXpx;
    Add rose as background to 'wrapper' <div>
    #wrapper {
    border: 1px solid #333;
    border-bottom: none;
    width: XXXpx /* width you require */
    margin: 0 auto; /* centers wrapper horizontally in browser window */
    background-image: url(rose.jpg);
    background-position: top right;
    background-repeat: no-repeat;
    Hummm that its really....add a 'footer2' <div> for the address line

  • How can superimpose buttons over an image?

    Hello to all. I have a little problem with my program in the front panel. I inserted a tab control and I wanted to insert all I had inside it. The problem is that I had an image below a set of buttons and at the time I migrate all to the tab control, the image superimpose to the buttons, how can I avoid it? Thank you
    Carlos Ignacio Sarmiento
    Electronic Engineer
    (Ciudad Madero Technological Institute)
    Laboratory of Bioelectronics, Biohospital Engineering Departament
    National Institute of Neurology and Neurosurgery "Manuel Velasco Suárez" (Mexico City)
    Attachments:
    problema.png ‏240 KB

    Hi
    select your image, and press Ctl+Shift+J. or, you can do as shown in the image attaced. move your picture behind the controls.
    Attachments:
    move back.PNG ‏170 KB

  • How do i set a background of the button as an image??

    hey guys... i was wondering if there is a way i could change the background of spark button component to an image??
    ive looked into the icon property of the spark button, but thats not what i need... i need the entire background of an image to be an image...
    any ideas??
    thanks in advance!!!

    Something like this: http://blog.flexexamples.com/2010/04/29/setting-a-bitmap-fill-on-a-spark-button-control-in -flex-4/
    Peter

  • How to transfer Painter 12 with my custom brushes from old iMac to a new iMac both running Mavericks

    Hi,
    I just purchased a new iMac. Have Painter 12 on my older iMac and want to be able to transfer Painter 12, with my custom brushes to my new iMac. Tried Airdrop but got a message that it was damaged and that I should trash it. Does any know how to get  Painter 12 with all my custom brushes to my new machine? Both are running Mavericks.
    Thank you,
    Jim

    etresoft,
    Thank you for your response. I did try to use Migration Assistant and I got as far as the two machines trying to communicate with each other. So I guess the answer to your question would be I tried, I failed and it worked until a certain point. I understand that there are other ways to connect the machines, I would have to see if I have the proper cables to do so. Airdrop did get data to my new machine and that tells me that they do see each other on the network. I do not know why they did not see each other via Migration Assistant. I assume that I could go to Corel's site and download Painter 12 to my new machine, ( my new machine does not have a disk drive ), but I will not have my custom brushes etc. and would have to totally make them up in the fresh install version and I am trying to avoid that.
    You are probably correct in saying that Painter 12 probably needs a full reinstallation to work properly. Am I to assume that Migration Assistant will do a full reinstallation, including my custom brushes? I guess I am a little confused here.
    Jim

  • Confused about how to use paint()

    Hi, I have been working really hard to try to get the following program to work but I am really confused on how to use paint(). I do not have anyone to ask so I thought this forum could help. Anyways, here is my problem...
    I am trying to recursively figure out the Sierpinski fractal. I cannot figure out if my math works or not because I do not know how to call the paint() recursively to draw my triangles.
    Please have a look at the following code. Thank you!
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    public class DrawTriangle extends Frame {
      Point a = new Point(20,480),
            b = new Point(350,40),
            c = new Point(680,480),
            halfB,
            halfC,
            halfB2,
            halfC2,
            halfC3;
      int width;
      public DrawTriangle() {
        super("Sierpinski Fractal");
        addWindowListener( new WindowAdapter() {
          public void windowClosing( WindowEvent we ) {
            dispose();
            System.exit( 0 );
        setBackground( Color.white );
        setSize(700, 500);
        setVisible(true);
      public void paint(Graphics g, Point a, Point b, Point c) {
        width = c.x - a.x;
        if (width < 6){return;}
        g.setColor( Color.GREEN );
        g.drawPolygon( new int[] { a.x, b.x, c.x }, new int[] { a.y, b.y, c.y }, 3 );
        halfC.x = c.x/2;
        halfC.y = c.y;
        halfB.y = b.y/2;
        halfB.x = b.x;
        halfB2.y = halfB.y + a.y;
        halfB2.x = a.x;
        halfC2.x = halfC.x + a.x;
        halfC2.y = a.y;
        halfC3.x = halfC.x/2 + a.x;
        halfC3.y = halfB2.y;
        paint(g, a, halfC, halfB);
        paint(g, halfC3, halfC, halfB);
        paint(g, halfC2, halfC, halfB);
      public static void main(String[] args) {
         new DrawTriangle();

    thanks jsalonen, your tip let me start working on the math to correct it.
    I have a new problem now. My math is correct but I am having problems with the recursion. I can draw only the top , left , or right triangles. I cannot get them all to work together. See code and comments below.
    Any ideas why I cant call all three of the paint()s toegther and have them work?
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    public class DrawTriangle extends Frame {
      Point a = new Point(20,480),
            b = new Point(350,40),
            c = new Point(680,480),
            halfA,
            halfB,
            halfC;
      int width;
      public DrawTriangle() {
        super("Sierpinski Fractal");
        addWindowListener( new WindowAdapter() {
          public void windowClosing( WindowEvent we ) {
            dispose();
            System.exit( 0 );
        setBackground( Color.white );
        setSize(700, 500);
        setVisible(true);
      public void paint(Graphics g)
      paint(g, a, b, c);
      public void paint(Graphics g, Point a, Point b, Point c) {
        width = c.x - a.x;
        if (width < 6){return;}
        halfA = new Point((a.x+b.x)/2, (a.y+b.y)/2);
        halfB = new Point((a.x+c.x)/2, (a.y+c.y)/2);
        halfC = new Point((b.x+c.x)/2, (b.y+c.y)/2);
        g.setColor( Color.GREEN ); //draw left triangle in green
        g.drawPolygon( new int[] { a.x, halfA.x, halfB.x }, new int[] { a.y, halfA.y, halfB.y }, 3 );
        g.setColor( Color.RED ); //draw top triangle in red
        g.drawPolygon( new int[] { b.x, halfA.x, halfC.x }, new int[] { b.y, halfA.y, halfC.y }, 3 );
        g.setColor( Color.BLUE ); //draw right triangle in blue
        g.drawPolygon( new int[] { c.x, halfB.x, halfC.x }, new int[] { c.y, halfB.y, halfC.y }, 3 );
        /*If you were to comment our two of these paint() calls the one will work correctly alone.
         *But my problem is that they do not work together! */
        //g, left point, top point, right point
        paint(g, halfA, b, halfC); //top triangle
        paint(g, halfC, halfB, c); //right triangle
        paint(g, a, halfA, halfB); //left triangle
      public static void main(String[] args) {
         new DrawTriangle();
    }

  • How to call paint ( )?

    Hi,
    I am trying to display a tring but I dont know how to call paint ( ). I used show but its giving me mesg that show is deprecated.
    Can somebody help me in this regard?
    import java.awt.*;
    class DisplayText extends Frame{
    DisplayText (String s) {
      super(s);
    public void paint (Graphics g) {
      g.drawString("Hello World", 10,10);
    public static void main (String args[ ]) {
       DisplayText screen = new DisplayText("Example 1");
                screen.setSize(500,100);
                screen.setVisible(true);
    }Zulfi.

    I don't really recommend using the paint() methodfor
    drawing Strings on the Frame. Paint should be used
    just for Graphics
    So how do you make a component show a text? Use a Label :-)If your custom component needs to paint graphics and text then there is nothing wrong with drawing a String in the paintComponent() method. (In Swing you should override paintComponent(), not paint(). It doesn't make sense to create a JLabel with all the extra overhead that requires.
    The problem with this posting is overriding the paint(..) method of the entire frame is not a good idea.

  • How to have a box layout on a JDialog, with an image set as background

    Hi,
    I need to have a JDialog in which there is a background image set (I already did this part, but only with the default layout). I further need to add text to the lower part of the JDialog. (For this, I guess I need to have a box layout.). I am not able to do so, because if I do so, I wont be able to set image background to the entire JDialog. Please help me out with how to solve this issue?
    Thanks,
    Joby

    Hi jduprez,
    Thanks for the reply. I checked Rob Camick's blog. It gives a nice way to add an image to a panel (*master panel*) and to use it.
    I still have my problem open. The above solution gives panel that I can add to my JDialog. But on the bottom half of the image (as you said, BorderLayout.South), I need to add another structured set of components using a Border Layout again.!, ie, one more panel with a BorderLayout. So when I add
    this panel, to the master panel containing the image, then the image gets cut off, at that point. I tried using component.setOpaque(false) on my sub-panel, still it does not work. Any idea, how to achieve this...?
    Following is the code I have adapted.
    public class BackgroundPanel extends JPanel
         public static final int SCALED = 0;
         public static final int TILED = 1;
         public static final int ACTUAL = 2;
         private Paint painter;
         private Image image;
         private int style = SCALED;
         private float alignmentX = 0.5f;
         private float alignmentY = 0.5f;
         private boolean isTransparentAdd = true;
         public static void main(String[] args) {
              Image img = getImage("D:/imgs/Snowdrop.jpg");
              BackgroundPanel panel = new BackgroundPanel(img);
              JDialog dlg = new JDialog();
              dlg.setLayout(new BorderLayout());
              dlg.add(panel);
              panel.setTransparentAdd(true);
              Panel nPanel = new Panel();
              nPanel.setLayout(new BorderLayout());
              JLabel label = new JLabel();
              //label.set
              label.setText("<html>HI<br>This is another line<br><br><br><br><br><br><br><br><br><br></html>");
              nPanel.add(label, BorderLayout.NORTH);
              panel.add(nPanel/*label*/,BorderLayout.SOUTH);
              dlg.setSize(600, 500);
              dlg.setVisible(true);
         private static Image getImage(String fileName){
              File file = new File(fileName);
             Image image = null;
             try{
                  image = ImageIO.read(file);     
             catch(IOException ioe){
                  /*JOptionPane.showMessageDialog(dlg, "Error in loading image file",
                            "Error", JOptionPane.ERROR_MESSAGE, null);*/
             return image;
          *  Set image as the background with the SCALED style
         public BackgroundPanel(Image image)
              this(image, SCALED);
          *  Set image as the background with the specified style
         public BackgroundPanel(Image image, int style)
              this(image,style,-1,-1);
          *  Set image as the backround with the specified style and alignment
         public BackgroundPanel(Image image, int style, float alignmentX, float alignmentY)
              setImage( image );
              setStyle( style );
              if (alignmentX  > 0){
                   setImageAlignmentX( alignmentX );     
              if (alignmentY  > 0){
                   setImageAlignmentY( alignmentY );     
              setLayout( new BorderLayout() );
          *  Use the Paint interface to paint a background
         public BackgroundPanel(Paint painter)
              setPaint( painter );
              setLayout( new BorderLayout() );
          *     Set the image used as the background
         public void setImage(Image image)
              this.image = image;
              repaint();
          *     Set the style used to paint the background image
         public void setStyle(int style)
              this.style = style;
              repaint();
          *     Set the Paint object used to paint the background
         public void setPaint(Paint painter)
              this.painter = painter;
              repaint();
          *  Specify the horizontal alignment of the image when using ACTUAL style
         public void setImageAlignmentX(float alignmentX)
              this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
              repaint();
          *  Specify the horizontal alignment of the image when using ACTUAL style
         public void setImageAlignmentY(float alignmentY)
              this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;
              repaint();
          *  Override method so we can make the component transparent
         public void add(JComponent component)
              add(component, null);
          *  Override method so we can make the component transparent
         public void add(JComponent component, Object constraints)
              if (isTransparentAdd)
                   makeComponentTransparent(component);
              super.add(component, constraints);
          *  Controls whether components added to this panel should automatically
          *  be made transparent. That is, setOpaque(false) will be invoked.
          *  The default is set to true.
         public void setTransparentAdd(boolean isTransparentAdd)
              this.isTransparentAdd = isTransparentAdd;
          *     Try to make the component transparent.
          *  For components that use renderers, like JTable, you will also need to
          *  change the renderer to be transparent. An easy way to do this it to
          *  set the background of the table to a Color using an alpha value of 0.
         private void makeComponentTransparent(JComponent component)
              component.setOpaque( false );
              if (component instanceof JScrollPane)
                   JScrollPane scrollPane = (JScrollPane)component;
                   JViewport viewport = scrollPane.getViewport();
                   viewport.setOpaque( false );
                   Component c = viewport.getView();
                   if (c instanceof JComponent)
                        ((JComponent)c).setOpaque( false );
          *  Add custom painting
         protected void paintComponent(Graphics g)
              super.paintComponent(g);
              //  Invoke the painter for the background
              if (painter != null)
                   Dimension d = getSize();
                   Graphics2D g2 = (Graphics2D) g;
                   g2.setPaint(painter);
                   g2.fill( new Rectangle(0, 0, d.width, d.height) );
              //  Draw the image
              if (image == null ) return;
              switch (style)
                   case SCALED :
                        drawScaled(g);
                        break;
                   case TILED  :
                        drawTiled(g);
                        break;
                   case ACTUAL :
                        drawActual(g);
                        break;
                   default:
                     drawScaled(g);
          *  Custom painting code for drawing a SCALED image as the background
         private void drawScaled(Graphics g)
              Dimension d = getSize();
              g.drawImage(image, 0, 0, d.width, d.height, null);
          *  Custom painting code for drawing TILED images as the background
         private void drawTiled(Graphics g)
                 Dimension d = getSize();
                 int width = image.getWidth( null );
                 int height = image.getHeight( null );
                 for (int x = 0; x < d.width; x += width)
                      for (int y = 0; y < d.height; y += height)
                           g.drawImage( image, x, y, null, null );
          *  Custom painting code for drawing the ACTUAL image as the background.
          *  The image is positioned in the panel based on the horizontal and
          *  vertical alignments specified.
         private void drawActual(Graphics g)
              Dimension d = getSize();
              Insets insets = getInsets();
              int width = d.width - insets.left - insets.right;
              int height = d.height - insets.top - insets.left;
              float x = (width - image.getWidth(null)) * alignmentX;
              float y = (height - image.getHeight(null)) * alignmentY;
              g.drawImage(image, (int)x + insets.left, (int)y + insets.top, this);
    }Thanks,
    Joby

  • How to restart iPhone 5 with broken power button and frozen home button?

    How to restart iPhone 5 with broken power button and frozen home button?

    hmm..just realised that waiting here for answer is like watching paint drying.. Thanks Apple!
    So best option is..
    1) backup your phone
    2) log to iCloud
    3) erese your phone
    4) pug and restore your phone from backup
    .. I'm clearly starting to dislike this company and seriously considering switching to someone else.

  • How can I use the print module to print different size images on one large "canvas"?

    How can I use the print module to print different size images on one large "canvas"? An example would be in Photoshop, go to file>new, and create the size paper I want, and move images of different sizes onto it.
    I was thinking the print module would do this automatically for me.

    You can't, at present. It's been a requested feature, so we'll see if it shows up in a future version, but it's not there at present.

  • In popup step how do I get the "text on the button" in the report rather than button index?

    (1)
    How do I configure the POPUP step in "SEQ MAIN.seq" to execute the second
    step (IS OUT PUT IS 20?) in "SEQ 1.seq" when "IS OUT PUT IS 20?" button
    hit and execute the third step (IS OUT PUT IS 30?) in "SEQ 1.seq" when "IS
    OUT PUT IS 30?" button hit.
    (2)
    In popup step how do I get the "text on the button" in the report rather than button index?
    File attached
    Attachments:
    test_stand.zip ‏32 KB

    The handle to the Step.Button1Label gets you the data, but there are several ways to get it into the actual report.
    The easiest is to use the reporttext.
    In a post expression, you can use something like
    Step.Result.ReportText = Evaluate("Step.Button" + Str(Step.Result.ButtonHit) +"Label")
    and then the default report generation will include it in the report. Otherwise, you need to get the text into the Resultlist by various means (check the user manual, or the TestStand II customisation course) and handle the report generation yourself inside of the appropriate sequence in the process model.
    Just my 2cents
    S.
    // it takes almost no time to rate an answer
    Attachments:
    IncludeButtonTextInReport2_0.seq ‏18 KB

  • How to create a shape based on the shape of an image

    I want to erase a portion of a mask based on the shape of an image. how can i do this? btw how to create a circle? thx

    Both your questions leave room for imagining what you might really mean, but here's a shot at answering them.
    If you have a mask that you want to erase portions of based on an image... assuming the mask is a simple rectangle you drew with the rectangle tool, change the opacity of the rectangle's fill in the color panel to make it see-thru, place the mask over the image, and using the eraser tool, erase the portions of the mask that you do not intend to retain using the image below it as a guide.
    As for drawing a circle, hover over the rectangle tool and a menu should appear.  Select the oval/ellipse tool.  To draw a circle, hold down the shift key and draw just as you did for the rectangle.  The shift key forces it to retain equal width and height.

  • How do I convert my Windows 7 Folder to an ISO image to burn to a USB, in order to use Bootcamp Assistant to partition my MacBook Pro for Mac and Windows?

    How do I convert my Windows 7 Folder to an ISO image to burn to my  USB Flash drive (16GB)  in order to use Bootcamp Assistant to partition my MacBook Pro for Mac and Windows?

    Sorry - confusing - My question has NOT been solved yet - can anyone help?

  • How do I change a color to another in a bitmapData image?

    Let's say you have an image of a cartoon human that is wearing a blue jacket with white sleeves, blue shoes, and grey pants. Brown hair and biege skin tone.
    (My project actually has 1300 of these images - 325 are blue cloths, 325 are red cloths, 325 are green cloths, and 325 are purple cloths)
    I am having a major problem with file size and memory usage due to all the images, so I have been asked to see if there was a way I could programmatically change the clothing color... this way we can use any color we want, and only have 325 base images.
    I have been trying to play with color matrix filter and color transform, but I can't seem to get the hang of it
    Is there a way I can extract only the "blue" colors from the image and do a hue shift on it to the color that I want it to be without effecting the non-blue colors?
    My idea was something along the lines of:
    Copy the "blue" color channel, hue shift it, then merge it back into frame....

    Oh boy... this seems to be an issue with your manager wanting it done 'NOW' rather than 'CORRECTLY'.
    For my original statement to work, the clothing would have to be seperated from the people - as per your suggestion to them - which would make your lives a whole lot easier.  The problem with hue shifting the entire image is --> it will hue shift the entier image = all colors, and that may not be the best solution in your situation.
    I would suggest replacing the pixel color (eg. getPixel32 and setPixel32 for the size of the image) but it seems that the shoulders and shoes have a darker color tone than the rest of the body.
    Secondary solution:
    Import one of the image into Flash and do a bitmap trace of the image. 
    Seperate the clothes and people there. 
    Add the clothes as a new movie clip and change the color on a few frames 5 or so different colors (so its not too much work)
    Show them an example of how 'easy' it is to change the colors CORRECTLY when the images are seperate (even in the same file)
    The secondary will give them the 'all in one file' they requested... and also give you solid grounds for convincing them to change the graphics.
    Hope this helps.  Other wise... its a long not so nice solution which will go over budget because they did not plan for this before they made the graphics.

Maybe you are looking for