문제는 제가 마우스를 클릭하고 드래그 한 다음 놓아 줘야 만 circle을 그리는 paintComponent를 얻으려는 것입니다. 그러나 내 paintPanel 클래스 내에서 내가 만든 객체를 초기화해야합니다 (예 : movedCircle myCircle = new movedCircle (0,0,0,0);) 그냥 객체를 만들면 movedCircle myCircle; 실제로 값으로 객체를 완전히 초기화 할 때까지 오류가 발생합니다.paintComponent로 java/Swing 문제를 해결했습니다.
내가 찾고있는 것 : 이 문제에 대한 가장 좋은 방법은 무엇입니까? 나는 그것이 필요하기 전에 불필요한 것을 그리기를 원하지 않는다.
나는 그것을 고치는 법을 알고 있습니다 : boolean 값을 paintComponent 안에 넣으십시오. 그렇게하면 실제로 거기에 서기까지 그리지 않습니다.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class drawCircle extends JFrame{
private JPanel myPanel = new paintPanel();
public drawCircle(){
add(myPanel);
}
private class paintPanel extends JPanel{
private int x1, y1, x2, y2;
movedText myText = new movedText(0,0,0,0);
movedCircle myCircle = new movedCircle(0,0,0,0);
public paintPanel(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
myCircle = new movedCircle(x1, y1, 0, 0);
repaint();
}
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
myCircle = new movedCircle(x1, y1, x2, y2);
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
myText = new movedText(x1, y1, x2, y2);
myCircle = new movedCircle(x1, y1, x2, y2);
repaint();
}
public void mouseMoved(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
x2 = 0;
y2 = 0;
myText = new movedText(x1, y1, x2, y2);
repaint();
}
});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
//draw oval after mouse released
myText.paintText(g);
myCircle.paintCircle(g);
}
}
class movedCircle{
private int x1, y1, x2, y2;
public movedCircle(int x1, int y1, int x2, int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void paintCircle(Graphics g){
g.drawOval(x1, y1, x2 - x1, y2 - y1);
}
}
class movedText{
private int x1, y1, x2, y2;
public movedText(int x1, int y1, int x2, int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void paintText(Graphics g){
g.drawString("x1: "+x1+" y1: "+y1+" x2: "+x2+" y2: "+y2, x1, y1);
}
}
class RedSquare{
private int xPos = 50;
private int yPos = 50;
private int width = 20;
private int height = 20;
public void setX(int xPos){
this.xPos = xPos;
}
public int getX(){
return xPos;
}
public void setY(int yPos){
this.yPos = yPos;
}
public int getY(){
return yPos;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public void paintSquare(Graphics g){
g.setColor(Color.RED);
g.fillRect(xPos,yPos,width,height);
g.setColor(Color.BLACK);
g.drawRect(xPos,yPos,width,height);
}
}
public static void main(String[] args){
JFrame frame = new drawCircle();
frame.setTitle("Is in ellipse? Demo");
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
마지막으로 제공 한 해결책은 입니다. movedCircle myCircle과 다르지 않습니다. movedCircle로 설정된 경우에도 myCircle = new nullMovedCircle(); 문제는 paintComponent가 항상이 라인 "myCircle.paintCircle (g);"로 인해 거기에없는 것을 그려야한다는 것입니다. 이상하게도 이상하게도 프로그램을 실행할 수 있지만 원하는 원을 그리기 위해 필요한 마우스 이벤트를 완성 할 때까지 아무 것도 그리지 않는 오류가 발생합니다. 이것은 실제로 bool 값 검사를 배치하고 중첩 된 movedCircle 클래스에서 bool 값을 반환하는 getDraw() 메서드를 만드는 계획입니다. – mjrevel
글쎄, 나는 틀렸다. .. 그리고 기쁜. 나는 리터럴에 내 paintComponent를 if 문과 함께 쓰고 싶지 않았습니다 ... 그리고 그들은 실제로 거기에 속한 것 같지 않았습니다. 여기에는 수정 같습니다 \t 클래스 nullMovedCircle가 movedCircle 연장 { \t \t 공개 nullMovedCircle (INT의 X1, INT Y1, INT X2, INT Y2) { \t \t \t 슈퍼 (X1, Y1, X2, Y2); \t \t} \t \t 공공 무효 paintCircle (그래픽 g) {} \t} 한마디로 : 감사합니다! – mjrevel
당신은 그렇게 할 수 있습니다.하지만 파직 (pajton)은 개발, 테스트 및 유지 보수를위한 과잉 클래스라고 암시합니다.모범 사례는 코드를 읽을 수 있도록 노력해야하며 if! null은 의도를 훨씬 더 많이 전달한다고 주장합니다. – vickirk