paintComponent方法未在面板上显示任何内容

时间:2011-12-01 01:23:23

标签: java swing jpanel paintcomponent

我一直试图调试这几个小时。该程序应该是一个图形坐标的图形,但我不能得到任何东西甚至不是随机线,但如果我把一个打印声明放在那里它的工作原理。这是paintComponent方法的一个问题。当我在g.drawLine之前打印语句然后打印,但即使我放置了一个带坐标(1,3),(2,4)的随机行,它也不会绘制任何行。

import java.awt.*;
import java.util.*;
import javax.swing.*;
public abstract class XYGrapher
{
    abstract public Coordinate xyStart();
    abstract public double xRange();
    abstract public double yRange();
    abstract public Coordinate getPoint(int pointNum);
    public class Paint extends JPanel
    {
        public void paintGraph(Graphics g, int xPixel1, int yPixel1, int xPixel2, int yPixel2) 
        {
            super.paintComponent(g);
            g.setColor(Color.black);
            g.drawLine(xPixel1, yPixel1, xPixel2, yPixel2);
        }
        public void paintXAxis(Graphics g, int xPixel, int pixelsWide, int pixelsHigh) 
        {
            super.paintComponent(g);
            g.setColor(Color.green);
            g.drawLine(xPixel, 0, xPixel, pixelsHigh);
        }
        public void paintYAxis(Graphics g, int yPixel, int pixelsWide, int pixelsHigh)
        {
            super.paintComponent(g);
            g.setColor(Color.green);
            g.drawLine(0, yPixel, pixelsWide, yPixel);
        }
    }
    public void drawGraph(int xPixelStart, int yPixelStart, int pixelsWide, int pixelsHigh)
    {
        JFrame frame = new JFrame();
        Paint panel = new Paint();
        panel.setPreferredSize(new Dimension(pixelsWide, pixelsHigh));
        panel.setMinimumSize(new Dimension(pixelsWide, pixelsHigh));
        panel.setMaximumSize(new Dimension(pixelsWide, pixelsHigh));
        frame.setLocation(frame.getToolkit().getScreenSize().width / 2 - pixelsWide / 2, frame.getToolkit().getScreenSize().height / 2 - pixelsHigh / 2);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

        double xRange = xRange();
        double yRange = yRange();
        Coordinate xyStart = xyStart();

        int xPixel = xPixelStart - (int) (xyStart.getX() * (pixelsWide / xRange));
        int yPixel = yPixelStart + (int) ((xyStart.getY() + yRange) * (pixelsHigh / yRange));

        System.out.println(xPixel + " " + yPixel);

        if(yPixel > 0 && (yPixel < pixelsHigh))
        {
            System.out.println("y");
            panel.paintYAxis(panel.getGraphics(), yPixel, pixelsWide, pixelsHigh);
        }
        if(xPixel > 0 && (xPixel < pixelsHigh))
        {
            System.out.println("x");
            panel.paintXAxis(panel.getGraphics(), xPixel, pixelsWide, pixelsHigh);
        }

        for(int i = 0; i>=0; i++)
        {
            Coordinate point1 = getPoint(i);
            Coordinate point2 = getPoint(i+1);
            if(point2 == null)
            {
                break;
            }
            else
            {
                if(point1.drawFrom() && point2.drawTo())
                {
                    int xPixel1 = (int) (xPixelStart + (point1.getX() - xyStart.getX()) * (pixelsWide / xRange));
                    int yPixel1 = (int) (yPixelStart + (xyStart.getY() + yRange-point1.getY()) * (pixelsHigh / yRange));
                    int xPixel2 = (int) (xPixelStart + (point2.getX() - xyStart.getX()) * (pixelsWide / xRange));
                    int yPixel2 = (int) (yPixelStart + (xyStart.getY() + yRange - point2.getY()) * (pixelsHigh / yRange));

                    panel.paintGraph(panel.getGraphics(), xPixel1, yPixel1, xPixel2, yPixel2);
                }
            }
        }
        frame.pack();
    }
}

这就是我测试的方式,它应该是一个正方形,但没有任何显示。

public class GrapherTester extends XYGrapher
{
    public Coordinate xyStart()
    {
        return new Coordinate(-2,2);
    }
    public double xRange()
    {
        return 4;
    }
    public double yRange()
    {
        return 4;
    }
    public Coordinate getPoint(int pointNum)
    {
        switch(pointNum)
        {
            case 0: return new Coordinate(-1,-1);
            case 1: return new Coordinate(1,-1);
            case 2: return new Coordinate(1,1);
            case 3: return new Coordinate(-1,1);
            case 4: return new Coordinate(-1,-1); 
        }
        return null;
    }
    public static void main(String[] args)
    {
        new GrapherTester().drawGraph(100, 100, 500, 500);
    }
}

协调课程,以便任何人想要运行并尝试一下。这就是你所需要的一切。

public class Coordinate
{
    float x;
    float y;
    boolean drawTo;
    boolean drawFrom;
    Coordinate(double x, double y) 
    {
        this.x = (float) x;
        this.y = (float) y;
        drawFrom = true;
        drawTo = true;
    }
    Coordinate(double x, double y, boolean drawFrom, boolean drawTo) 
    {
        this.x = (float) x;
        this.y = (float) y;
        this.drawFrom = drawFrom;
        this.drawTo = drawTo;
    }
    public double getX()
    {
        return x;
    }
    public double getY()
    {
        return y;
    }
    public boolean drawTo()
    {
        return drawTo;
    }
    public boolean drawFrom()
    {
        return drawFrom;
    }
}

2 个答案:

答案 0 :(得分:5)

方法paintGraph(...),paintXAxis(...)和paintYAxis(...)不应该调用paintComponent()。

相反,代码应该是另一种方式。那就是你应该覆盖paintComponent()方法,而后者又将调用这3个方法。您传递给每个方法的参数也应该是Paint类的属性。因此,您可能还需要添加一些setter方法来为所有属性赋值。

另外,不要忘记覆盖Paint类的getPreferredSize()方法,以便布局管理器可以正确布局组件。

我建议您首先阅读Custom Painting上Swing教程中的部分,以获取更详细的说明和工作示例。

答案 1 :(得分:3)

该课程不会覆盖 paintComponent(Graphics)


显示的代码有一些其他奇怪的方面,从..

开始
frame.setLocation(frame.getToolkit().getScreenSize().width / 2 - pixelsWide / 2, frame.getToolkit().getScreenSize().height / 2 - pixelsHigh / 2);

在任何人类语言中,“屏幕中心”都会转化为什么?如果是这样,可以使用以下方法简化:

frame.setLocationRelativeTo(null);

但更好的是,我们现在......

frame.setLocationByPlatform(true);

请参阅here以获取快速示例。