开发者社区> 问答> 正文

gui画图? 400 报错

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) {}
}
}

展开
收起
爱吃鱼的程序员 2020-05-30 22:03:25 374 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    Swing/AWT方面的应用程序在实际业务中很少用到,不建议花太多精力在GUI编程上。面向Web的应该多花精力去学习、练习。######这是作业~QAQ~第三步的监听器也不知道要怎么写~######jfx######

    javafx 可以嵌入webview,就是能用html和js写界面,调用java方法,写起来就容易多了

    ######好的 感谢~

    2020-05-30 22:03:25
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载