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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello, MediaTracker problem

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello, I am trying to "Fill the top half of the window with 3 horizontal bars each covering the full width of the window(equal height), then Fill the bottom half of the window with the image indicated in class. A MediaTracker will be used to load the image.

My Problem is.. I can't load the image when it is displayed.
Could anyone check my code if something is wrong? Thank you very much, I am very appreciated it.

W.

---------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class DemoPanel extends JPanel {

Image img;
Bar top;
Bar middle;
Bar bottom;

public DemoPanel(Image image)
{
img = image;
top = new Bar("Hello There, Gee Whiez", Color.green, Color.red);
middle = new Bar("Hello There, Gee Whiez", Color.blue, Color.yellow);
bottom = new Bar("Hello There, Gee Whiez", Color.cyan, Color.blue);
}

public void paintComponent(Graphics g)
{
Dimension dim = getSize();
double width = dim.getWidth();
double height = dim.getHeight();


int barheight = (int)(height/6.0);
top.paint(0, 0, (int)width, (int)height/6, g);
middle.paint((int)width, (int)height/6, (int)width, (int)height/3, g);

//g.drawImage(img, 0, (int)height/2,
// (int)width, (int)height/2, null);

g.setColor(Color.red);
g.fillRect(0, 0, (int)width, barheight);

Font font = new Font("San Serif", Font.PLAIN, barheight/2);
g.setFont(font);
FontMetrics fm = g.getFontMetrics();
String str = "Hello There, gee whiz";
int strWidth = fm.stringWidth(str);
int x = ((int)width - strWidth) / 2;
g.setColor(Color.green);
g.drawString(str, x, (int)(0.5 * barheight));

//////////////////////////////////////////////////
g.setColor(Color.blue);
g.fillRect(0, barheight, (int)width, barheight);

Font font1 = new Font("San Serif", Font.PLAIN, barheight/2);
g.setFont(font1);
FontMetrics fm1 = g.getFontMetrics();
String str1 = "Hello There, gee whiz";
int strWidth1 = fm1.stringWidth(str);
int x1 = ((int)width - strWidth1) / 2 ;
g.setColor(Color.yellow);
g.drawString(str1, x1, (int)( barheight *1.5 ));

/////////////////////////////////////////////////
g.setColor(Color.yellow);
g.fillRect(0, barheight *2, (int)width, barheight);

Font font2 = new Font("San Serif", Font.PLAIN, barheight/2);
g.setFont(font2);
FontMetrics fm2 = g.getFontMetrics();
String str2 = "Hello There, gee whiz";
int strWidth2 = fm2.stringWidth(str);
int x2 = ((int)width - strWidth2) / 2 ;
g.setColor(Color.blue);
g.drawString(str2, x2, (int)( barheight *2.5));

g.drawImage(img, 0, (int)height/2,(int)width, (int)height/2, null);
//////////////////////////////////////////////////
/*Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("pf2.gif");
MediaTracker tracker = new MediaTracker(new Panel());


*/




}
}


class Bar{
String text;
Color fg;
Color bg;
public Bar(String text, Color fg, Color bg)
{
this.text = text;
this.fg = fg;
this.bg = bg;
}

public void paint(int x, int y, int width, int height, Graphics g)
{
g.setColor(bg);
g.fillRect(x, y, width, height);

FontMetrics fm = g.getFontMetrics();
int textwidth = fm.stringWidth(text);
g.setColor(fg);
g.drawString(text, (width - textwidth)/4, y + 3 * height/4);
}

}

public class Demo6 extends JFrame
{
class WindowHandler implements WindowListener
{
public void windowActivated(WindowEvent e)
{
System.out.println("Window Activated");
}
public void windowClosed(WindowEvent e)
{
System.out.println("Window Closed");
}
public void windowClosing(WindowEvent e)
{
System.out.println("Window Closing");
}
public void windowDeactivated(WindowEvent e)
{
System.out.println("Window Deactivated");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("Window De-icon");
}
public void windowIconified(WindowEvent e)
{
System.out.println("Window icon");
}
public void windowOpened(WindowEvent e)
{
System.out.println("Window open");
}

}

public Demo6()
{ /*
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get size
Dimension dimension = toolkit.getScreenSize();
setSize(dimension.width/2, dimension.height/2);
setLocation(dimension.width/2,0);
*/


WindowHandler windowHandler = new WindowHandler();
addWindowListener(windowHandler);


Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int width = (int)dim.getWidth();
int height = (int)dim.getHeight();

setLocation(width/4, height/4);
setSize(width/2, height/2);

Image img = tk.getImage("C:\\pf2.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 0);
try
{
mt.waitForID(0);
}
catch(Exception e)
{
}

DemoPanel pnl = new DemoPanel(img);
getRootPane().setContentPane(pnl);

setTitle("Graphics Demonstration");

setResizable(true);
setVisible(true);
}

public static void main(String[] args)
{
Demo6 app = new Demo6();
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top