问题描述
我正在为我的学校项目在 Eclipse 上使用 java 创建一个非常简化的 GPS。我有一辆从 A 点到 B 点的汽车(.jpg 形式),以及使用paintComponent(Graphics g) 绘制街道路线的背景图。我的问题是,街道画与汽车图像重叠,因此汽车的一部分不可见。我怎样才能解决我已经尝试了一切。提前致谢。这是我的项目的一些照片:
这是我的代码(不要介意法语):
package com.solution;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
public class AnimVoiture extends JPanel implements ActionListener {
private static final long serialVersionUID = -7173445602168406358L;
private Timer timer;
private Voiture auto_1;
private final int auto_x = 40;
private final int auto_y = 80;
private final int A_larg = 1080;
private final int A_haut = 720;
private final int delay = 0; //vitesse?
private ArrayList<Voiture> auto_G = new ArrayList<Voiture>();
private int autoRegulTimer = 0;
public AnimVoiture() {
inittest();
}
public void inittest() {
setBackground(Color.white);
setFocusable(true);
setPreferredSize(new Dimension(A_larg,A_haut));
auto_1 = new Voiture(auto_x,auto_y,"//C:/Users/danin/git/Projet_Prog_From_GIt/projet_prog2/Projet_prog2_hiv21/src/com/ressource/car/vv1.png");
auto_G.add(auto_1); // ajouter auto_1 à l'ArrayList
timer = new Timer(delay,this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawObjects(g);
// g.setColor(Color.white);
// g.fillRect(0,this.getWidth(),this.getHeight());
// dessin du cadre
// g.setColor(Color.black);
// g.fillRect(0,this.getHeight());
/* routes horizontales */
for (int i = 100; i <= 700; i = i + 200) {
g.setColor(Color.black);
g.fillRect(100,i,880,50);
for (int j = 110; j <= 950; j = j + 70) {
g.setColor(Color.white);
g.fillRect(j,i + 22,30,5);
}
}
/* routes verticales */
for (int i = 300; i <= 700; i = i + 400) {
g.setColor(Color.black);
g.fillRect(i,50,600);
for (int j = 40; j <= 600; j = j + 70) {
g.setColor(Color.white);
g.fillRect(i + 22,j,5,40);
}
}
// for (int i = 0; i < auto_G.size(); i++) {
// g.fillRect(auto_G.get(i).getX(),auto_G.get(i).getY(),30);
// }
}
private void drawObjects(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < auto_G.size(); i++) {
Voiture v = auto_G.get(i);
g2d.drawImage(v.getimage(),v.getX(),v.getY(),this);
}
}
@Override
public void actionPerformed(ActionEvent e) {
autoRegulTimer++;
auto_1.setX(auto_1.getX() + 1);
if (autoRegulTimer % 125 == 0) {
if (auto_G.size() > 0) {
auto_G.add(auto_1);
}
}
repaint();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)