简体   繁体   English

JPanel中的Java代码绘画

[英]Java Code Painting in JPanel

I just started learning Java and am now recently learning new things outside of the console. 我刚开始学习Java,现在最近在控制台之外学习新东西。 I copied a code from The Professor's board but i can't seem to get it to work. 我从教授董事会复制了一个代码,但似乎无法正常工作。 It is suppose to draw circles and rectangles for now but i just get a blank window when i try to paint. 假设现在要绘制圆形和矩形,但是当我尝试绘画时,我只是得到一个空白窗口。 I think i may have missed a line of code or something. 我想我可能错过了一行代码或其他东西。 I am using Eclipse on Mac. 我在Mac上使用Eclipse。

I understand there may be many ways to do this but i want to kind of keep the code exactly how he has it for now in addition with whatever fixes you think will make it work. 我知道可能有很多方法可以做到这一点,但是我想让代码暂时保持他现在的状态,以及您认为可以使代码工作的任何修复方法。 Thanks alot. 非常感谢。

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

public class Bunny {
    public static void main(String [] args)
    {
        MyFrame f = new MyFrame();
    f.setSize(500,400);
    f.setVisible(true);
    f.setLocation(50,100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }






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

public class MyFrame extends JFrame
    {
        public MyFrame()
        {   
        }
    }



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


public class MyPanel extends JPanel
{
public void paintComponent(Graphics g)
{Graphics2D g2 = (Graphics2D) g;


Ellipse2D.Double Circle = new Ellipse2D.Double(100,50,75,75);

g2.setColor(Color.RED);
g2.fill(Circle);

Rectangle box = new Rectangle(200,100,150,150);
g2.setColor(Color.RED);
g2.fill(box);

Color myColor = new Color(255,0,0);
}
}

I'd like to, very much, tell your professor that they need to go back to school and learn how to use Swing properly 我非常想告诉您的教授,他们需要回到学校,学习如何正确使用Swing

You should start by taking a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing 您应该先看一下AWT和Swing中的 绘画以及执行自定义绘画 ,以了解有关在Swing中绘画如何工作的更多详细信息。

例

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            Ellipse2D.Double Circle = new Ellipse2D.Double(100, 50, 75, 75);

            g2.setColor(Color.RED);
            g2.fill(Circle);

            Rectangle box = new Rectangle(200, 100, 150, 150);
            g2.setColor(Color.RED);
            g2.fill(box);
            g2.dispose();
        }

    }

}

Code Review... 代码审查...

public class Bunny {

    public static void main(String[] args) {
        MyFrame f = new MyFrame();
        f.setSize(500, 400);
        f.setVisible(true);
        f.setLocation(50, 100);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Bad: Interacting with UI from outside the context of the Event Dispatching Thread. 不良:从事件调度线程的上下文外部与UI交互。 See Initial Threads for details 有关详细信息,请参见初始线程

public class MyFrame extends JFrame {

    public MyFrame() {
    }
}

Bad: Extending from a top level container is (like JFrame ) is generally discourage, you're not adding any new features to the class and this is one of the areas where most problems occur. 不好:通常不鼓励从顶级容器中进行扩展(例如JFrame ),而无需在类中添加任何新功能,这是发生大多数问题的领域之一。 You might like to do some research into "composition over inheritance". 您可能想对“组成于继承”进行一些研究。

You also not actually adding anything to the frame, so it will appear blank. 您实际上也没有在框架中添加任何内容,因此它将显示为空白。

public class MyPanel extends JPanel {

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        Ellipse2D.Double Circle = new Ellipse2D.Double(100, 50, 75, 75);

        g2.setColor(Color.RED);
        g2.fill(Circle);

        Rectangle box = new Rectangle(200, 100, 150, 150);
        g2.setColor(Color.RED);
        g2.fill(box);

        Color myColor = new Color(255, 0, 0);
    }
}

Bad: paintComponent should never be public , there is no reason ever that any other class should be calling this method. 坏: paintComponent永远不应该是public ,没有任何其他类可以调用此方法。 Not calling super.paintComponent will cause no end of graphic glitches and issues, you should always call super.paintComponent first. 不调用super.paintComponent不会引起图形故障和问题,您应该始终首先调用super.paintComponent The only times you wouldn't is when you know what you're doing and you have an extremely good reason not to...which is like <1% of the time. 只有当您知道自己在做什么时,您才不会这样做,而您极有充分的理由不这样做……大约少于1%的时间。

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

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