简体   繁体   English

paint方法没有被调用

[英]paint method is not being called

I don't know why, but I can't call the method paint(Graphics g) from code. 我不知道为什么,但我不能从代码中调用方法paint(Graphics g) I have MouseListeners : 我有MouseListeners

    @Override
    public void mouseReleased(MouseEvent e) {       
        pointstart = null;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        pointstart = e.getPoint();
    }

and MouseMotionListeners : MouseMotionListeners

    @Override
    public void mouseMoved(MouseEvent e) {
        pointend = e.getPoint();
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        pointend = arg0.getPoint();
        // ==========================================================================
        //          call repaint:

        repaint();


        // ==========================================================================
    }

and my paint method: 和我的paint方法:

public void paint(Graphics g){
    super.paint(g); //here I have my breakpoint in debugger
    g.translate(currentx, currenty);
    if(pointstart!=null){
        g.setClip(chartPanel.getBounds());
        g.setColor(Color.BLACK);
        g.drawLine(pointstart.x, pointstart.y, pointend.x, pointend.y);
    }
}

in the initialize method: 在初始化方法中:

currentx = chartPanel.getLocationOnScreen().x;
currenty = Math.abs(chartPanel.getLocationOnScreen().y - frame.getLocationOnScreen().y);

All in class, which is my work frame, it extends JFrame . 所有在课堂上,这是我的工作框架,它扩展了JFrame

Problem is, that program doesn't call paint method. 问题是,该程序不会call paint方法。 I checked that in debugger. 我在调试器中检查过。 I tried do this by paintComponent , without super.paint(g) . 我试过paintComponent ,没有super.paint(g) And the best is that, that code I copied from my other project, where it works fine. 最好的是,我从我的其他项目复制的代码,它工作得很好。

UPDATE: This is code which I want to draw line on panel (no matter - panel/chartpanel/etc). 更新:这是我想在面板上绘制线条的代码(无论是面板/图表面板/等)。 And it doesn't paint. 而且它不会画画。 I tried do this by paint(Graphics g){super.paint(g) ..... } and it doesn't too. 我尝试通过paint(Graphics g){super.paint(g).....}来做到这一点,但它也没有。 Painting should be aneble after clicking button "line". 单击“线”按钮后,绘画应该是有效的。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class Window extends JFrame {
private JFrame frame;
public JPanel panelbuttons;
public JPanel panelscrollpane;
public JButton btnLine;
public JToolBar toolBar;
public JScrollPane scrollPane;
public JPanel panel;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Window window = new Window();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Window() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 644, 430);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();

    panelbuttons = new JPanel();
    frame.getContentPane().add(panelbuttons, BorderLayout.WEST);
    panelbuttons.setLayout(new BorderLayout(0, 0));

    toolBar = new JToolBar();
    toolBar.setOrientation(SwingConstants.VERTICAL);
    panelbuttons.add(toolBar, BorderLayout.WEST);

    btnLine = new JButton("line");
    btnLine.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            panel.addMouseListener(mouselistenerLine);
            panel.addMouseMotionListener(mousemotionlistenerLine);
        }
    });
    toolBar.add(btnLine);

    panelscrollpane = new JPanel();
    frame.getContentPane().add(panelscrollpane, BorderLayout.CENTER);
    panelscrollpane.setLayout(new BorderLayout(0, 0));



    scrollPane = new JScrollPane(panel);
    panel.setLayout(new BorderLayout(0, 0));
    scrollPane.setViewportBorder(null);
    panelscrollpane.add(scrollPane);
    panelscrollpane.revalidate();
}

    Point pointstart = null;
    Point pointend = null;

private MouseListener mouselistenerLine = new MouseListener() {


        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println("I'm in first listener. - mouse Released");
            pointstart = null;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println("I'm in first listener. - mousePressed");
            pointstart = e.getPoint();
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

    };

    private MouseMotionListener mousemotionlistenerLine = new MouseMotionListener() {

            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("I'm in second listener. - mouseMoved");
                pointend = e.getPoint();
            }

            @Override
            public void mouseDragged(MouseEvent arg0) {
                System.out.println("I'm in second listener. - mouseDragged");
                pointend = arg0.getPoint();
                repaint();
            }

    };

    public void paintComponent(Graphics g){

        System.out.println("I'm in paintComponent method.");

        if(pointstart!=null){
            System.out.println("I'm drawing.");
            g.setClip(scrollPane.getBounds());
            g.setColor(Color.BLACK);
            g.drawLine(pointstart.x, pointstart.y, pointend.x, pointend.y);

        }
    }
}

You are creating two separate instances of JFrame and showing only one. 您正在创建两个单独的JFrame实例,并且只显示一个。
One instance is created because Window class extends JFrame , and the second is created inside initialize method. 创建一个实例是因为Window类扩展了JFrame ,第二个是在initialize方法中创建的。

To fix this without a lot of changes in code do this: 要在没有很多代码更改的情况下修复此问题,请执

private void initialize() {
    frame = this;
    frame.setBounds(100, 100, 644, 430);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BTW. BTW。 change paintComponent(Graphic g) to paint(Graphic g) because JFrame is not a JComponent . paintComponent(Graphic g)更改为paint(Graphic g)因为JFrame不是JComponent

在一个ChartPanel上下文中,考虑的一个org.jfree.chart.annotationsXYLineAnnotation所示, 这里在这里

Calling repaint() will trigger your paint(Graphics g) method to be called. 调用repaint()将触发调用paint(Graphics g)方法。 So, you don't have to explicitly call paint() . 因此,您不必显式调用paint()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM