【发布时间】:2015-05-19 10:36:06
【问题描述】:
我有一个类 Roundabout(JFrame)、Surface(JPanel) 和 Spawn。在 Surface paintComponent 我已经画了一些红绿灯,因为它们总是在环形交叉路口。但我也想在运行时绘制组件。所以我在 JFrame 中添加了一个 mousePressed 事件,我在这里调用的方法最终从 Spawn.java 调用了spawnPedestrian,因为println,我可以看到这一点。但是当spawnPedestrian 被调用时,什么都没有画!如果我在 Surface 类中创建一个行人并绘制它,它就可以工作。我怎样才能使spawnPedestrian 工作?我尝试包含最小代码 sn-p(见下文)。
human.png
roundabout.png
public class Spawn {
public void spawnPedestrian(){
//Create a new pedestrian.
Pedestrian p = new Pedestrian(100,100);
Roundabout.getSurface().add(p);
Roundabout.getSurface().revalidate();
Roundabout.getSurface().repaint();
}
}
public class Roundabout extends JFrame{
static Surface surface=new Surface();
public Roundabout(){
initUI();
}
private void initUI() {
setTitle("Roundabout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(surface);
this.addMouseListener(new MouseAdapter() {// empty implementation of all
// MouseListener`s methods
@Override
public void mousePressed(MouseEvent e) {
System.out.println(e.getX() + "," + e.getY());
Spawn spawn=new Spawn();
spawn.spawnPedestrian();
}
});
setSize(1618,850);
setLocationRelativeTo(null);
}
public static JPanel getSurface() {
return surface;
}
public static void main(String[] args) {
//Swing thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Roundabout roundabout=new Roundabout();
roundabout.setVisible(true);
}
});
}
class Surface extends JPanel {
Track track=new Track();
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Make sure the track is painted first
track.paint(g);
}
}
public class Track{
BufferedImage track;
Point trackPosition;
static final Point TRACK_POS = new Point(0, 0);
static final Point SENSOR_POS = new Point(250, 70);
public Track(){
try {
track = ImageIO.read(Roundabout.class.getResource("images/roundabout.png"));
} catch (Exception ex) {
System.out.println("Problem loading track image: " + ex);
}
trackPosition=new Point(TRACK_POS.x,TRACK_POS.y);
}
public void paint(Graphics g)
{
g.drawImage(track,TRACK_POS.x, TRACK_POS.y, null);
}
}
public class Spawn {
//Needs x/y pos
public void spawnPedestrian(){
Pedestrian p = new Pedestrian(30,50);
System.out.println("Spawn me ");
Roundabout.getSurface().add(p);
Roundabout.getSurface().revalidate();
Roundabout.getSurface().repaint();
}
}
}
public class Pedestrian extends JComponent {
BufferedImage pedestrian;
Point pedestrianPosition;
double pedestrianRotation = 0;
int pedestrianW, pedestrianH;
public Pedestrian(int x, int y){
try {
pedestrian = ImageIO.read(Car.class.getResource("images/human.png"));
} catch (IOException e) {
System.out.println("Problem loading pedestrian images: " + e);
}
pedestrianPosition = new Point(x,y);
pedestrianW = pedestrian.getWidth();
pedestrianH = pedestrian.getHeight();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.rotate(Math.toRadians(pedestrianRotation), pedestrianPosition.x, pedestrianPosition.y);
g2d.drawImage(pedestrian, pedestrianPosition.x, pedestrianPosition.y, null);
}
编辑:以为我通过将Surface 布局设置为null 并使用setBounds 为Pedestrian 解决了这个问题。但是我将图像向右移动得越多,我必须在setBounds()中设置height和width就越高。谁能帮忙?
编辑:修改了我的帖子,现在它是一个可运行的示例,您只需为Track 和Pedestrian 选择自己的图像。
编辑:一个复制粘贴操作来添加代码和图像。
【问题讨论】:
-
Surface使用哪种布局?你能用标准的JButton替换Pedestrian实例吗?Roundabout.getSurface().add(new JButton(""));看看有没有上色? -
@AndrewThompson 我不知道我可以删除什么,这是我认为的最低限度,即使它仍然是很多代码。
-
@Rupak 我不使用
Surface的布局。我试过你的建议来添加一个JButton,它被涂在顶部中心。但是Pedestrian不会被绘制。有什么解决方案的想法吗? -
“我不知道我可以删除任何东西” 这不是(必然)关于“删除”任何东西。我注意到缺少
TrafficLight,因此代码无法编译。因此,它不能被视为运行时问题的示例。此外,虽然 MCVE/SSCCE 可以是多个类,但它必须是单个源文件(其余类从public降级并使用main方法粘贴在源代码之后)。再次阅读 SSCCE 链接。