gpt4 book ai didi

java - DrawRect、DrawOval、DrawLine、FillRect 和 FillOval 仅在提供的代码中绘制一条水平直线

转载 作者:行者123 更新时间:2023-12-01 19:01:39 25 4
gpt4 key购买 nike

我目前正在练习 Java 中的 GUI,并制作了一个程序,可以根据用户输入按程序生成随机形状。但是,它似乎无法正常工作。该程序目前看起来像这样(线条而不是矩形和椭圆形): Lines instead of Rectangles and Ovals

但是,正如上面所建议的,程序应该有一些填充的、一些未填充的矩形、椭圆形和线条。这些水平线并不代表输出的样子。

这是下面的代码。

DrawPanelTest.java(主文件)

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;

public class DrawPanelTest {
public static void main(String[] args) {
String amount = JOptionPane.showInputDialog("How many shapes should exist?");
int intAmount = Integer.parseInt(amount);
DrawPanel drawPanel = new DrawPanel(intAmount);
JLabel text = new JLabel("Lines: " + drawPanel.getLineAmount() + ", Ovals: " + drawPanel.getOvalAmount()
+ ", Rectangles: " + drawPanel.getRectangleAmount());
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(3);
jFrame.add(drawPanel);
jFrame.add(text, BorderLayout.SOUTH);
jFrame.setSize(600, 600);
jFrame.setVisible(true);
}
}

DrawPanel.java

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class DrawPanel extends JPanel {
private Random random = new Random();
private Shape[] shapes;
private int lineAmount;
private int ovalAmount;
private int rectangleAmount;

public DrawPanel(int amount) {
shapes = new Shape[amount];
for (int i = 0; i < shapes.length; i++) {
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
int choice = random.nextInt(3);
switch (choice) {
case 0:
shapes[i] = new Line(random.nextInt(300), random.nextInt(300), random.nextInt(300), random.nextInt(300),
color);
lineAmount++;
break;
case 1:
shapes[i] = new Oval(random.nextInt(300), random.nextInt(300), random.nextInt(300), random.nextInt(300),
color, random.nextBoolean());
ovalAmount++;
break;
case 2:
shapes[i] = new Rectangle(random.nextInt(300), random.nextInt(300), random.nextInt(300),
random.nextInt(300), color, random.nextBoolean());
rectangleAmount++;
break;
}
}
}

public int getLineAmount() {
return lineAmount;
}

public int getOvalAmount() {
return ovalAmount;
}

public int getRectangleAmount() {
return rectangleAmount;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Shape shape : shapes) {
shape.draw(g);
}
}
}

形状.java

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

public abstract class Shape {
private int x1;
private int y1;
private int x2;
private int y2;
private Color color;

public Shape() {
setX1(0);
setY1(0);
setX2(100);
setY2(100);
setColor(0, 0, 0);
}

public Shape(int x1, int y1, int x2, int y2, Color color) {
setX1(x1);
setY1(y1);
setX2(x2);
setY2(y1);
setColor(color);
}

public void setX1(int newX1) {
if (newX1 >= 0) {
x1 = newX1;
} else {
x1 = 0;
}
}

public int getX1() {
return x1;
}

public void setY1(int newY1) {
if (newY1 >= 0) {
y1 = newY1;
} else {
y1 = 0;
}
}

public int getY1() {
return y1;
}

public void setX2(int newX2) {
if (newX2 >= 0) {
x2 = newX2;
} else {
x2 = 0;
}
}

public int getX2() {
return x2;
}

public void setY2(int newY2) {
if (newY2 >= 0) {
y2 = newY2;
} else {
y2 = 0;
}
}

public int getY2() {
return y2;
}

public void setColor(int r, int g, int b) {
color = new Color(r, g, b);
}

public void setColor(Color newColor) {
color = newColor;
}

public Color getColor() {
return color;
}

public abstract void draw(Graphics g);
}

BoundedShape.java

import java.awt.Color;
import java.math.*;

public abstract class BoundedShape extends Shape {
private boolean filled;

public BoundedShape() {
super();
setFill(false);
}

public BoundedShape(int x1, int y1, int x2, int y2, Color color, boolean fill) {
super(x1, y1, x2, y2, color);
setFill(fill);
}

public void setFill(boolean fill) {
if (fill == true || fill == false) {
filled = fill;
} else {
throw new IllegalArgumentException("fill has to be either true or false");
}
}

public boolean getFill() {
return filled;
}

public int getUpperLeftX() {
return Math.min(super.getX1(), super.getX2());
}

public int getUpperLeftY() {
return Math.min(super.getY1(), super.getY2());
}

public int getWidth() {
return Math.abs(super.getX2() - super.getX1());
}

public int getHeight() {
return Math.abs(super.getY2() - super.getY1());
}
}

Line.java

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

public class Line extends Shape {

public Line() {
super();
}

public Line(int x1, int y1, int x2, int y2, Color color) {
super(x1, y1, x2, y2, color);
}

@Override
public void draw(Graphics g) {
g.setColor(super.getColor());
g.drawLine(super.getX1(), super.getY1(), super.getX2(), super.getY2());
}
}

矩形.java

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

public class Rectangle extends BoundedShape {
public Rectangle() {
super();
}

public Rectangle(int x1, int y1, int x2, int y2, Color color, boolean filled) {
super(x1, y1, x2, y2, color, filled);
}

public void draw(Graphics g) {
g.setColor(super.getColor());
if (super.getFill() == false) {
g.drawRect(super.getUpperLeftX(), super.getUpperLeftY(), super.getWidth(), super.getHeight());
} else if (super.getFill() == true) {
g.fillRect(super.getUpperLeftX(), super.getUpperLeftY(), super.getWidth(), super.getHeight());
}
}
}

椭圆形.java

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

public class Oval extends BoundedShape {
public Oval() {
super();
}

public Oval(int x1, int y1, int x2, int y2, Color color, boolean filled) {
super(x1, y1, x2, y2, color, filled);
}

public void draw(Graphics g) {
g.setColor(super.getColor());
if (super.getFill() == false) {
g.drawOval(super.getUpperLeftX(), super.getUpperLeftY(), super.getWidth(), super.getHeight());
} else if (super.getFill() == true) {
g.fillOval(super.getUpperLeftX(), super.getUpperLeftY(), super.getWidth(), super.getHeight());
}
}
}

提前致谢,麻卜迪36

最佳答案

OvalRectangledraw() 方法中,您调用 super.getWidth()super.getHeight()。但由于 Shape 中构造函数中的拼写错误,您的高度始终为 0:

您调用setY2()并传入y1,因此y2 == y1,因此abs(y2 - y1) = 0

 public Shape(int x1, int y1, int x2, int y2, Color color) {
setX1(x1);
setY1(y1);
setX2(x2);
setY2(y1); // change this to setY2(y2)
setColor(color);
}

现在高度将不再为 0,您将看到二维形状。

关于java - DrawRect、DrawOval、DrawLine、FillRect 和 FillOval 仅在提供的代码中绘制一条水平直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59628903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com