了解困难的Java摇摆 [英] understanding difficulties java swing

查看:52
本文介绍了了解困难的Java摇摆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图通过单击JMenu在JPanel上绘制随机(尚未)的圆圈. 我正在使用JTextField(并且必须保留此输出)用于某些输出. 这是我的课程:

im trying to paint random (not yet) circles on a JPanel through click on a JMenu. Im using a JTextField (and have to keep this) for some output. Here is my 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 draw x:%d y:%d",x,y));

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

呆在那里,我什么都没画.没有它,我就不知所措了,我不确定是什么问题.我不知道为什么什么也没画.

stay in there i get nothing drawn. Without it i get my circle, im not sure what the problem is. I cant figure out why nothing is drawn.

非常感谢

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

Hello, i tried to understand the given informations. Sadly I have to draw using the Graphics2D class, so i guess i can not draw using shapes. So i tried this, sadly it wont draw yet, can u give me some tips? I tried to create a new class DrawShape, my thought was that i could keep track with those objects. In my understanding there should be a drawn oval right now
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方法.

Painting and such happens event-driven. If a a piece of a component needs to be redrawn its paintComponent method is called.

这意味着您需要一个组件,例如,现在该如何绘制:

This means you need a component that nows how to draw by for instance:

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);
        }
    }
}

然后添加形状以便稍后重绘.

And then just add shapes to be redrawn a bit later.

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


更详细


More detailed

形状具有许多实现像矩形和椭圆形的兴趣. Graphics2D可以绘制和填充Shape.因此,在您的情况下,添加这样的Shape是理想的.也许还有颜色和文字.因此,我带上了DrawShape类来保存这些属性.

A Shape has many implementations of interest like rectangle and oval. Graphics2D can draw and fill a Shape. So in your case it would be ideal to add such a Shape. Maybe together with color and text. So I took your DrawShape class to hold these properties.

    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)); 整数w = generator.nextInt(100)+ 10; int h = generator.nextInt(100)+ 10; 形状rect = Rectangle2D.Double(x,y,w,h) addShape(new DrawShape(text,Color.BLACK,rect)); }

generator.nextInt(y)); int w = generator.nextInt(100) + 10; int h = generator.nextInt(100) + 10; Shape rect = Rectangle2D.Double(x, y, w, h) addShape(new DrawShape(text, Color.BLACK, rect)); }

这篇关于了解困难的Java摇摆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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