简体   繁体   English

Java 使用 Graphics 在 JFrame 上显示形状

[英]Java using Graphics to display shapes on a JFrame

I am learning how to program a graphical user interface in Java.我正在学习如何用 Java 编写图形用户界面。 I pretty much know some basics but in this program, I am trying to draw onto a JFrame with a black background, but as soon as I run the program the JFrame only displays a white line on a white background.我几乎知道一些基础知识,但在这个程序中,我试图在黑色背景上绘制 JFrame,但是一旦我运行该程序,JFrame 只会在白色背景上显示一条白线。 I would appreciate it very much if anyone knew how to fix this, I have been trying myself but I can't seem to figure it out.如果有人知道如何解决这个问题,我将不胜感激,我一直在尝试自己,但我似乎无法弄清楚。 Thanks for your attention.感谢您的关注。 I'm looking forward to a reply.我期待着回复。

public class test1 {
public static void main (String[] args)
{
    JFrame frame = new JFrame();
    frame.setSize(1835,1019);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.getContentPane().setBackground(Color.BLACK);

    
    JPanel raum = new JPanel()
    {
        public void paint(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(Color.WHITE);
            
            g2.drawLine(500,500,500,800);
        }
        
    };
    frame.add(raum);
}

} }

The raum Panel is the one occupying the frame, so the background of the frame is the same background color of the JPanel. raum Panel 是占据框架的,所以框架的背景与JPanel 的背景颜色相同。 So you have to paint the background before drawing the other shapes.所以你必须在绘制其他形状之前绘制背景。

JPanel raum = new JPanel()
{
    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.fillRect(0, 0, this.getWidth(), this.getHeight());

        g2.setPaint(Color.WHITE);

        g2.drawLine(500,500,500,800);
    }

};

There a number of issues which are going to cause you endless amount of problems going into the future.有许多问题会导致您在未来遇到无穷无尽的问题。

The obvious one is the fact that the background color of the panel is very close to WHITE , so it makes it very difficult to see the line.显而易见的一个事实是面板的背景颜色非常接近WHITE ,因此很难看到线条。 You could change the background color of the panel or the line and it should solve the immediate issue.您可以更改面板或线条的背景颜色,它应该可以解决当前的问题。

You really need to take a look at Performing Custom Painting and Painting in AWT and Swing to get a better understanding of how painting works in Swing.您确实需要查看在 AWT 和 Swing执行自定义绘画绘画,以更好地了解绘画在 Swing 中的工作原理。

It is generally recommended to override paintComponent and avoid overriding paint .通常建议覆盖paintComponent并避免覆盖paint paint does a lot work and unless you're willing to take over ALL it's workload, you're better off avoiding it. paint做了很多工作,除非你愿意承担所有的工作量,否则最好避免它。

As a general rule, you should also call the super.paintXxx method before you do any custom painting.作为一般规则,您还应该在进行任何自定义绘画之前调用super.paintXxx方法。 Again, painting is generally a complex workflow, best to just let the parent class do its job.同样,绘画通常是一个复杂的工作流程,最好让父类完成它的工作。

A component should also provide sizing hints back to the parent container, the parent container can then make better decisions (via the LayoutManager ) as to how all the components should be laid out.一个组件还应该向父容器提供大小提示,然后父容器可以做出更好的决定(通过LayoutManager )关于所有组件的布局方式。 Because different platforms (and even same platforms with different settings) can generate different size window decorations, you're better off managing the size of the "content" over the size of the "window".因为不同的平台(甚至具有不同设置的相同平台)可以生成不同大小的窗口装饰,所以最好管理“内容”的大小而不是“窗口”的大小。 Again, this is going to save you no end of headaches into the future.同样,这将让您在未来无止境地头痛。

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 TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBackground(Color.BLACK);
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setPaint(Color.WHITE);
            g2d.drawLine(500, 500, 500, 800);
            g2d.dispose();
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM