你好,我正在尝试学习java中的图形,同时学习类和对象。我现在的目标是制作一个包含具有矩形或圆形的不同类的程序,然后我想在其他类中使用这些矩形和圆形并更改它们的参数(如大小、颜色和位置)以绘制某种图案。
我现在的问题是我可以制作一个矩形,我想我什至可以制作第二个矩形,但我无法更改它的参数(颜色、大小和位置)我尝试向这部分代码添加变量Rect rect = new Rect(int Variables);
但它不起作用。
通常我可以解决这样的简单问题,但我真的不明白类和对象在java中是如何工作的,如果有人能给我一些帮助那就太好了。
这是我的代码
public class Main{
public static void main(String[] args ) {
Pattern.drawPattern();
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rect extends JPanel{
public static Color myColor = Color.RED;
public static int myX = 10;
public static int myY = 10;
public static int myWidth = 200;
public static int myHeight = 200;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(myColor);
g.fillRect(myX, myY, myWidth, myHeight);
}
}
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
public class Pattern {
public static void drawPattern() {
JFrame window = new JFrame("test");
window.setSize(1000, 800);
window.setVisible(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rect rect = new Rect();
Rect rect1 = new Rect();
window.add(rect);
window.add(rect1);
Rect.myColor = Color.lightGray;
}
}
最佳答案
这里有很多问题,但我看到的主要问题是:
- 在 Rect 类中过度使用 static 修饰符。通过使用静态字段,矩形实例将不会有自己独特的状态、颜色和位置。将所有这些字段设为私有(private)非静态(实例)。如果这导致编译问题,请通过不将字段设为静态而是不尝试从类访问它们来修复它。
- 如果您想在类之外更改这些字段,还可以提供这些字段设置方法。如果你想查询它们,还有 getter 方法
- 您忽略了 JFrame 的 contentPane 默认使用 BorderLayout 的事实。此布局将通过接下来添加的任何内容覆盖先前添加的组件。如果您需要在此容器中包含多个组件,请使用不同的布局
- 但你的主要问题是 Rect 不应该扩展 JPanel,它不应该是一个组件类,而应该是一个逻辑类。
- 而是创建一个扩展 JPanel 的类并执行所有绘制操作,然后为其提供多个 Rect 实例以在其单个 PaintComponent 方法中进行绘制。您可以使用
ArrayList<Rect>
为此。 - 将此单个绘图 JPanel 添加到 JFrame。如果您这样做,则无需更改 JFrame 的布局管理器,因为 BorderLayout 可以很好地工作,允许绘图 JPanel 填充 JFreme 的中心。
小问题:
- 避免为您的类提供与常见 Java 核心类冲突的名称,例如 Java 正则表达式分析中经常使用的 Pattern
- 不知道为什么你甚至需要 Pattern 类,因为它没有做任何在 main 方法中无法完成的有用的事情。
例如,矩形(或此处命名为 Rect2 以表明它与您的类不同)可能类似于:
// imports here
public class Rect2 {
private Color myColor = Color.RED;
private int x = 10;
private int y = x;
private int width = 200;
private int height = width;
public Rect2() {
// default constructor
}
public Rect2(Color myColor, int x, int y, int width, int height) {
this.myColor = myColor;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// method used to allow the rectangle to be drawn
public void draw(Graphics g) {
g.setColor(myColor);
g.fillRect(x, y, width, height);
}
public void setMyColor(Color myColor) {
this.myColor = myColor;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
// more setters and some getters if need be
}
和绘图 JPanel 类似:
// imports here
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private List<Rect2> rectList = new ArrayList<>();
// ..... more code
public void addRect2(Rect2 rect) {
rectList.add(rect);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// iterate through the rectList and draw all the Rectangles
for (Rect2 rect : rectList) {
rect.draw(g);
}
}
// ...... more code
}
它可以像这样放入 JFrame 中......
Rect2 rectA = new Rect2();
Rect2 rectB = new Rect2();
rectB.setMyColor(Color.BLUE);
rectB.setX(300);
rectB.setY(300);
// assuming that the class's constructor allows sizing parameters
DrawingPanel drawingPanel = new DrawingPanel(1000, 800);
drawingPanel.addRect2(rectA);
drawingPanel.addRect2(rectB);
JFrame frame = new JFrame("Main2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(drawingPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
关于java - 我正在尝试使用图形并绘制一些矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51582491/