2017-12-08 6 views
0

버튼이 작동하지만 페인트로 그릴 수 없습니다(). SwingDraw의 주요 메소드와 관련이 있다고 생각하지만 100 % 확실하지는 않습니다. 긴 코드에 대한 모든 도움을 주셔서 감사하고 죄송합니다 :/이 코드가 paint()를 트리거하지 않는 이유는 무엇입니까?

SwingDraw :

// Import Core Java packages 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.event.*; 
import javax.swing.*; 

public class SwingDraw extends JFrame implements ActionListener, ItemListener{ 

// Initial Frame size 
static final int WIDTH = 1500;    // frame width 
static final int HEIGHT = 1000;    // frame height 

// Color choices 
static final String[] COLOR_NAMES = new String[]{"None", "Red", "Blue", "Green"}; 
static final Color COLORS[] = {null, Color.red, Color.blue, Color.green }; 

// Button control 
JButton circle; 
JButton roundRec; 
JButton threeDRec; 
JButton line; 
JButton square; 
JButton oval; 

JButton clear; 
JButton delete; 

// List to keep track of shapes 
JList<String> shapesKeeper; 

// Color choice box 
JComboBox<String> colorChoice = new JComboBox<>(COLOR_NAMES); 

SwingDrawCanvas betterCanvas = new SwingDrawCanvas(); 

/** 
* Constructor 
*/ 
public SwingDraw() { 
    // Create a frame 
    setSize(WIDTH, HEIGHT); 
    setLocation(130, 100); 
    // add window closing listener 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    // create panel for controls 
    JPanel topPanel = new JPanel(); 
    JPanel leftList = new JPanel(); 



    // create button control 
    JPanel buttonPanel = new JPanel(); 
    topPanel.add(buttonPanel, BorderLayout.NORTH); 

    circle = new JButton("Circle"); 
    buttonPanel.add(circle); 
    roundRec = new JButton("Rounded Rectangle"); 
    buttonPanel.add(roundRec); 
    threeDRec = new JButton("3D Rectangle"); 
    buttonPanel.add(threeDRec); 
    line = new JButton("Line"); 
    buttonPanel.add(line); 
    square = new JButton("Square"); 
    buttonPanel.add(square); 
    oval = new JButton("Oval"); 
    buttonPanel.add(oval); 
    clear = new JButton("Clear"); 
    buttonPanel.add(clear); 

    JPanel listPanel = new JPanel(); 
    leftList.add(listPanel, BorderLayout.WEST); 
    JList<String> shapesKeeper = new JList<>(); 
    listPanel.add(shapesKeeper); 
    delete = new JButton("Delete Shape"); 
    listPanel.add(delete); 

    // add button listener 
    circle.addActionListener(this); 
    roundRec.addActionListener(this); 
    threeDRec.addActionListener(this); 
    line.addActionListener(this); 
    square.addActionListener(this); 
    oval.addActionListener(this); 
    clear.addActionListener(this); 

    // create panel for color choices 
    JPanel colorPanel = new JPanel(); 
    JLabel label = new JLabel("Filled Color:"); 
    topPanel.add(colorPanel); 
    colorPanel.add(label); 
    JComboBox<String> colorChoice = new JComboBox<>(COLOR_NAMES); 
    colorPanel.add(colorChoice); 

    colorChoice.addItemListener(this); 

    add(topPanel, BorderLayout.NORTH); 
    add(leftList, BorderLayout.WEST); 
    setVisible(true); 

} // end of constructor 


/** 
* Implementing ActionListener 
*/ 
public void actionPerformed(ActionEvent event) { 
    if(event.getSource() == circle) { // circle button 
     betterCanvas.setShape(SwingDrawCanvas.CIRCLE); 
    } 
    else if(event.getSource() == roundRec) { // rounded rectangle button 
     betterCanvas.setShape(SwingDrawCanvas.ROUNDED_RECTANGLE); 
    } 
    else if(event.getSource() == threeDRec) { // 3D rectangle button 
     betterCanvas.setShape(SwingDrawCanvas.RECTANGLE_3D); 
    } 
    else if(event.getSource() == clear){ 
     betterCanvas.setShape(SwingDrawCanvas.CLEAR); 
    } 
    else if(event.getSource() == line){ 
     betterCanvas.setShape(SwingDrawCanvas.LINE); 
    } 
    else if(event.getSource() == square){ 
     betterCanvas.setShape(SwingDrawCanvas.SQUARE); 
    } 
    else if(event.getSource() == oval){ 
     betterCanvas.setShape(SwingDrawCanvas.OVAL); 

    } 
} 

/** 
* Implementing ItemListener 
*/ 
public void itemStateChanged(ItemEvent event) { 
    Color color = COLORS[colorChoice.getSelectedIndex()]; 
    betterCanvas.setFilledColor(color); 
} 

/** 
* the main method 
*/ 
public static void main(String[] argv) { 
    new SwingDraw(); 
} 
} 

SwingDrawCanvas :

public class SwingDrawCanvas extends JPanel implements MouseListener, MouseMotionListener { 

// Constants for shapes 
public static final int CIRCLE = 1; 
public static final int ROUNDED_RECTANGLE = 2; 
public static final int RECTANGLE_3D = 3; 
public static final int LINE = 4; 
public static final int SQUARE = 5; 
public static final int OVAL = 6; 

public static final int CLEAR = 7; 

// Coordinates of points to draw 
private int x1, y1, x2, y2; 

// shape to draw 
private int shape = CIRCLE; 
/** 
* Method to set the shape 
*/ 
public void setShape(int thisShape) { 
    System.out.println("HEY"); 
    this.shape = thisShape; 
    System.out.println(shape); 
} 

// filled color 
private Color filledColor = null; 
/** 
* Method to set filled color 
*/ 
public void setFilledColor(Color color) { 
    filledColor = color; 
} 

/** 
* Constructor 
*/ 
public SwingDrawCanvas() { 
    addMouseListener(this); 
    addMouseMotionListener(this); 
} // end of constructor 

/** 
* painting the component 
*/ 
public void paint(Graphics g) { 

    System.out.println("ARHFHASJDASHDHAs"); 
    super.paint(g); 

    System.out.println("AHAHAHAHAHAHAHA"); 

    g.drawString("FUCK", 1, 1); 

    // the drawing area 
    int x, y, width, height; 

    // determine the upper-left corner of bounding rectangle 
    x = Math.min(x1, x2); 
    y = Math.min(y1, y2); 

    // determine the width and height of bounding rectangle 
    width = Math.abs(x1 - x2); 
    height = Math.abs(y1 - y2); 


    if(filledColor != null) 
     g.setColor(filledColor); 
    switch (shape) { 
     case ROUNDED_RECTANGLE : 
      if(filledColor == null) 
       g.drawRoundRect(x, y, width, height, width/4, height/4); 
      else 
       g.fillRoundRect(x, y, width, height, width/4, height/4); 
      break; 
     case CIRCLE : 
      int diameter = Math.max(width, height); 
      if(filledColor == null) 
       System.out.println("HRE BITCHS"); 
      else 
       System.out.println("HEY FUCK YOU GUY"); 
      break; 
     case RECTANGLE_3D : 
      if(filledColor == null) 
       g.draw3DRect(x, y, width, height, true); 
      else 
       g.fill3DRect(x, y, width, height, true); 
      break; 
    } 
} 

/** 
* Implementing MouseListener 
*/ 
public void mousePressed(MouseEvent event) { 
    x1 = event.getX(); 
    y1 = event.getY(); 
} 

public void mouseReleased(MouseEvent event) { 
    x2 = event.getX(); 
    y2 = event.getY(); 
    repaint(); 
} 

public void mouseClicked(MouseEvent e) {} 
public void mouseEntered(MouseEvent e) {} 
public void mouseExited(MouseEvent e) {} 

/** 
* Implementing MouseMotionListener 
*/ 
public void mouseDragged(MouseEvent event) { 
    x2 = event.getX(); 
    y2 = event.getY(); 
    repaint(); 
} 

public void mouseMoved(MouseEvent e) {} 

}

나는이에서 아주 좋은 아니에요, 나는 ' 당신들이 저에게 줄 수있는 도움을 찾고 있습니다. D

+0

SwingDraw()에서 프레임에'betterCanvas'를 추가해야합니다. –

+0

'SwingDrawCanvas' 패널이 프레임에 절대 추가되지 않습니다. – Berger

+0

그 외에도'void paint (Graphics g) {'를'@Override public void paintComponent (Graphics g) {'1)로 변경하십시오.'@Orride' 메소드는 메소드가 존재하는지 컴파일 타임에 검사합니다. 모든 메서드를 재정의 할 때마다이 메서드를 사용하는 것이 좋습니다. 2) 「paintComponent (..)」메소드를 확장 한 「JComponent」또는 클래스의 커스텀 페인트를 실시하는 올바른 방법. –

답변

0

paint() 대신에, 귀하의 기능은 paintComponent(Graphics g)이어야합니다. paintComponent(Graphics g) 함수를 만들 때 paintComponent(Graphics g)을 직접 호출 할 수 없기 때문에 java가 함수를 인식 할 수 있도록 @Override이 필요합니다.

또한 패널을 새로 고치려면 언제든지 repaint()으로 전화하거나 컴파일 된 코드를 따라 JFrame의 크기를 조정할 수 있습니다. 이 두 가지 방법 모두 paintComponent(Graphics g) 함수를 호출합니다.

관련 문제