使用repaint()定位特定绘图 [英] Target specific drawing using repaint()

查看:74
本文介绍了使用repaint()定位特定绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绘制方法中绘制了5个椭圆,但我正在进行的斗争是使用KeyListener在不同的时间单独移动它们。我该怎么办?为了说明如何使用重绘方法单独定位它们?现在,如果我按下向上,向下,向右和向左箭头键,它们都会在我不想要的同时移动。



什么我试过了:



I draw 5 Ovals in the paint method but the struggle I am having is moving them individual at different time using KeyListener.How do I do that? In order words how do target them individually with the repaint method? Right now if I press the UP, DOWN, RIGHT, AND LEFT arrow key they all move at the same times which I don't want.

What I have tried:

public void paint(Graphics g) {

g.fillOval(green_x, green_y, 30, 30);
g.fillOval(568, 118, 30, 30); 
g.fillOval(268, 0, 30, 30);
}

public void keyPressed(KeyEvent evt) {
		switch (evt.getKeyCode()) {

		case KeyEvent.VK_LEFT:
		           goLeft();
			   repaint();
			break;

		case KeyEvent.VK_RIGHT:
			     goRight();
			     repaint();

			break;

		case KeyEvent.VK_UP:
			goUp();
                        break;

}
}

推荐答案

不要使用固定值来决定在哪里绘制你的对象。创建一个对象类,用于保存每个形状的详细信息。然后在您的按键代码中,您需要更改您感兴趣的形状的位置值。然后当重绘时,它将重新绘制所有形状,但只有一个会移动。
Don't use fixed values to decide where to draw your objects. Create a class of object that will hold the details of each shape. Then in your keypress code you need to change the location values for only the shape you are interested in. Then when the repaint occurs it will repaint all the shapes but only one will move.


这篇关于使用repaint()定位特定绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆