从线程绘制图像

时间:2013-11-22 13:15:27

标签: java swing jpanel paintcomponent runnable

我有类扩展JPanel的应用程序。此面板正在绘制图像。这是一个代码:

public class BackgroundImage extends JPanel{

private BufferedImage img;
private File imageFile;

public BackgroundImage(){
    super();

    Samolot s1 = new Samolot(200,200,new Dimension(15,15));
    s1.start();

    imageFile = new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_mapa.jpg");

    try {
        mapa = ImageIO.read(imageFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(img, 0, 0, getWidth(), getHeight(), null);        
}
}

我想要做的是课程ball,它将扩展Threads。 每个球都是下一个线程。球需要通过简单的增量坐标int x从左到右移动,该坐标定义了所创建的球的X位置。 为了表明球在移动,我需要在当前坐标中绘制图像。更改位置后,将球重新绘制到下一个位置。更重要的是我需要将MouseListener添加到我的球中,以便在点击它之后不会让问题变得更加困难只需要System.out.println("somemessage");

我的问题: 我需要在哪里画球?在班级Ball?还是BackgroundImage?应该怎么样? 在哪里以及如何添加MouseListener以使他的工作属性。 如何在每个线程循环中重新绘制我的面板。

这是我的Ball类的代码:

  class Ball extends Thread {

  // The image to display
  private BufferedImage img;
  private int x;
  private int y;
  Dimension dim;

public void run() {
    super.run();

    try {
        while(x<1000){
            Thread.sleep(300);
            x++;
            System.out.println(x);              
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
  // Instantiate the panel and perform initialization
  Samolot(int x, int y, Dimension d) {
      this.x = x;
      this.y = y;
      this.dim = d;

    try {
        img = ImageIO.read(new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_samolot.png"));
    } catch (IOException e) { }

  }
}

我读到我不应该在其他线程中绘制任何东西,然后是我的主线程(这里是BackgroundImage)。

我正在考虑这样的事情,但它不起作用:

class Ball

  class Ball extends JComponent implements Runnable {
MouseListener listener;

public void addListener(MouseListener toAdd) {
    listener = toAdd;
}

  // The image to display
  private BufferedImage img;
  private int x;
  private int y;
  Dimension dim;

public void run() {     
    try {
        while(x<1000){
            Thread.sleep(300);
            x++;
            listener.mouseClicked(new MouseEvent(this, 1, 1, 0, x, y, 1, true)); 
            System.out.println(x);              
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
  // Instantiate the panel and perform initialization
  Samolot(int x, int y, Dimension d) {
      this.x = x;
      this.y = y;
      this.dim = d;

    try {
        img = ImageIO.read(new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_samolot.png"));
    } catch (IOException e) { }

  }
    public BufferedImage getImg(){
        return this.img;
    }
}

班级BackgroundImage

public class BackgroundImage extends JPanel implements MouseListener{

private BufferedImage mapa;
private File imageFile;
private Ball s1; 
public MapaSwiata(){
    super();

    s1 = new Ball(200,200,new Dimension(15,15));
    Thread t = new Thread(s1);
    t.start();

    imageFile = new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_mapa.jpg");

    try {
        mapa = ImageIO.read(imageFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(mapa, 0, 0, getWidth(), getHeight(), null);      
    g2d.drawImage(s1.getImg(), 200, 200, 15, 15, null);
}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.println("message");
}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}
}

0 个答案:

没有答案