drawLine JAVA不会停留在MAC OS X上

时间:2015-04-16 11:03:47

标签: java macos swing graphics

首先这个问题仅对 os x有效我不知道为什么,如果有人能告诉我......(他在linux和windows上工作正常)

当我点击画画时,旧点消失而不是停留。 如果删除 super.paintComponent 上的注释,结果在osx上是相同的,但在窗口和linux上这是不同的。

点击绘制。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Stack extends JPanel {
    JFrame jf;
    Panneau jp;
    Point p;

    public Stack() {
        p = new Point();
        jf = new JFrame("Window");
        jp = new Panneau();
        jp.setBackground(Color.white);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(800,600);
        jf.setLayout(new GridLayout());
        jf.add(jp);
        jf.setVisible(true);

        jp.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
               setPoint(p);
               jp.repaint();
            }
        });
    }

    public void setPoint(Point p) {
        this.p.x += 10;
        this.p.y += 10;
    }

    public class Panneau extends JPanel {
        public void paintComponent(Graphics g) {
            // super.paintComponent(g);
            g.drawLine(p.x, p.y, p.x+5, p.y+5);
        }
    }

    public static void main (String []args) {
        Stack s = new Stack();
    }
}

1 个答案:

答案 0 :(得分:2)

这是因为您没有重新绘制先前点所在的区域,显然在Mac上,它会因某种原因重绘该区域?

但关键是,你不应该依靠通过不绘制前一个区域来绘制更多的点,并且你应该总是调用super.paintComponent(g)。我建议你创建一个点列表,然后在重新绘制时绘制所有这些点。

我自己创造了这个代码,我希望你能理解我在这里做的事情:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Stack extends JPanel {
    JFrame jf;
    Panneau jp;
    //List of points instead of one point
    List<Point> points;

    public Stack() {
        //Instantiating the list
        points = new ArrayList<Point>();
        jf = new JFrame("Window");
        jp = new Panneau();
        jp.setBackground(Color.white);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(800,600);
        jf.setLayout(new GridLayout());
        jf.add(jp);
        jf.setVisible(true);

        jp.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
                //Adding a new point to the list
                addPoint();
                jp.repaint();
            }
        });
    }

    public void addPoint() {
        if(points.isEmpty()){
            //If this is the first point to be added, set it to 0,0
            points.add(new Point(0, 0));
        }else{
            //Get the last point currently in the list
            Point lastPoint = points.get(points.size()-1);
            //Create the newpoint, 10px right and 10px down from the current point
            Point newPoint = new Point(lastPoint.x + 10, lastPoint.y + 10);
            //Add the new point to the list
            points.add(newPoint);
        }
    }

    public class Panneau extends JPanel {
        public void paintComponent(Graphics g) {
            //Make sure the background is drawn! Should always be called
            super.paintComponent(g);
            //Iterate over all the points and draw them all
            for(Point p : points){
                g.drawLine(p.x, p.y, p.x + 5, p.y + 5);
            }
        }
    }

    public static void main (String []args) {
        Stack s = new Stack();
    }
}