gui画图? 400 报错
文件Circles.java创建JFrame。文件Circle.java响应mouse click事件,绘制随机大小和颜色的圆,每个圆取代它的前一个。文件CirclePanel.java传递mouse click事件。保存编译运行并根据要求修改程序:
1.当前的这个程序每次都创建一个新的圆,写一个方法void move(Point p),使得将当前的圆移动到move方法的参数p为圆心的位置上。修改你的监听器CirclesListener,使得第一次mouse click时创建一个圆,此后每次鼠标点击调用move方法修改圆的位置。
2.为Circle类写一个方法boolean isInside(Point p),判断p是否在圆中(距离公式sqrt((x2-x1)2+(y2-y1)2)
3.修改CirclesListener 类的mousePressed方法:
a.如果屏幕上没有圆,用户点击任何地方,绘制一个新的(随机)圆
b.如果屏幕上已经有圆且用户点击圆内的任何区域,圆要消失
c.如果屏幕上已经有圆且用户点击圆外的任何区域,圆要移动到所点击的点
//********************************************************************
// Circles.java
//
// Demonstrates mouse events and drawing on a panel.
// Derived from Dots.java in Lewis and Loftus
//********************************************************************
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);
}
}
// ****************************************************************
// Circle.java
//
// Define a Circle class with methods to create and draw
// a circle of random size, color, and location.
//
// ****************************************************************
import java.awt.*;
import java.util.Random;
public class Circle
{
private int centerX, centerY;
private int radius,n;
private Color color,m;
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 point)
{
radius = Math.abs(generator.nextInt())%50 + 25;
color = new Color(Math.abs(generator.nextInt())% 16777216);
centerX = point.x;
centerY = point.y;
m = color;
n = radius;
}
//---------------------------------------------------------
// Draws circle on the graphics object given
//---------------------------------------------------------
/*public void draw(Graphics page)
{
page.setColor(color);
page.fillOval(centerX-radius,centerY-radius,radius*2,radius*2);
}
*/
public void move(Point p)
{
p.setColor(m);
p.fillOval(centerX-n,centerY-n,n*2,n*2); 这里不会写
}
}
//*************************************************************************
// CirclePanel.java
//
// Represents the primary panel for the Circles program on which the
// circles are drawn. Derived from the Lewis and Loftus DotsPanel class.
//*************************************************************************
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;
//-----------------------------------------------------------------
// 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);
}*/
public void moveComponent(Point p)
{
center = p;
if (circle != null) 这个不知道要怎么改
circle.move(p);
}
//*****************************************************************
// 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)
{
circle = new Circle(event.getPoint());
repaint();
}
//-----------------------------------------------------------------
// Provide empty definitions for unused event methods.
//-----------------------------------------------------------------
public void mouseClicked (MouseEvent event) {
/* circle = new Circle(event.getPoint());
repaint();*/
}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
}
}
Swing/AWT方面的应用程序在实际业务中很少用到,不建议花太多精力在GUI编程上。面向Web的应该多花精力去学习、练习。######这是作业~QAQ~第三步的监听器也不知道要怎么写~######jfx######
javafx 可以嵌入webview,就是能用html和js写界面,调用java方法,写起来就容易多了
######好的 感谢~版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。