所以我正在製作一個應用程序,我想跟蹤添加到屏幕上的形狀。到目前爲止,我有以下代碼,但添加一個圓時不能移動/更改。理想情況下,我想要按住shift鍵點擊它來移動/突出顯示它。Java圖形 - 保持跟蹤形狀
我也想知道我怎樣才能讓它可以從一個圓圈拖到另一個圓圈。我不知道我是否在這裏使用了錯誤的工具來完成這項工作,但是我會感激任何幫助。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MappingApp extends JFrame implements MouseListener {
private int x=50; // leftmost pixel in circle has this x-coordinate
private int y=50; // topmost pixel in circle has this y-coordinate
public MappingApp() {
setSize(800,800);
setLocation(100,100);
addMouseListener(this);
setVisible(true);
}
// paint is called automatically when program begins, when window is
// refreshed and when repaint() is invoked
public void paint(Graphics g) {
g.setColor(Color.yellow);
g.fillOval(x,y,100,100);
}
// The next 4 methods must be defined, but you won't use them.
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {
x = e.getX(); // x-coordinate of the mouse click
y = e.getY(); // y-coordinate of the mouse click
repaint(); //calls paint()
}
public static void main(String argv[]) {
DrawCircle c = new DrawCircle();
}
}
graphics.draw(ellipse);給我一個錯誤... – Tim
圖形是僞的。我指的是你使用的任何圖形對象。您使用'g'作爲參考的例子。嘗試使用g.draw() – Nikki
+1來繪製Shapes。但是,Graphics類不知道如何繪製形狀。你需要使用'Graphics2D'類。你也應該使用'fill()'方法。 draw()方法只是繪製Shape的輪廓。有關更多信息,請參見[使用形狀](http://tips4java.wordpress.com/2013/05/13/playing-with-shapes/)。 – camickr