在Mac上无法绘图 [英] Swing not drawing on Mac

查看:194
本文介绍了在Mac上无法绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java中的swing绘制令人难以置信的基本形状,但是由于某些原因,它似乎无法正常工作.这是我从讲师那里下载的代码,他在一次演讲中向我们展示了该代码,但是当我运行该代码时,窗口会打开,但什么都没画,我也不知道为什么.

I am trying to draw an incredibly basic shape using swing in Java, however for some reason it does not seem to be working. This is code that I downloaded from my lecturer that he showed us in a lecture, but when I run it the window opens but nothing is drawn and I have no idea why.

package graphicsEx;

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

public class Lecture1Example extends JPanel{
    // This is where the JPanel gets (re-)painted when the screen is refreshed.
    public void paintComponent(Graphics g) {
        // Cast to Graphics2D for more features.        
        Graphics2D g2D = (Graphics2D) g;

        Rectangle2D rect = new Rectangle2D.Double(20,30,40,50);
        g2D.setColor(Color.red);
        g2D.draw(rect);
        g2D.fill(rect); 
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame("Playing with Graphics");
        frame.setSize(500, 400);
        frame.setVisible(true);
        frame.setContentPane(new Lecture1Example());        
    }
}

我正在使用Eclipse IDE.

I am using the Eclipse IDE.

推荐答案

尊敬的user1821475的讲师:

Dear user1821475's lecturer:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆