简体   繁体   English

drawLine JAVA不会停留在MAC OS X上

[英]drawLine JAVA doesn't stay on MAC OS X

First this problem is effective on os x only i don't know why, if anyone can tell me...(he works fine on linux and windows) 首先,这个问题仅在os x上有效,我不知道为什么,如果有人能告诉我...(他在linux和Windows上都能正常工作)

When i'm clicking to draw, the old points disappear instead of stay. 当我单击绘制时,旧点消失而不是留下。 If you remove the commentary on the super.paintComponent , the result is same on osx but on window and linux this is different. 如果删除super.paintComponent上的注释 ,则在osx上的结果相同,但在window和linux上的结果不同。

Click to draw. 单击以绘制。

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

This happens because youre not re-drawing the areas where you previous points are located, apparently on mac, it does redraw that area for some reason? 发生这种情况是因为您没有重绘以前的点所在的区域,显然在Mac上,由于某些原因,它确实重绘了该区域吗?

But the point is, you shouldnt rely on drawing more points by not drawing over a previous area, and you should always call super.paintComponent(g). 但是要点是,您不应该依靠绘制更多的点而不是在先前的区域上绘制,而应该始终调用super.paintComponent(g)。 I recommend you create a list of points, and then draw all these points when you repaint. 我建议您创建一个点列表,然后在重新绘制时绘制所有这些点。

I've taken the liberty of creating this code myself, I hope you understand what im doing here: 我已经自由创建了自己的代码,希望您了解我在这里做什么:

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