Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mixing Graphics and Components

Status
Not open for further replies.

brownie124

Programmer
Sep 19, 2002
61
US
Hi,

I am very new to Java and am trying to do something that seems simple. I am drawing a rectangle and then attempting to put some components (Checkboxes, Lists, etc.) in the rectangle (NOTE: I cannot use the swing package so this is all AWT and Graphics).

I am having all kinds of problems with this. I am able to put one or the other up, but not both the rectangle and the components. Also, I am trying to setLocation() of my component and that isn't working at all. Here is the code:
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Test1
{
	public static void main(String [] args) 
	{
		try 
		{
			AppWindow appwin = new AppWindow();
			appwin.setSize(new Dimension(appwin.WIDTH, appwin.HEIGHT));
			appwin.setBackground(Color.lightGray);
			appwin.setTitle("Test");
			appwin.center();
			appwin.show();
		}
		catch(Exception e) 
		{
			e.printStackTrace();
		}
	}
} // End Test1 Class

class AppWindow extends Frame
{
	public static final int WIDTH = 400;
	public static final int HEIGHT = 300;
	public static final String BOXCAPTION = "Show:";
	
	public AppWindow()
	{
		addWindowListener	(
							new WindowAdapter()
							{
								public void windowClosing(WindowEvent we)
								{
                   					dispose();
									System.exit(0);
	               				}
           					}
							);

	}

	public void paint(Graphics g)
	{
		//	Create our group box.
		
		GroupBox gb = new GroupBox(15, 35, WIDTH - 15, 105, g, BOXCAPTION);
		createCheckBoxes();
	}
	
	public void createCheckBoxes()
	{
		//	Add our check boxes to the group box.

		Checkbox cb1 = new Checkbox("Check box 1", true);
		Checkbox cb2 = new Checkbox("Check box 2", false);
		
		cb1.setLocation(80, 100);
		cb2.setLocation(80, 160);
		
		add(cb1);
		add(cb2);
		
		cb1.setVisible(true);
		cb2.setVisible(true);
	}
	
	public void center()
	{
		Dimension dim = getToolkit().getScreenSize();
		Rectangle abounds = getBounds();
		setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2);
	}
} // End AppWindow Class

class GroupBox
{
	public GroupBox(int x, int y, int intWidth, int intHeight, Graphics g)
	{
		this(x, y, intWidth, intHeight, g, "");
	}
		
	public GroupBox(int x, int y, int intWidth, int intHeight, Graphics g, String strCaption)
	{
		FontMetrics fm = g.getFontMetrics();
		int intSpace = 0;
	
		g.drawLine(x, y, x, intHeight);						// 	Left side of box.
		g.drawLine(x, y, 23, y);							//	Top left part of box -- before caption.
		if (strCaption.length() > 0)
		{
			g.drawString(strCaption, x + 10, y + 5);		//	Caption.
			intSpace = 2;
		}
		g.drawLine(23 + fm.stringWidth(strCaption) + intSpace, y, intWidth, y);	//	Top right part of the box -- after caption.
		g.drawLine(intWidth, y, intWidth, intHeight);		// 	Right side of box.
		g.drawLine(x, intHeight, intWidth, intHeight);		//	Draw bottom of box.
	}
} // End GroupBox Class

Any help or guidance would be very much appreciated.

Thanks,
- Michael
 
Michael,
Since nobody is answering this thread I gave it a try. Just before I wanted to give up and wish you good luck, it finally ran correct.
(It's your code with a few changes :
- setLayout(null); // to be able to specify x,y position
- setBounds(x,y,width,heigth); // if you don't use a LayoutManager you have to setBounds(...) for all components).
The book "Graphic Java : Mastering the awt" contains some usefull classes (source) (e.g. Border). With the book is included a jar file with the examples from the book. Size=1,57 MB. If you post your e-mail I can mail it to you.
Here is the code :
Code:
package com.ecc.swing2;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class AwtExample
{
    public static void main(String [] args)
    {
        try
        {
            AppWindow appwin = new AppWindow();
            appwin.setLayout(null); //Ward
            appwin.setSize(new Dimension(appwin.WIDTH, appwin.HEIGHT));
            appwin.setBackground(Color.lightGray);
            appwin.setTitle("Test");
            appwin.center();
            appwin.createCheckBoxes();  // Ward
            appwin.show();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
} // End Test1 Class

class AppWindow extends Frame
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;
    public static final String BOXCAPTION = "Show:";

    public AppWindow()
    {
        addWindowListener    (
                            new WindowAdapter()
                            {
                                public void windowClosing(WindowEvent we)
                                {
                                       dispose();
                                    System.exit(0);
                                   }
                               }
                            );

    }

    public void paint(Graphics g)
    {
       GroupBox gb = new GroupBox(15, 35, WIDTH - 15, 105, g, BOXCAPTION);
    }

    public void createCheckBoxes()
    {
        //    Add our check boxes to the group box.
        Checkbox cb1 = new Checkbox("Check box 1", true);
        Checkbox cb2 = new Checkbox("Check box 2", false);
        cb1.setBounds(20,60,90,20);   // Ward
        cb2.setBounds(120,60,90,20);  // Ward
        add(cb1);
        add(cb2);
   }

    public void center()
    {
        Dimension dim = getToolkit().getScreenSize();
        Rectangle abounds = getBounds();
        setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2);
    }
} // End AppWindow Class

class GroupBox
{
    public GroupBox(int x, int y, int intWidth, int intHeight, Graphics g)
    {
        this(x, y, intWidth, intHeight, g, "");
    }

    public GroupBox(int x, int y, int intWidth, int intHeight, Graphics g, String strCaption)
    {
        FontMetrics fm = g.getFontMetrics();
        int intSpace = 0;

        g.drawLine(x, y, x, intHeight);                        //     Left side of box.
        g.drawLine(x, y, 23, y);                            //    Top left part of box -- before caption.
        if (strCaption.length() > 0)
        {
            g.drawString(strCaption, x + 10, y + 5);        //    Caption.
            intSpace = 2;
        }
        g.drawLine(23 + fm.stringWidth(strCaption) + intSpace, y, intWidth, y);    //    Top right part of the box -- after caption.
        g.drawLine(intWidth, y, intWidth, intHeight);        //     Right side of box.
        g.drawLine(x, intHeight, intWidth, intHeight);        //    Draw bottom of box.
    }
} // End GroupBox Class
 
If you are using jdk1.2 or later, casting the Graphics g object to a Graphics2D object should give you some extra functionality in the paint method.
Code:
    public void paint(Graphics g)
    {
      super.paint(g);
      Graphics2D g2d = (Graphics2D)g;
      g2d.drawString(...);
      ...
    }

 
Thanks for the great feedback.

Hologram: Thanks for taking the time to get this to work. Also, I would love to get that book. My email is
michael@michaelbrown.online.com

- Michael
 
Michael,
When I try to mail to you, I get an error on your email address :
Code:
The message could not be sent because one of the recipients was rejected by the server. The rejected e-mail address was 'michael@michaelbrown.online.com'. Subject 'Graphic Java, Mastering the AWT examples', Account: 'Telenet Mail', Server: 'smtp.telenet.be', Protocol: SMTP, Server Response: '450 <michael@michaelbrown.online.com>: Recipient address rejected: Domain not found', Port: 25, Secure(SSL): No, Server Error: 450, Error Number: 0x800CCC79
Only the examples are on the CD, NOT the book itself (1997)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top