Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

.paint(g) question

Posted on 2005-03-06
10
Medium Priority
?
236 Views
Last Modified: 2010-03-31
In the following .paint() code, I am writing some text to the screen.  I want for the g.drawString() text to appear on top of the components that are displayed in this container....and the text does appear in the right position, however it is behind the buttons and you can only see bits of it.

How to go about changing the order...or depth of the drawing operations?

Thanks!
0
Comment
Question by:lomidien
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13472079
Use a layered pane.
0
 
LVL 92

Expert Comment

by:objects
ID: 13472087
0
 
LVL 5

Expert Comment

by:ashok3sep
ID: 13472110
public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
   
        // Draw background...
   
        // Set alpha.  0.0f is 100% transparent and 1.0f is 100% opaque.
        float alpha = .3f;
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
   
        // Draw foreground...
    }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lomidien
ID: 13472114
Objects,

Sorry I always seem to leave out one piece of info for you.  :(  This is the problem you've been helping me with the past several days actually....and I've now resorted to trying to design my own component to handle the overlayed menu.  I'm limited to 1.1.8 and can't use GlassPane or LayeredPane...so it's a real challenge.

If you want to see EXACTLY what I'm trying to do, check this direct link....it'll only take a second.  :)  And if you can offer any advice on how to do that in a 1.1.8 compatible fashion, I'll do cartwheels all the way through Picadilly Circus!

http://www.toytoygames.com/mrmuncher/playonline.html

Thanks,
David

0
 
LVL 5

Expert Comment

by:ashok3sep
ID: 13472132
public class MyFrame extends JFrame
{
  class MyContentPane extends JPanel
  {
    public MyContentPane()
    {
      this.setOpaque( false ) ;
    }
  }

  public void paint(Graphics g)
  {
   g.drawImage(backgroundImg.getImage(), 0, 0, this);
   super.paint( g ) ;
  }

  public static void main( String[] args )
  {
    MyFrame f = new MyFrame() ;
    f.setContentPane( new MyContentPane() ) ;
  }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 13472167
you could handle the painting of the overlay in your component.

public void paint(Graphics g)
{
    // paint your component first

    if (overlay)
    {
        // paint your overlay
    }
}
0
 

Author Comment

by:lomidien
ID: 13472227
I forgot to post my code......here it is:

import java.applet.Applet;
import java.awt.*;



public class MenuOverlay extends Applet {
    //class variables
    private boolean paused = true;
   
    public void init() {
        //test components
        for(int i=0; i<40; i++)
            add(new Button("Test"));
       
    }
   
    public void start() {
    }
   
    public void stop() {
    }
   
    public void destroy() {
    }
   
    public void paint(Graphics g) {
        //normal paint method called if game is not paused
        if(!paused) {
            super.paint(g);
        }
        //image is greyed out if game is paused
        else if(paused){
//            super.paint(g); //normal paint operation
            g.setColor(new Color(204,204,204,128));
            g.fillRect(0, 0, getWidth(), getHeight());

            g.setFont(new Font("Serif", Font.BOLD, 30));
            g.setColor(Color.BLACK);
           
            //determine center location
            int height = getHeight();
            int width = getWidth();
            FontMetrics fm = g.getFontMetrics();
            int textWidth = fm.stringWidth("Paused");
            width = width - textWidth;
            int x = width/2;
            int y = height/2;            
           
            g.drawString("Paused", x, y);            
        }
    }
}
0
 
LVL 5

Expert Comment

by:ashok3sep
ID: 13472246
why to give else if(paused)

you can directly give

else{}
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 2000 total points
ID: 13476620
Hi lomidien,

In fact the paint() method should look like this :


        super.paint(g);
        if(paused) {
        //image is greyed out if game is paused
//            super.paint(g); //normal paint operation
            g.setColor(new Color(204,204,204,128));
            g.fillRect(0, 0, getWidth(), getHeight());

            g.setFont(new Font("Serif", Font.BOLD, 30));
            g.setColor(Color.BLACK);
           
            //determine center location
            int height = getHeight();
            int width = getWidth();
            FontMetrics fm = g.getFontMetrics();
            int textWidth = fm.stringWidth("Paused");
            width = width - textWidth;
            int x = width/2;
            int y = height/2;            
           
            g.drawString("Paused", x, y);            
        }


And this will work  if you use lightweight components only   (excluding AWT ones : Button, TextField, ...)
you can use swing components instead like  JButton, JTextField, ...
or create your own by extending the Component class.

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 13484350
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question