使用 Java (awt) 绘制多个矩形

发布于 2024-08-18 05:02:35 字数 1451 浏览 3 评论 0原文

这是我的代码:

class Ramka extends JFrame
    {
    public static final int SZEROKOSC = 800;
    public static final int WYSOKOSC = 600;
    Container powZawartosci = getContentPane();
    public Ramka()
        {
        setSize(SZEROKOSC, WYSOKOSC);
        setTitle("Siatka bryły by Paweł Mysior");
        }
    public void addRectangle(int startX, int startY, int sizeX)
        {
        drawRectangle rect = new drawRectangle(startX, startY, sizeX);
        powZawartosci.add(rect);
        }  

class drawRectangle extends JPanel
    {
    private int a, startX, startY;
    public drawRectangle(int startX, int startY, int a) // square
        {
        this.a = a;
        this.startX = startX;
        this.startY = startY;
        }
    public void paintComponent(Graphics g)
        {
        Rectangle2D rect = new Rectangle2D.Double(startX, startY, a, a);
        Graphics2D g1 = (Graphics2D) g;
        g1.draw(rect);
        }
    }

public class Main
    {
    public static void main(String[] args)
        {
        Ramka ramka = new Ramka();
        ramka.addRectangle(200, 200, 50);
        ramka.addRectangle(100, 100, 100);
        ramka.addRectangle(300, 300, 150);
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.setVisible(true);
        }
    }

我想要它做的是绘制三个矩形(抛开这样做的功能和意义,我仍然在学习)。

但它只绘制最后一个,从 300 和 300 开始。我不太明白 PaintComponent 的东西...

提前感谢您的帮助, 保罗

Here's my code:

class Ramka extends JFrame
    {
    public static final int SZEROKOSC = 800;
    public static final int WYSOKOSC = 600;
    Container powZawartosci = getContentPane();
    public Ramka()
        {
        setSize(SZEROKOSC, WYSOKOSC);
        setTitle("Siatka bryły by Paweł Mysior");
        }
    public void addRectangle(int startX, int startY, int sizeX)
        {
        drawRectangle rect = new drawRectangle(startX, startY, sizeX);
        powZawartosci.add(rect);
        }  

class drawRectangle extends JPanel
    {
    private int a, startX, startY;
    public drawRectangle(int startX, int startY, int a) // square
        {
        this.a = a;
        this.startX = startX;
        this.startY = startY;
        }
    public void paintComponent(Graphics g)
        {
        Rectangle2D rect = new Rectangle2D.Double(startX, startY, a, a);
        Graphics2D g1 = (Graphics2D) g;
        g1.draw(rect);
        }
    }

public class Main
    {
    public static void main(String[] args)
        {
        Ramka ramka = new Ramka();
        ramka.addRectangle(200, 200, 50);
        ramka.addRectangle(100, 100, 100);
        ramka.addRectangle(300, 300, 150);
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.setVisible(true);
        }
    }

What I want it to do is draw three rectangles (set aside the functionality and sense of doing so, I'm still just learning).

But it draws only the last one, starting at 300 and 300. I don't really understand the paintComponent thing...

Thanks in advance for any help,
Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥︴琐丶欲为 2024-08-25 05:02:35

我相信您正在相互添加三个 JPanel 。这似乎是一种奇怪的绘制矩形的方法,但对于这种设计,您需要使用 LayoutManager

查看链接,并尝试学习。不过下面的代码应该可以解决问题。

...
Container powZawartosci = getContentPane();
public Ramka()
    {
    setSize(SZEROKOSC, WYSOKOSC);
    setTitle("Siatka bryły by Paweł Mysior");
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));//Only this line is inserted.
    }
public void addRectangle(int startX, int startY, int sizeX)
    {
    drawRectangle rect = new drawRectangle(startX, startY, sizeX);
    powZawartosci.add(rect);
    }  
...

在您的 JPanel 衍生产品中,您可以跟踪需要绘制的 矩形。我正在自发地编写下面的代码,因此请先检查是否有错误。

class RectangleDrawer extends JPanel{
    ArrayList<Rectangle> rList = new ArrayList()<Rectangle>;
    public void addRectangle(Rectangle rect){
        rList.add(rect);
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for(int i=0; i<rList.size(); r++){
            g.drawRectangle(rList.get(i));
        }
    }
}

I beleive that you are adding three JPanels on top of each other. This seems like an odd way to draw rectangles, but with this design, you need to use a LayoutManager.

Check out this link, and try to learn. The code below should do the trick though.

...
Container powZawartosci = getContentPane();
public Ramka()
    {
    setSize(SZEROKOSC, WYSOKOSC);
    setTitle("Siatka bryły by Paweł Mysior");
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));//Only this line is inserted.
    }
public void addRectangle(int startX, int startY, int sizeX)
    {
    drawRectangle rect = new drawRectangle(startX, startY, sizeX);
    powZawartosci.add(rect);
    }  
...

In your JPanel derivative, you can keep track of the Rectangles that you need to draw. I am writing the code below spontanously, so check for errors first.

class RectangleDrawer extends JPanel{
    ArrayList<Rectangle> rList = new ArrayList()<Rectangle>;
    public void addRectangle(Rectangle rect){
        rList.add(rect);
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for(int i=0; i<rList.size(); r++){
            g.drawRectangle(rList.get(i));
        }
    }
}
手心的温暖 2024-08-25 05:02:35

问题基本上在于您在这里使用了两种不同的抽象级别。

首先,您向 JFrame 添加一个组件,这在某些时候是没问题的。

您正在添加“DrawRectangle”实例,就像添加新按钮、标签或另一个面板一样。当您在同一位置添加组件时,问题就会出现。 JFrame 的主面板(内容窗格)使用 " 边框“布局管理器,如果您不添加任何约束,则将组件放置在中间。

为了方便起见,BorderLayout 将字符串规范的缺失解释为与常量 CENTER 相同

alt text

因此,这一行:

powZawartosci.add(rect);

始终将您的组件添加到“中心”,覆盖前一个组件。这就是为什么你只看到一个矩形。

这里使用的第二个抽象层次是自己绘制组件。这是低级别的,您必须告诉组件谁在哪里绘制每条线。

这很好,但是如果您想在同一个组件中绘制多个矩形,则必须保存每个矩形的引用(使用像列表这样的集合),然后迭代该集合并绘制所有矩形。

像这样:

许多http://img40.imageshack.us/img40/8125/capturadepantalla201001nd。 png

我获取了你的代码,并对其进行了更改,以反映我所说的内容。最终结果使用相同的组件,但该组件依次绘制所有矩形。

另请注意命名/大括号样式,虽然不是强制性的,但在 Java 编程中很常见

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;

class Ramka extends JFrame {
    public static final int SZEROKOSC = 800;
    public static final int WYSOKOSC = 600;
    Container powZawartosci = getContentPane();
    DrawRectangle rectangle = new DrawRectangle();
    public Ramka() {
        setSize(SZEROKOSC, WYSOKOSC);
        setTitle("Siatka bryły by Paweł Mysior");
        powZawartosci.add( new JLabel("Several rectangles are being displayed"), BorderLayout.NORTH );
        powZawartosci.add(rectangle);
    }
    public void addRectangle(int startX, int startY, int sizeX) {
        this.rectangle.addRectangle( startY, startY, sizeX );
    }

}  

class DrawRectangle extends JPanel {
    private java.util.List<Rectangle2D> squares;
    //private int a, startX, startY;
    public DrawRectangle(){
        squares = new ArrayList<Rectangle2D>();
    }

    public void addRectangle(int startX, int startY, int a)  { // square
        squares.add( new Rectangle2D.Double(startX, startY, a, a) ) ;
        //this.a = a;
        //this.startX = startX;
        //this.startY = startY;
    }
    public void paintComponent(Graphics g) {
        Graphics2D g1 = (Graphics2D) g;
        for( Rectangle2D rect : squares ) {
            g1.draw(rect);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Ramka ramka = new Ramka();
        //ramka.addRectangle(200, 200, 50);
        //ramka.addRectangle(100, 100, 100);
        //ramka.addRectangle(300, 300, 150);
        for( int i = 0 ; i < 20 ; i++ ){
            ramka.addRectangle( i * 10 , i * 10 , i * 20 );
        } 
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.setVisible(true);
    }
}

The problem basically is that you're using two different levels of abstraction here.

In the first, you are adding a component to your JFrame, which is fine at some point.

You're adding your "DrawRectangle" instance, just the same way you would add a new button, a label or another panel. The problem comes when you add components in the same position. JFrame's main panel ( the content pane ) uses a "Border" layout manager that places the component in the middle if you don't add any constraint.

As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER

alt text

So, this line:

powZawartosci.add(rect);

Always adds your component in the "center", overriding the previous one. That's why you only saw one rectangle.

The second level of abstraction used here is painting the component yourself. This is low level and you have to tell the component who to draw each line and where.

That's fine, but if you want to draw several rectangles in the same component, you have to hold the references for each one ( using a collection like a list ) and then iterate that collection and draw them all.

Like this:

many http://img40.imageshack.us/img40/8125/capturadepantalla201001nd.png

I took your code, and changed it, to reflect what I'm saying. The final result, uses the same component, but this component in turn draws all the rectangles.

Notice also the naming/brace style, while is not mandatory it is common while programming in Java

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;

class Ramka extends JFrame {
    public static final int SZEROKOSC = 800;
    public static final int WYSOKOSC = 600;
    Container powZawartosci = getContentPane();
    DrawRectangle rectangle = new DrawRectangle();
    public Ramka() {
        setSize(SZEROKOSC, WYSOKOSC);
        setTitle("Siatka bryły by Paweł Mysior");
        powZawartosci.add( new JLabel("Several rectangles are being displayed"), BorderLayout.NORTH );
        powZawartosci.add(rectangle);
    }
    public void addRectangle(int startX, int startY, int sizeX) {
        this.rectangle.addRectangle( startY, startY, sizeX );
    }

}  

class DrawRectangle extends JPanel {
    private java.util.List<Rectangle2D> squares;
    //private int a, startX, startY;
    public DrawRectangle(){
        squares = new ArrayList<Rectangle2D>();
    }

    public void addRectangle(int startX, int startY, int a)  { // square
        squares.add( new Rectangle2D.Double(startX, startY, a, a) ) ;
        //this.a = a;
        //this.startX = startX;
        //this.startY = startY;
    }
    public void paintComponent(Graphics g) {
        Graphics2D g1 = (Graphics2D) g;
        for( Rectangle2D rect : squares ) {
            g1.draw(rect);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Ramka ramka = new Ramka();
        //ramka.addRectangle(200, 200, 50);
        //ramka.addRectangle(100, 100, 100);
        //ramka.addRectangle(300, 300, 150);
        for( int i = 0 ; i < 20 ; i++ ){
            ramka.addRectangle( i * 10 , i * 10 , i * 20 );
        } 
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文