繁体   English   中英

drawLine JAVA不会停留在MAC OS X上

[英]drawLine JAVA doesn't stay on 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();
    }
}

暂无
暂无

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

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