|
|
| draw on JPanel from other class |
|
| Author |
Message |
jenny_jones_79

|
Posted: 8/28/2004 12:59:00 PM |
Top |
java-programmer, draw on JPanel from other class
HI,
I need to draw string to subclass of JPanel from subclass of JFrame.
But I don't know how. Here is a sample. Another problem is the
Thread tt does not run.
Thank you for help.
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class TryFont1 extends JFrame {
RectPanel r = new RectPanel();
public TryFont1() {
super("Rectangles");
setSize(410, 430);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.add(r);
setContentPane(content);
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();
rect.r.comp2D.setColor(Color.blue);
for ( int i = 0; i < 10; i++ ) {
rect.r.comp2D.drawString("Main thread",250,150 + i*10);
//rect.r.repaint();
System.out.println("1 thread");
}
}
}
class RectPanel extends JPanel {
public Graphics2D comp2D;
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
comp2D = (Graphics2D) comp;
setBackground(Color.white);
comp2D.rotate(-.4,200,200);
Font f = new Font("Garbage",Font.BOLD,40);
Font f1 = new Font("Times New Roman",Font.BOLD,40);
comp2D.setColor(Color.green);
comp2D.setFont(f);
comp2D.drawString("Happy", 50,150);
comp2D.setColor(Color.red);
comp2D.setFont(f1);
comp2D.drawString("Birthday!", 50,200);
}}
|
| |
|
| |
 |
| |
|