我正在尝试使用Graphics并绘制一些矩形

时间:2018-07-29 16:48:53

标签: java swing class object graphics

你好,我正在尝试学习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;

    }

}

1 个答案:

答案 0 :(得分:1)

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

  • 在Rect类中过度使用静态修饰符。通过使用静态字段,Rect实例将没有自己的唯一状态,自己的颜色和位置。将所有这些字段设为私有非静态(实例)。如果这会导致编译问题,请通过不使字段为静态,而不要尝试从类中访问它们来解决此问题。
  • 如果您想在类外更改它们,还可以给这些字段设置方法。还有getter方法,如果您想查询它们的话
  • 您将忽略JFrame的contentPane默认使用BorderLayout的事实。此布局将覆盖接下来添加的所有内容,覆盖先前添加的组件。如果您需要在此容器中使用多个组件,请使用其他布局
  • 但是您的主要问题是Rect不应该扩展JPanel,它不应该是 component 类,而应该是逻辑类。
  • 相反,创建一个扩展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);