【问题标题】:I'm trying to work with Graphics and draw some Rectangles我正在尝试使用图形并绘制一些矩形
【发布时间】:2018-07-29 16:48:52
【问题描述】:

您好,我正在尝试学习 Java 中的图形以及类和对象。我现在的目标是制作一个包含不同类的程序,其中包含 Rectangles 或 Circles,然后我想在其他类中使用这些 Rectangles 和 Circles,并更改它们的参数,如大小、颜色和位置来绘制某种图案。

我现在的问题是我可以制作一个矩形,我想我什至可以制作第二个,但我无法更改它的参数(颜色、大小和位置)我尝试在这部分代码中添加变量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;

    }

}

【问题讨论】:

  • 嗯,在 IntelliJ 中将所有内容放在一个类中效果很好。但是,如果您手动触发重绘,也许会有所帮助。因为如果你改变你绘制的内容的属性,这就是你需要做的。
  • 请查看更新以回答。有什么问题请追问

标签: java swing class object graphics


【解决方案1】:

这里有很多问题,但我能看到的主要是:

  • 在 Rect 类中过度使用静态修饰符。通过使用静态字段,Rect 实例将不会有自己独特的状态、颜色和位置。将所有这些字段设为私有非静态(实例)。如果这会导致编译问题,请不要将字段设置为静态,而是不要尝试从类中访问它们来修复它。
  • 如果您想在类之外更改这些字段设置方法,也可以提供这些方法。如果你想查询它们,还有 getter 方法
  • 您忽略了 JFrame 的 contentPane 默认使用 BorderLayout 的事实。此布局将覆盖先前添加的组件,然后添加任何内容。如果您需要此容器中的多个组件,请使用不同的布局
  • 但您的主要问题是 Rect 不应该扩展 JPanel,它不应该是一个 component 类,而应该是一个 logical 类。
  • 而是创建一个扩展 JPanel 并完成所有绘图的类,然后为其提供多个 Rect 实例以在其单个 paintComponent 方法中进行绘图。您可以为此使用 ArrayList<Rect>
  • 将此单个绘图 JPanel 添加到 JFrame。如果您这样做,则无需更改 JFrame 的布局管理器,因为 BorderLayout 可以很好地工作,允许绘图 JPanel 填充 JFreme 的中心。

小问题:

  • 避免使用与常见 Java 核心类冲突的类名称,例如 Java 正则表达式分析中经常使用的 Pattern
  • 不知道为什么您甚至需要 Pattern 类,因为它没有做任何在 main 方法中无法完成的有用的事情。

例如,Rect(或这里命名为 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2020-02-05
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多