java - 如何使用 getX() 和 getY() 画一条线?

标签 java

这是一项更大的家庭作业。 如何使用 getX() 和 getY() 绘制一条线? 这是我的代码,请帮忙。

package shapes;

import java.awt.Color;
import java.awt.Graphics;

public class Line extends Rectangle {
    /**
    * Constructor.  Just passes the params to the Rectangle constructor.
    */
    public Line(int x, int y, int w, int h, Color lineColor, Color fillColor, boolean fill) {
        super(x, y, w, h, lineColor, fillColor, fill);
    }

    /*
    * Override Rectangle draw(Graphics g) method.
    */
    public void draw(Graphics g) {
        // Be nice. Save the state of the object before changing it.
        Color oldColor = g.getColor();

        g.setColor(getLineColor());
        g.drawLine(getX(), getY(), getWidth(), getHeight());
        // Set the state back when done.
        g.setColor(oldColor);
    }

    /**
    * Returns a String that represents this object.
    */
    public String toString() {
        //return "Line: \n\tx = " + getX() + "\n\ty = " + getY() + "\n\tw = " + getWidth() + "\n\th = " + getHeight();
        return "Line";
    }
}

最佳答案

您误解了 drawline(...) 方法中的参数。

drawLine(int x1, int y1, int x2, int y2) 

从点(x1, y1) 到点(x2, y2) 绘制一条线。不存在从一个点画一条具有高度和宽度的线这样的事情。

关于java - 如何使用 getX() 和 getY() 画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5307531/

相关文章:

Java Socket编程客户端服务器通信问题

java - 数组越界异常保龄球示例

java - 查找异常类型

java - 如何播放通过虚拟音频输入设备输出的音频? (电缆输入)

java - 包含的 ListView 不会填充

java - Dropwizard Json pretty-print 查询参数

java - Eclipse 创建您自己的 system.out.println 快捷方式

java - Java 中使用递归解决迷宫

java - 如何通过聊天应用程序的套接字将字符串安全地编码为字节

java - spring data JPA 中 api Page<E> 的空数据