有關GUI繪圖 |
尚未結案
|
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
想請問一下
我想要寫一個程式滑鼠點一下圓會出現圓的中心點就是滑鼠點的地方
如果華鼠再點到其它地方則相同顏色和大小的圓會再出現如果點在圓裡面員圓就會消失
我寫的程示碼如下 import javax.swing.JFrame; public class Circles
{
//-----------------------------------------------------------------
// Creates and displays the application frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame circlesFrame = new JFrame ("Circles");
circlesFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); circlesFrame.getContentPane().add (new CirclePanel()); circlesFrame.pack();
circlesFrame.setVisible(true);
}
} import java.awt.*;
import java.util.Random; public class Circle
{
private int centerX, centerY;
private int radius;
private Color color;
private Circle circle, circle1;
private Graphics page;
static Random generator = new Random(); //---------------------------------------------------------
// Creates a circle with center at point given, random radius and color
// -- radius 25..74
// -- color RGB value 0..16777215 (24-bit)
//---------------------------------------------------------
public Circle (Point p)
{
radius =Math.abs(generator.nextInt())P 25;
color = new Color(Math.abs(generator.nextInt())% 16777216);
centerX = p.x;
centerY = p.y;
}
public void move (Point p)
{radius =circle.radius;
color =circle.color;
centerX = p.x;
centerY = p.y;
page.fillOval(centerX-radius,centerY-radius,radius*2,radius*2);
}
//---------------------------------------------------------
// Draws circle on the graphics object given
//---------------------------------------------------------
public void draw(Graphics page)
{
page.setColor(color.green);
page.fillOval(centerX-radius,centerY-radius,radius*2,radius*2);
}
} import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*; public class CirclePanel extends JPanel
{
private final int WIDTH = 600, HEIGHT = 400;
private Circle circle;
private Point p; //-----------------------------------------------------------------
// Sets up this panel to listen for mouse events.
//-----------------------------------------------------------------
public CirclePanel()
{
addMouseListener (new CirclesListener());
setPreferredSize (new Dimension(WIDTH, HEIGHT));
} //-----------------------------------------------------------------
// Draws the current circle, if any.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);
if (circle != null)
circle.draw(page);
} //*****************************************************************
// Represents the listener for mouse events.
//*****************************************************************
private class CirclesListener implements MouseListener
{
//--------------------------------------------------------------
// Creates a new circle at the current location whenever the
// mouse button is pressed and repaints.
//--------------------------------------------------------------
public void mousePressed (MouseEvent event)
{ if (circle==null)
{ circle = new Circle(event.getPoint());}
else
{circle.move(p);}
repaint();
} //-----------------------------------------------------------------
// Provide empty definitions for unused event methods.
//-----------------------------------------------------------------
public void mouseClicked (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
}
} 我的問題出現在我的void move(Point p)也就是說目前我可以creat random的size和顏色的圓但如何在我的void move(Point p)方法裡確定滑鼠再點時出現的是之前相同的員而不是再creat 一個新圓
還有如何寫滑鼠點在圓裡面圓會消失呢
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
|
chenglovebun
一般會員 發表:15 回覆:16 積分:6 註冊:2004-10-02 發送簡訊給我 |
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |