java - 理解java swing的困难

标签 java swing java-2d

我试图通过单击 JMenu 在 JPanel 上绘制随机(尚未)圆圈。 我使用 JTextField (并且必须保留它)来进行某些输出。 这是我的类(class):

class RandomDrawer extends JPanel implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        //double x = Math.random();
        //double y = Math.random();
        Random generator = new Random();
        int x = generator.nextInt(100)+1;
        int y = generator.nextInt(100)+1;
        //System.out.printf("x = %d y = %d\n", x, y);
        status.setText(String.format("rnd draw x: %d y: %d", x, y));
        Graphics2D gg = (Graphics2D) canvas.getGraphics();
        gg.setColor(Color.BLACK);
        gg.drawOval(50, 50, 50, 50);
    }
}

只要我放线

status.setText(String.format("rnd 绘制 x: %d y: %d", x, y));

呆在那里我什么也没画。没有它我就得到了我的圈子,我不确定问题是什么。我不明白为什么没有绘制任何内容。

非常感谢

编辑:

您好,我试图理解给定的信息。 遗憾的是我必须使用 Graphics2D 类进行绘制,所以我想我无法使用形状进行绘制。所以我尝试了这个,遗憾的是它还不会绘制,你能给我一些提示吗? 我尝试创建一个新类 DrawShape,我的想法是我可以跟踪这些对象。 根据我的理解,现在应该有一个绘制的椭圆形
gg.drawOval(100,100,100,100);

谢谢。

class DrawShape {
    public DrawShape(String string) {
        // TODO Auto-generated constructor stub
    }
}

class RandomDrawer extends JPanel implements ActionListener {
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */ 
    private List<DrawShape> shapes = new ArrayList<DrawShape>();


    public void addShape(DrawShape s) {
        shapes.add(s);
        repaint();
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        final Graphics2D gg = (Graphics2D) g;
        gg.setColor(Color.BLACK);
        gg.drawOval(100, 100, 100, 100);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        Random generator = new Random();
        int x = generator.nextInt(100)+100;
        int y = generator.nextInt(100)+100;

        if (e.getActionCommand()==("Draw RandomCircle")) {
            System.out.printf("x = %d y = %d\n", x, y);
            status.setText(String.format("rnd draw x:%d  y:%d ", x, y));
            DrawShape circle = new DrawShape("Circle");
            addShape(circle);
            int count = shapes.size(); 
            System.out.printf("objects in array: %d\n", count); 
        }

        else if (e.getActionCommand()==("Draw RandomRectangle")) {
            System.out.printf("x = %d y = %d\n", x, y);
            //status.setText(String.format("rnd draw x:  y: "));
            //Graphics2D gg = (Graphics2D) canvas.getGraphics();
            //gg.setColor(Color.BLACK);
            //gg.drawRect(x, y, generator.nextInt(x), generator.nextInt(y));
        }
    }
}

最佳答案

绘画等都是由事件驱动发生的。如果需要重新绘制组件的一部分,则调用其 paintComponent 方法。

这意味着您需要一个现在可以绘制的组件,例如:

public class DrawShape {

    public final String text;
    public final Color color;
    public final Shape shape;

    public DrawShape(String text, Color color, Shape shape) {
        this.text = text;
        this.color = color;
        this.shape = shape;
    }
}

public class CanvasWithShapes extends JPanel {

    private List<DrawShape> shapes = new ArrayList<>();

    public void addShape(DrawShape shape) {
        shapes.add(shape);
    }

    @Override
    public void paintComponent(Graphics g) {
        final Graphics2D gg = (Graphics2D) g;
        // Java 8: shapes.stream().forEach((shape) -> gg.draw(shape));
        for (DrawShape drawShape : shapes) {
            gg.setColor(drawShape.color);
            gg.draw(drawShape.shape);
            Rectangle bounds = shape.getBounds();
            gg.drawString(shape.text, bounds.x+ 10, bounds.y + 20);
        }
    }
}

然后添加要稍后重绘的形状。

Shape oval = ...;
c.add(oval);
c.repaint(50L); // A bit later 
<小时/>

更详细

一个Shape有许多有趣的实现,例如矩形和椭圆形。 Graphics2D 可以绘制和填充形状。因此,在您的情况下,添加这样的形状是理想的选择。也许与颜色和文字一起。所以我用你的 DrawShape 类来保存这些属性。

    Random generator = new Random();
    int x = generator.nextInt(100)+100;
    int y = generator.nextInt(100)+100;

    if (e.getActionCommand().equals("Draw RandomCircle")) {
        System.out.printf("x = %d y = %d\n", x, y);
        status.setText(String.format("rnd draw x:%d  y:%d ", x, y));
        int w = generator.nextInt(100) + 10;
        int h = w;
        Shape circle = new Ellipse2D.Double(x, y, w, h);
        addShape(new DrawShape(text, Color.BLACK, circle));
        int count = shapes.size(); 
        System.out.printf("objects in array: %d\n", count); 
    } else if (e.getActionCommand().equals("Draw RandomRectangle")) {
        System.out.printf("x = %d y = %d\n", x, y);

generator.nextInt(y)); int w = 生成器.nextInt(100) + 10; int h = 生成器.nextInt(100) + 10; 形状矩形 = Rectangle2D.Double(x, y, w, h) addShape(new DrawShape(文本, Color.BLACK, 矩形)); }

关于java - 理解java swing的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36518270/

相关文章:

java - 从下面的程序中的不同列表中删除公共(public)元素的算法

java - Wowza - onHTTPCupertinoStreamingSessionCreate 被多次调用

java - 代码示例中的堆栈溢出解释

java - Jasper 报告在 netbeans 中工作正常,但在部署为 jar 应用程序时出现异常

java - java游戏开发中的窗口崩溃

java - 如何在MySQL中动态地将行转换为列并在Swing中显示结果

Java 游戏 - ClassCastException

java - 在子窗口中扩大或缩小 vaadin 日历组件的大小

Java移动矩形更快?

java - 相对于面板java绘制椭圆