Jump to content
  • Advertisement
Sign in to follow this  
blow

[java] Draw swing component and canvas

This topic is 4354 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
If i comment bufferstartegy code i can see my label...but oviusly i cant see animation...

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!