如何在图像上绘制圆圈?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在图像上绘制圆圈?相关的知识,希望对你有一定的参考价值。
我正在学习如何制作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;
}
}
答案
您可以在绘画中绘制所有内容而不使用面板:
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;
}
}
以上是关于如何在图像上绘制圆圈?的主要内容,如果未能解决你的问题,请参考以下文章