我试图从另一个类调用重绘。但这行不通。我必须画一个框架。
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()不管用。为什么?
paint()
d.repaint()
看一下这个页面,看看第一个答案。这是一个与您的问题类似的,甚至不是确切的问题。
JFrame的paint()方法已被弃用。编译器或您的IDE应该有点抱怨,特别是如果您将@Override标记直接放置在方法上方(使用它来测试是否可以重写此方法,也就是您想做的事情)。
@Override
这意味着不鼓励使用它,并且某些功能可能已删除。使用时javax.swing,您将需要全面了解JPanels和了解系统JComponents。要在屏幕上绘制内容,您需要添加一个JPanel随该add(Component c)方法扩展的自定义类。然后,重写paintComponent(Graphics g)该类中的方法。确保该方法的第一行是super.paintComponent(g);这样,以便窗口可以刷新自身。
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调用。
repaint()
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 tried that with JPanel but i could not do that.
下一步:I prefer to do it with JFrame.如果希望在JFrame中处理鼠标,请执行以下操作:使JFrame成为侦听器,但JPanel是鼠标数据的来源。如下:
I prefer to do it with JFrame.
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.
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.
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?
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。到处都有关于这些东西的教程。在将来寻求具体帮助之前,请先阅读它们。
Swing