Sign in to follow this  
blow

[java] Draw swing component and canvas

Recommended Posts

blow    120
Hi all, i would paint a JLabel on my canvas, but all my solution dont work! I have a JFrame and in this JFrame i add a canvas and a JLabel:
//...
JFrame frame=new JFrame("Test");
frame.setBounds(0,0,800,600);
Container cnt=frame.getContentPane();
JLabel label=new JLabel("Im a LABEL!");
cnt.setLayout(null);
label.setBounds(100,50,100,30);
MyCanvas canvas=new MyCanvas();
cnt.add(canvas);  //add canvas and add label, but i cant see label...!
cnt.add(label);
frame.setVisible(true);
//...
MyCanvas is some like this...
public class MyCanvas extends Canvas implements Runnable{
   
   BufferStartegy bs=null;

   public MyCanvas(){
        setBounds(0,0,800,600);
        setVisible(true);
   }

   public void addNotify(){
      super.addNotify();
      createBufferStrategy(2);
      bs = getBufferStrategy();
      Thread t = new Thread(this);
      t.start();
   }

   public void run(){
       while(true){
          Graphics g = bs.getDrawGraphics();
          g.setColor(Color.white);
          g.fillRect(0,0,800,600);
          g.setColor(Color.black);
          g.fillOval(50,400,100,100);
          bs.show();
          g.dispose();
        }
   }
}
Animation work perfectly but i cant see my JLabel!!! How can i relove this problem?

Share this post


Link to post
Share on other sites
CaptainJester    523
You shouldn't mix AWT and Swing components. It can be done, but it is a lot better not to. You can try this instead.


import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferStrategy;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame implements Runnable {
BufferStrategy bs = null;
MyPanel panel;

public Test() {
panel = new MyPanel();
getContentPane().add(panel);
setIgnoreRepaint(true);
pack();
setVisible(true);
createBufferStrategy(2);
bs = getBufferStrategy();
Thread t = new Thread(this);
t.start();
}
public static void main(String[] args) {
Test t = new Test();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run(){
Insets in = getInsets();
while(true){
Graphics g = bs.getDrawGraphics().create(in.left, in.top, 800, 600);
panel.paint(g);
bs.show();
g.dispose();
}
}
}

class MyPanel extends JPanel {
public MyPanel() {
setPreferredSize(new Dimension(800, 600));
setLayout(new FlowLayout());
add(new JButton("TEST"));
setBackground(Color.WHITE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillOval(400,0,100,100);
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this