?
Solved

.paint(g) question

Posted on 2005-03-06
10
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn 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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 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