Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Can't seem to paint on to a Frame.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your complete code ?
Where is your paint method ? You should write drawString etc. inside paint().
------------------
 
Chris Singer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
------------------
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic