drawLine JAVA不会停留在MAC OS X上



首先,这个问题在OS X上有效,我不知道为什么,如果有人可以告诉我...(他在Linux和Windows上工作得很好)

当我点击绘制时,旧点消失而不是停留。如果你删除super.paintComponent上的注释,结果在osx上是相同的,但在window和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();
    }
}

发生这种情况是因为您没有重新绘制先前点所在的区域,显然在 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();
    }
}

相关内容

  • 没有找到相关文章

最新更新