从另一个类JFrame调用重画 [英] Calling repaint from another class JFrame

查看:63
本文介绍了从另一个类JFrame调用重画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个类调用重绘.但这是行不通的.我必须画一个框架.

I'm trying to call repaint from another class. But it does not work. I have to draw on a frame.

public class Tester extends JFrame{

    public static dtest d ;
    public static void main(String[] args) {
        Tester t = new Tester();
        d = new dtest();
        test tnew = new test();
    }

    public static class dtest extends JFrame implements MouseMotionListener{
        public static int x,y;
        dtest()
        {
            super("title");
            setSize(500,500);
            setVisible(true);
            addMouseMotionListener(this);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            x = e.getX();
            y = e.getY();
            repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        public void paint(Graphics g)
        {
            System.out.println("I am called");
        }
    }

    public static class test {
        public test()
        {   
            for(int i = 0 ; i < 5 ; i++)
            {
                System.out.println("I am called from run");
                d.repaint();
            }
        }
    }

}

此打印

I am called from run

I am called from run

I am called from run

I am called from run

I am called from run

,因此它不执行paint()部分. d.repaint()不起作用.为什么?

so it does not executing the paint() portion. d.repaint() is not working. why?

推荐答案

看看此页面,并查看第一个答案.这与您的问题很相似,甚至不完全相同.

Take a look at this page and look at the first answer. It's a similar if not exact question to yours.

JFrame的paint()方法已被弃用.编译器或您的IDE应该有点抱怨,尤其是将@Override标记直接放在该方法上方时(使用此方法可以测试该方法是否可以被重写,也就是您要执行的操作).

JFrame's paint() method has been deprecated. The compiler, or your IDE, should be complaining a bit, especially if you place the @Override tag directly above the method (use this to test if this method can be rewritten... aka what you're trying to do).

这意味着不鼓励使用它,并且某些功能可能已删除.使用javax.swing时,您将希望完全了解JPanelsJComponents的系统.要在屏幕上绘画,您需要添加一个自定义类,该类使用add(Component c)方法扩展了JPanel.然后,重写该类中的paintComponent(Graphics g)方法.确保该方法的第一行为super.paintComponent(g);,以便窗口可以刷新自身.

This means that its use has been discouraged and some functionality may have been removed. When using javax.swing, you'll want to learn the system completely about JPanels and JComponents. To paint something on a screen, you'll want to add a custom class that extends JPanel with the add(Component c) method. Then, override the paintComponent(Graphics g) method in that class. Make sure to have the first line in that method be super.paintComponent(g); so that the window can refresh itself.

出于完整性考虑:

public class MyWindow extends JFrame {

    MyPanel thePanel;

    public MyWindow(int x, int y) {
        setSize(x, y);
        thePanel = new MyPanel(x, y);
        this.add(thePanel);
    }

}

public class MyPanel extends JPanel {
    public MyPanel(int x, int y)
        setSize(x, y);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(ImageManager.getImage("Cute Puppy"), 40, 40, null); // Or whatever
    }
}

因此,当在MyWindow上调用repaint()revalidate()方法时,面板将收到paintComponent调用.

So, when the repaint() or revalidate() method is called on the MyWindow, the Panel will recieve a paintComponent call.

如果您需要任何其他帮助,请在评论中让我知道.

Please let me know in the comments if you need any additional help.

由于您需要使用MouseMotionListener,而且我仍然不太了解我需要从另一个类调用重绘"的上下文和麻烦……我会尽力而为.

Since you need to use MouseMotionListener, and I'm still not quite understanding the context and trouble of "I need to call repaint from another class"... I will try my best.

首先,在Oracle上查看本教程页面.另外,在GUI上查看其他内容.您将学到很多有关组织和显示的知识,这将使您意识到他们的系统如何与您的系统一起工作.

Firstly, check out this tutorial on the Oracle pages. Also, check out the others on GUI's. You'll learn a lot about organization and displaying that will make you realize how their system can work with yours.

现在,您的问题:

i have to use MouseMotionListener.

不太...这是一种设置的好方法,但是您可以运行一个Thread(不断不断地运行方法的东西)来检查Mouse坐标.当您进入游戏和其他各种应用程序时,您将要开始执行此操作.

Not quite... it is a good way for set up but you can run a Thread (something that constantly runs methods over and over) to check the Mouse coordinates. You'll want to start doing this when you get into games and other miscellaneous applications.

new Thread() {
    public void run() {
        Point mouse;
        int mousex;
        int mousey;
        while (true) {
            mouse = MouseInfo.getPointerInfo().getLocation();
            mousex = mouse.x - theWindow.getLocationOnScreen().x - 3; // You'll need to get the 
                // x coordinate, subtract the window's x coordinate, and subtract 3 because of 
                // the blue border around a standard pc window.
            mousey = mouse.y - theWindow.getLocationOnScreen().y - 29; // 29 is top bar height
            SomeOtherClass.processMove(mousex, mousey);
        }
    }
}.start();

下一步:I tried that with JPanel but i could not do that.如果您在编辑顶部阅读了该教程,就会发现他们很容易实现MouseMotionListener.

Next: I tried that with JPanel but i could not do that. If you read the tutorial at the top of my edit, you see they implement MouseMotionListener with ease.

下一步:I prefer to do it with JFrame.如果要在JFrame中处理鼠标,请执行以下操作:使JFrame成为侦听器,但JPanel是鼠标数据的来源.如下:

Next: I prefer to do it with JFrame. If you wish to process the mouse in the JFrame, do the following: Have your JFrame the listener, but the JPanel be where the mouse data comes from. As follows:

public class MyWindow extends JFrame implements MouseMotionListener {
    public MyPanel thePanel;
    public int x;
    public int y;

    public MyWindow() {
        thePanel = new MyPanel();
        thePanel.addMouseMotionListener(this); 
            // Make this JFrame get called when the mouse                    
            // moves across the panel.
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        thePanel.repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}

public class MyPanel extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // Other painting stuff
    }
}

下一步:Now i have to update the frame from another class. I could not find a way to update the GUI(the frame) from another class.

简单.由于需要更新JPanel,因此将以下方法添加到MyWindow类:

Simple. Since the JPanel is what needs to be updated, add the following method to the MyWindow class:

public void repaintWindow() {
    thePanel.repaint();
}

并在需要更新时将其添加到其中:

And add this to whenever you need to update it:

MyWindow theWindow = new MyWindow();
theWindow.repaintWindow();

下一步:all the answers here extended JPanel. So i could not find my answer.

很抱歉,但您需要一个小组.可以使用JFrames,但是如果您想开始做原始的和低级的事情,则需要通过学习阅读oracle教程和oracle文档来学习这些事情的工作方式.现在,以我向您展示的任何方式使用JPanels.

I apologize, but you NEED a panel. It is possible to do with JFrames, but if you want to start doing things raw and low-level, you need to learn how these things work by learning to read the oracle tutorials and the oracle documentation. For now, use JPanels in any ways I've shown you.

下一步:from another class I have to draw something on JFrame.Is that possible?

是的,的确如此!每当您想画点东西时:

Yes, indeed! Whenever you want to draw something:

MyWindow theWindow = new MyWindow();

Graphics g = theWindow.thePanel.getGraphics();
BufferedImage someRandomImage = SomeRandomClass.getRandomImage();
g.drawImage(someRandomImage, 200, 481, null);

theWindow.repaintWindow();

我真的希望能有所帮助,但是要使用Java编程,您需要使用它们提供的工具,尤其是涉及诸如Swing之类的高级内容时.到处都有关于这些东西的教程.在将来寻求具体帮助之前,请先阅读它们.

I really hope I've helped but to program in java you need to use the tools they give you, especially when it comes to high level things like Swing. There are tutorials everywhere for this stuff. Please read them before asking for specific help in the future.

这篇关于从另一个类JFrame调用重画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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