Hi I can't seem to paint to a Frame object using a Graphics object. I have tried a variety of methods and I can't seem to be able to paint anything. I'm still new to the Java API so if you could explain what I'm doing wrong it would be a great help. Here is the code I'm using. I just want the ability to draw a series of strings in different colors onto a Frame object. Here is the code I am trying to get to work: [CODE] public class VoiceMail extends Frame implements WindowListener{ //============================================================================================================== /** * Made to extend frame so that it will be a GUI. */ public VoiceMail() { super("Voice Mail Announcer Ver 0.8"); Timer refresh = new Timer(true); setSize(600,700); addWindowListener(this); Font font = new Font("TimesRoman",Font.BOLD, 12);// Draw in Serif Bold font at 12 point.
g.setFont(font); g.setColor(Color.RED);// Set the color to red. temp = new String("Test"); g.drawBytes(temp.getBytes(),0,0,0,0); g.drawString(temp, 0, 0); ... [\CODE] As output I get a blank grey window. Thanks for your help.
Shital Shisode
Greenhorn
Joined: Nov 15, 2001
Posts: 7
posted
0
Can you post your complete code ? Where is your paint method ? You should write drawString etc. inside paint(). ------------------
Chris Singer
Greenhorn
Joined: Nov 27, 2001
Posts: 15
posted
0
Sorry about that, here is the complete code. I have added the paint method as you suggested but it still doesn't show any text. [CODE] import java.net.*; import java.io.*; import java.lang.*; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.Timer; //============================================================================== public class VoiceMail extends Frame implements WindowListener{ //============================================================================================================== /** * Made to extend frame so that it will be a GUI. */ public VoiceMail() { super("Voice Mail Announcer Ver 0.8"); Timer refresh = new Timer(true); setSize(600,700); addWindowListener(this); setVisible(true); paint("Testing painter"); } //============================================================================================================== /** * */ public void paint(String output) { Font font = new Font("TimesRoman",Font.BOLD, 12);// Draw in Serif Bold font at 12 point. Graphics g = this.getGraphics(); g.setColor(Color.RED); g.setFont(font); g.drawString(output, 0,0); g.drawBytes(output.getBytes(),0, output.getBytes().length,20,20);// Draw some byte to the screan. //g.drawString(output, 0.0f,0.0f); } //============================================================================================================== /** * */ public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { setVisible(false); dispose(); } public void windowClosing(WindowEvent e) { setVisible(false); dispose(); System.exit(0); } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } //============================================================================================================== /** * */ public static void main(String [] args) { VoiceMail app = new VoiceMail(); app.setVisible(true);
// Need a mainloop here to continue monitoring the connection. } } [\CODE]
Originally posted by Shital Shisode: Can you post your complete code ? Where is your paint method ? You should write drawString etc. inside paint().
Shital Shisode
Greenhorn
Joined: Nov 15, 2001
Posts: 7
posted
0
Chris, Here is a small code I wrote as an example to draw a string on a canvas in a frame. Note: paint() is an inbuilt method in java. ================================================================= /** The canvasapp Application */ /** This application writes a string on the canvas in a frame **/ import java.awt.*; import java.awt.event.*; import java.awt.Component.*; public class canvasapp extends Frame { public canvasapp() { setTitle("Canvas Application"); Panel p = new Panel(); RedCanvas redCanvas = new RedCanvas(); add(redCanvas); } public static void main(String args[]) { Frame f = new canvasapp(); f.setSize(300,300); f.show(); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
// This is a custom canvas class which inherits from Canvas class class RedCanvas extends Canvas { // The drawing on the canvas is done in paint method. public void paint(Graphics g) { g.setColor (Color.red); // set the drawing color // Write the string at 75,75 which is the x and y location to draw the string g.drawString ("Red", 75,75); // } }
Hope this helps !! Thanks Shital ------------------
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
The paint() method that the system calls must have the following signature: public void paint(Graphics g) In your code, you have a String parameter, which doesn't override the correct method to actually do the painting. Layne