从另一个类 JFrame 调用重绘

Calling repaint from another class JFrame(从另一个类 JFrame 调用重绘)

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

问题描述

我正在尝试从另一个类调用 repaint.但它不起作用.我必须在一个框架上画画.

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() 方法时,Panel 将收到一个 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,我还不太明白我需要从另一个类调用repaint"的上下文和麻烦......我会尽力的.

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.

现在,对于您的问题:

我必须使用 MouseMotionListener.

不完全...这是一种设置的好方法,但您可以运行线程(不断反复运行方法的东西)来检查鼠标坐标.当您进入游戏和其他杂项应用程序时,您会想要开始这样做.

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

下一步:我用 JPanel 尝试过,但我做不到.如果你阅读我编辑顶部的教程,你会看到他们轻松实现了 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.

下一步:我更喜欢使用 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
    }
}

下一步:现在我必须从另一个类更新框架.我找不到从另一个类更新 GUI(框架)的方法.

简单.由于 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();
}

并在需要更新时添加:

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

下一步:这里的所有答案都扩展了 JPanel.所以我找不到我的答案.

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

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.

下一步:从另一个类我必须在 JFrame 上画一些东西.这可能吗?

是的,确实!每当你想画东西时:

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 调用重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从另一个类 JFrame 调用重绘

基础教程推荐