如何使用Java和Jframe类绘制矩形?

发布于 2025-01-31 02:24:15 字数 988 浏览 1 评论 0原文

我现在要学习Java(C ++之后),我正在尝试使用GUI编程。

我在此代码中的目标是将一些简单的东西绘制到使用Jframe库创建的窗口中。

这是我的代码:

import gui.Gui;

public class Main
{
   public static void main (String[] args)
    {
        Gui g = new Gui();
        g.paint(null);
    }
}

package gui;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

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

public class Gui extends JFrame
{
    public Gui()
    {
        JFrame jf = new JFrame("Beispiel JFrame");
        jf.setTitle("Draw Something");
        jf.setSize(960, 960);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);

        ImageIcon img = new ImageIcon("./img.png");
        jf.setIconImage(img.getImage());       
    }
    public void paint(Graphics g)
    {
        g.drawRect(480, 480, 200, 100);
    }
}

它没有错误消息编译,但什么也没绘制。 我找不到这样做的原因,谁能帮我:)?

(并且,如果格式不是“完美”的,我是堆叠的新手。)

Im about to learn Java (after C++) now,and I am trying myself in Gui Programming.

My goal in this code was to draw something simple into a window created with the JFrame library.

This is my code:

import gui.Gui;

public class Main
{
   public static void main (String[] args)
    {
        Gui g = new Gui();
        g.paint(null);
    }
}

package gui;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

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

public class Gui extends JFrame
{
    public Gui()
    {
        JFrame jf = new JFrame("Beispiel JFrame");
        jf.setTitle("Draw Something");
        jf.setSize(960, 960);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);

        ImageIcon img = new ImageIcon("./img.png");
        jf.setIconImage(img.getImage());       
    }
    public void paint(Graphics g)
    {
        g.drawRect(480, 480, 200, 100);
    }
}

it compiles without an error message, but it draws nothing.
I am unable to find the reason for that, could anyone help me :) ?

(And sry if the formatting is not "perfect", I am new to StackOverflow.)

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

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

发布评论

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

评论(1

羁客 2025-02-07 02:24:15

通常建议您不要从顶级容器延伸,例如jframe。您不会在课程中添加任何新功能,而是将自己锁定到一个用例中。 jframe也是一个复合组件,也就是说,它包含许多构成其核心功能的儿童组件,并且由于绘画系统的工作方式,因此儿童组件有可能进行绘画与父容器/组件无关,在您的情况下,这将产生很多问题。

我建议您首先阅读在AWT和AWT和绘画中摇摆更好地了解油漆系统的工作原理和执行自定义绘画更好地了解如何使用它。

您可能还想阅读 2D Graphics trail 。

另外,作为一般的经验法则,除非您愿意承担所覆盖方法的全部责任,否则您应该调用其super实现。在这种情况下,绘画是复杂的工作流程,因此您基本上已经完全规避了它,这将无法很好地结束。

回到您的问题,而不是使用jframe和覆盖paint(通常是不符合要求的),您应该从jpanel和Override PaintComponent而不是

... /I.sstatic.net/cygjw.png“ alt =”在此处输入图像说明”>

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        public MainPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int width = getWidth() / 4;
            int height = getHeight() / 2;
            int x = (getWidth() - width) / 2;
            int y = (getHeight() - height) / 2;
            g2d.setColor(Color.RED);
            g2d.drawRect(x, y, width, height);
            g2d.dispose();
        }
    }
}

It's generally recommend that you NOT extend from a top level container like JFrame. You're not adding any new functionality to the class and you're locking yourself into a single use case. JFrame is also a compound component, that is, it contains a number of child components which make up its core functionality, and, because of the way the painting system works, it's possible for a child component to painted independently of the parent container/component, which, in your case, is going to produce a lot of issues.

enter image description here

I would recommend you start by reading through Painting in AWT and Swing to get a better understanding of how the paint system works and Performing Custom Painting to better understand how you should work with it.

You probably also want to read through the 2D Graphics trail.

Also, as a general rule of thumb, unless you're willing to take over the full responsibility of the method you're overriding, you should be calling its super implementation. In this case, painting is complex workflow, so you've basically completely circumvented it, which is not going to end well.

Back to your problem, instead of using JFrame and overriding paint (which is generally un-recommended), you should start with something like JPanel and override paintComponent instead, for example...

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        public MainPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int width = getWidth() / 4;
            int height = getHeight() / 2;
            int x = (getWidth() - width) / 2;
            int y = (getHeight() - height) / 2;
            g2d.setColor(Color.RED);
            g2d.drawRect(x, y, width, height);
            g2d.dispose();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文