如何在图像上绘制圆圈?

问题描述 投票:1回答:1

我正在学习如何制作java applet,我正在尝试拍摄一个十字路口的图像并在其上运行一个圆圈。我可以导入图像并将其显示在小程序中,但圆圈不会显示在它上面。我只是从左上方开始向屏幕左侧方向移动。有任何想法吗?

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;

public class App extends Applet implements Runnable{
    int x,y;
    boolean running = true;
    Image background;
    /**
     * 
     */
    private static final long serialVersionUID = -8667920279388305018L;

    public void init() {
        x = 0;
        y = 0;
        background = getImage(getCodeBase(), "street.png");
        BackGroundPanel bgp = new BackGroundPanel();
        bgp.setLayout(new FlowLayout());
        bgp.setBackGroundImage(background);
        setSize(500,500);
        // set the layout of the applet to Border Layout
        setLayout(new BorderLayout());

        // now adding the panel, adds to the center
        // (by default in Border Layout) of the applet
        add(bgp);

    }

    @Override
    public void start() {
        new Thread(this).start();
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillOval(x, y, 100, 100);
    }

    @Override
    public void run() {
        while(running) {
            ++x;
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            repaint();
        }

    }

````}

class BackGroundPanel extends Panel implements ImageObserver {
     /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Image backGround;

     BackGroundPanel() {
          super();
     }

     public void paint(Graphics g) {

          // get the size of this panel (which is the size of the applet),
          // and draw the image
          g.drawImage(getBackGroundImage(), 0, 0,
              (int)getBounds().getWidth(), (int)getBounds().getHeight(), this);
     }

     public void setBackGroundImage(Image backGround) {
          this.backGround = backGround;    
     }

     private Image getBackGroundImage() {
          return backGround;    
     }
}
java animation applet
1个回答
0
投票

您可以在绘画中绘制所有内容而不使用面板:

public class App extends Applet implements Runnable{
int x,y;
boolean running = true;
Image background;
/**
 * 
 */
private static final long serialVersionUID = -8667920279388305018L;

public void init() {
    x = 0;
    y = 0;
    background = getImage(getCodeBase(), "street.png");
    setSize(500,500);
    // set the layout of the applet to Border Layout
    setLayout(new BorderLayout());


}

@Override
public void start() {
    new Thread(this).start();
}

@Override
public void paint(Graphics g) {
    g.drawImage(background, 0, 0,
          (int)getBounds().getWidth(), (int)getBounds().getHeight(), this);
    g.setColor(Color.BLACK);
    g.fillOval(x, y, 100, 100);
}

@Override
public void run() {
    while(running) {
        ++x;
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        repaint();
    }

}


Or if you want the panel you add it and draw everything there - you have to move some stuff to the panel though such as the x, y and the image - and you repaint the panel:


public class App extends Applet implements Runnable{
    boolean running = true;
    BackGroundPanel bgp = new BackGroundPanel();
    /**
     * 
     */
    private static final long serialVersionUID = -8667920279388305018L;

    public void init() {
        bgp.setLayout(new FlowLayout());
        bgp.setImage(getImage(getCodeBase(), "street.png"));
        setSize(500,500);
        // set the layout of the applet to Border Layout
        setLayout(new BorderLayout());

        // now adding the panel, adds to the center
        // (by default in Border Layout) of the applet
        add(bgp);

    }

    @Override
    public void start() {
        new Thread(this).start();
    }

    @Override
    public void paint(Graphics g) {
    }

    @Override
    public void run() {
        while(running) {
            ++bgp.x;
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            bgp.repaint();
        }

    }

````}


class BackGroundPanel extends Panel implements ImageObserver {
    int x,y;
     /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Image background;

     BackGroundPanel() {
        super();
        x = 0;
        y = 0;
     }

    public void setImage (Image i) {
        background=i;
    }

     public void paint(Graphics g) {

          // get the size of this panel (which is the size of the applet),
          // and draw the image
          g.drawImage(background, 0, 0,
              (int)getBounds().getWidth(), (int)getBounds().getHeight(), this);
        g.setColor(Color.BLACK);
        g.fillOval(x, y, 100, 100);
     }

     public void setBackGroundImage(Image backGround) {
          this.backGround = backGround;    
     }

     private Image getBackGroundImage() {
          return backGround;    
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.