Draw a string on JPanel

J

Jenny

Hi,

I want to draw a string on JPanel based on user input. In each time
of drawing, I remove the JPanel and adda new one for drawing the new
string. Is there a beeter way of doing this? Another problem is I
used a line:

term.setText("new test");//Force repaint.

in the code to force the repaint. I do not want the label (term).
But without it, the program does not draw at all. How can I modify
the code to without using this line?

Thanks a lot.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class TryFont1 extends JFrame implements ActionListener{
RectPanel r;
Container pane;
JLabel term=new JLabel("Text:");
JButton button1 = new JButton("Draw");
boolean bDraw = false;
public TryFont1() {
super("Rectangles");
setSize(410, 430);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1.addActionListener(this);
pane = getContentPane();
FlowLayout flo = new FlowLayout(FlowLayout.LEFT,10,20);
pane.setLayout(flo);
pane.add(button1);
pane.add(term);
setVisible(true);

}
public void run() {
for ( int i = 0; i < 10; i++ ) {
System.out.println("New thread");
}
}
public static void main(String[] arguments) {
TryFont1 rect = new TryFont1();
Thread tt = new Thread();
tt.start();
tt.run();
}
public void actionPerformed(ActionEvent e){
term.setText("new test");//Force repaint.
if (bDraw){//Test if p exists.
pane.remove(r); //Tf p exists, remove it. We need to add a new one.
r=null;
}
r = new RectPanel("Happy");
r.setPreferredSize(new Dimension(300,300));
bDraw=true;//p exists. set bDraw to true.
pane.add(r);
}
}

class RectPanel extends JPanel {
String s;
RectPanel(String s1){
s=s1;
}
public Graphics2D comp2D;
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
comp2D = (Graphics2D) comp;
setBackground(Color.white);
comp2D.rotate(-.4,200,200);
Font f1 = new Font("Times New Roman",Font.BOLD,40);
comp2D.setColor(Color.green);
comp2D.setFont(f1);
comp2D.drawString(s,50,150);
comp2D.setColor(Color.red);
comp2D.drawString("Birthday!",50,200);
}}
 
B

Babu Kalakrishnan

Jenny said:
I want to draw a string on JPanel based on user input. In each time
of drawing, I remove the JPanel and adda new one for drawing the new
string. Is there a beeter way of doing this? Another problem is I
used a line:

term.setText("new test");//Force repaint.

in the code to force the repaint. I do not want the label (term).
But without it, the program does not draw at all. How can I modify
the code to without using this line?

Couldn't make out what some portions of your code meant. (esp. those
related to the Thread and the run() method) - however here are some
suggestions :

a) Yuo really don't need to recreate the panel everytime you want to
change what is painted on it. A better way is to provide s setter
method for the text (inside your RectPanel class) that will set the
text to the new value and forct itself to repaint something like :

public void setText(String s)
{
this.s = s;
repaint();
}

If the RectPanel is already added to the contentPane before it is
shown, the above code will make sure that it gets updated everytime
you call setText on the panel.

The reason why you need the line term.setText(..) is a little more
involved. In your code, you are changing the contents of the JFrame's
contentPane (when you remove the old panel and add a new one). Once
you do this to a container, the container also needs to be revalidated.
(validation is essentially the process by which the container recomputes
the boundaries of each of its child components - normally performed through
a LayoutManager). When you call setText on the Label with a different text
string, Swing realizes that the label might change its size and hence asks
the LayoutManager (the FlowLayout) to recompute the positions / sizes of all
its children and force them to repaint - that is the reason why your panel
suddesnly shows up with the new text when you add this call.

Normally the procedure to follow when adding / removing child components from a
container is to call invalidate() followed by validate() and repaint() on it
after the modification. If your container happens to a Swing component - i.e
anything descended from JComponent, it is better to use the revalidate() method
instead of the invalidate()/validate() sequence since it offers additional
performance optimizations.

Hope this helps.

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top