从另一个类JFrame调用重画

如何解决从另一个类JFrame调用重画

看一下这个页面,看看第一个答案。这是一个与您的问题类似的,甚至不是确切的问题。

JFrame的paint()方法已被弃用。编译器或您的IDE应该有点抱怨,特别是如果您将@Override标记直接放置在方法上方(使用它来测试是否可以重写此方法,也就是您想做的事情)。

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

为了完整性:

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

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

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

编辑:

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

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

现在,对于您的问题:

i have to use MouseMotionListener.

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

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。

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

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类中添加以下方法:

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

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

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

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

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

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

确实是的!每当您想画点东西时:

MyWindow theWindow = new MyWindow();

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

theWindow.repaintWindow();

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

解决方法

我试图从另一个类调用重绘。但这行不通。我必须画一个框架。

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()不管用。为什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?