i am new to swings and i don't properly know where and how to use drawString in swings !! please help!!

import java.awt.Graphics;
import java.awt.*;
import java.awt.Graphics2D;

public class Main extends javax.swing.JFrame implements Runnable {
    String m1="hello";
    Thread t =null;
    boolean flag;
    /** Creates new form NewJFrame */
    public Main() {
        initComponents();

    }
    public void start()
{
t=new Thread(this);
flag=false;
t.start();
}
public void run()
{
char ch1;
for(; ;)
{try
{
repaint();
Thread.sleep(500);
ch1=m1.charAt(0);
m1=m1.substring(1,m1.length());
m1+=ch1;
if(flag)
break;
}
catch(InterruptedException e)
{
}
}
}
public void stop()
{
flag=true;
t=null;
}


    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 290, Short.MAX_VALUE)

        ); 


        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 400, 290);

        pack();




}




    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });

    }


    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    // End of variables declaration


}

i want that string m1 should run as a banner across the screen .!!

Hi. First off, please correctly use the code tags:
http://www.daniweb.com/forums/announcement9-3.html

Second, the syntax for drawString is: (Most likely used in the paint method)
<GraphicsObject>.drawString("Text/String", xLoc, yLoc);

Third, using pack() instead of setSize(int, int) is going to make your Window as small as possible, only constraining to the minimum sizes of the components in your window, which you don't have.

may like this

public void paint(java.awt.Graphics g)
{
// within this function paint any thing u want
// u can get Graphics2D Object and feel free with drawing
g.drawString(/*x */50,/*y*/50,"ur Text");
}

i hope this help u,,

Mustafa

yes i did that but when the word hello goes across the screen , it keeps on becoming dark and dark and ultimately i cant see anything!!!
why is it?

yes i did that but when the word hello goes across the screen , it keeps on becoming dark and dark and ultimately i cant see anything!!!
why is it?

I do not know what is the code which used 
to cause that's problem of<darken and darken>
-----
so i will put the following  simple code :) 
if your problem Absolutely not solved,plz put your
code which cause that's problem
  
class A extends javax.swing.JFrame
{
	A()
	{
		super();
		setVisible(true);
		setSize(300,200);
	}
	public void paint(java.awt.Graphics g)
	{
		super.paint(g);
		g.drawString("DaniWeb",100,100);
	}
	public static void main(String args[])
	{
		new A();
	}
}

Mustafa

You need to use code tags, as mentioned.

And you need to format/indent. Your code is impossible to read due to inconsistent indentation, even in code tags.

import java.awt.Graphics;
import java.awt.*;
import java.awt.Graphics2D;

public class Main extends javax.swing.JFrame implements Runnable {
    String m1="hello";
    Thread t =null;
    boolean flag;
    /** Creates new form NewJFrame */
    public Main() {
        initComponents();

    }
    public void start()
{
t=new Thread(this);
flag=false;
t.start();
}
public void run()
{
char ch1;
for(; ;)
{try
{
repaint();
Thread.sleep(500);
ch1=m1.charAt(0);
m1=m1.substring(1,m1.length());
m1+=ch1;
if(flag)
break;
}
catch(InterruptedException e)
{
}
}
}
public void stop()
{
flag=true;
t=null;
}


    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 290, Short.MAX_VALUE)

        ); 


        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 400, 290);

        pack();




}




    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });

    }


    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    // End of variables declaration


}

I don't see a paintComponent function anywhere and I don't see a drawString command anywhere. It looks like you are using a GUI builder like NetBeans for this. Don't. It's not needed and it just complicates things. There are no components. Just make a JFrame, with an x variable and a y variable and a paintComponent function. Within that paintComponent, have a call to drawString using x and y. Inside your run () function, have x and y change and call repaint.

Start from scratch. All of this substring stuff in your run () function has nothing to do with having a String go across the screen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.