Question

J'ai essayé de déboguer pendant des heures. Le programme est censé être un grapheur que les graphiques coordonnées, mais je ne peux pas obtenir quoi que ce soit pour afficher même pas une ligne au hasard, mais si je mets une déclaration d'impression là-bas cela fonctionne. Il y a un problème avec la méthode paintComponent. Quand je sur la déclaration d'impression avant g.drawLine puis il imprime, mais elle ne tire pas toutes les lignes, même si je mets une ligne au hasard avec les coordonnées (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();
    }
}

Voici comment je teste, il est censé être un carré, mais rien ne montre.

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

Coordonner la classe donc si vous voulez courir et essayer. C'est tout ce que vous avez besoin.

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;
    }
}
Était-ce utile?

La solution

Les méthodes paintGraph (...), paintXAxis (...) et paintYAxis (...) ne devrait pas lancer paintComponent ().

Au lieu de cela le code devrait être l'inverse. C'est que vous devez remplacer la méthode paintComponent () qui à son tour invoquer ces 3 méthodes. De plus, les paramètres que vous passez à chacune des méthodes devraient être des propriétés de la classe de peinture. Donc, vous devrez peut-être ajouter une méthode setter à attribuer des valeurs à toutes les propriétés.

En outre, ne pas oublier de remplacer la méthode getPreferredSize () de la classe de peinture de sorte que le composant peut être layed correctement par les gestionnaires de mise en page.

Je vous suggère de commencer par la lecture de la section du tutoriel Swing sur personnalisée Peinture pour une explication plus détaillée et des exemples de travail.

Autres conseils

La classe ne override paintComponent(Graphics).


Il y a quelques autres aspects bizarres du code indiqués, en commençant par ..

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

Qu'est-ce que traduit dans une langue humaine, le «centre de l'écran? Si oui, cela peut être accompli en utilisant plus simple:

frame.setLocationRelativeTo(null);

Mais mieux encore, nous avons maintenant ..

frame.setLocationByPlatform(true);

Voir pour un exemple rapide.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top