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);
}}
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);
}}