[英]KeychainStore doesn't provide certificates to Java on 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.