NetBeans 8.0에서 나는 그림판 프로그램을 만들었고 다른 파일 인 인터페이스 파일에 영향을주기 위해 주 파일의 코드가 필요했습니다. 어떻게해야합니까? 내 코드 : 나는 색상을 변경 한 인터페이스로 메시지를 전송해야 할코드가있는 다른 파일에 어떻게 영향을 줍니까?
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class PaintInterface extends JPanel implements MouseListener, MouseMotionListener {
static int x = 0, y = 0;
@Override
public void paint(Graphics g) {
this.setBackground(Color.white);
this.addMouseMotionListener(this);
this.addMouseListener(this);
g.setColor(Color.black);
g.fillRect(x, y, 3, 3);
}
@Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
:
package paintapp;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
public class PaintApp extends javax.swing.JFrame {
int colourR, colourG, colourB;
static String Ccolour = "BLACK";
public static void main(String[] args) {
JFrame main = new JFrame("Tessellating Pi - Paint");
PaintInterface pi = new PaintInterface();
main.add(pi);
main.setSize(1000, 1000);
main.setVisible(true);
JFrame j = new JFrame("Colour Chooser");
JButton c = new JButton("Change Colour");
j.add(c);
j.setSize(150, 100);
j.setVisible(true);
c.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("BLACK".equals(Ccolour)) {
Ccolour = "RED";
//code to change code in interface to set colour to red
}
}
}
);
}
}
이 인터페이스입니다. 어떻게해야합니까? 이 작업을 수행 할 대체 방법이 있습니까?
'..Jendan .. @Override .. public void paint (Graphics g) {'Swing 컴포넌트의 경우,'paint'보다는'paintComponent'를 오버라이드 (override)합니다. 둘 중 하나를 오버라이드 할 때, BG와 테두리를 페인트하기 위해 super 메서드를 호출합니다. –