다음은 폭탄을 표시하는 데 사용할 수있는 몇 가지 버튼과 마우스 수신기는 당신이
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Dialog extends JFrame {
JPanel panSouth;
JPanel panel;
JButton but;
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;
private int colorIndex = 0;
Color color = Color.BLACK;
public Dialog() {
super("Graphics by Java: the cells drawing");
setSize(700, 700);
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
e.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
panSouth = new JPanel() {
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(color);
g2d.setStroke(new BasicStroke(Integer.parseInt(tf4.getText())));
int bx = panSouth.getWidth();
int by = panSouth.getHeight();
int nx = Integer.parseInt(tf1.getText());
int ny = Integer.parseInt(tf2.getText());
int dx = Math.round(bx/nx);
int dy = Math.round(by/ny);
double an = Integer.parseInt(tf3.getText());
g2d.rotate(Math.toRadians(an));
// OX
int fromX;
for (int i = -2*nx; i < nx * 2; i++) {
fromX = i * dx;
g2d.drawLine(fromX, -2*by, fromX, by*2);
}
// OX
int fromY;
for (int i = -2*ny; i < ny * 2; i++) {
fromY = i * dy;
g2d.drawLine(-2*bx, fromY, bx*2, fromY);
}
}
};
panel = new JPanel();
// panel.setBackground(Color.PINK);
// panSouth.setBackground(Color.CYAN);
tf1 = new JTextField(5);
tf1.setText("25");
tf2 = new JTextField(5);
tf2.setText("15");
tf3 = new JTextField(5);
tf3.setText("0");
tf4 = new JTextField(5);
tf4.setText("1");
but = new JButton("Change");
panel.add(new JLabel("T: "));
panel.add(tf4);
panel.add(new JLabel("X: "));
panel.add(tf1);
panel.add(new JLabel("Y: "));
panel.add(tf2);
panel.add(new JLabel(" D: "));
panel.add(tf3);
panel.add(but);
add(panel, BorderLayout.SOUTH);
add(panSouth, BorderLayout.CENTER);
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
addMouseListener(new MouseAdapter() {
// @Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
colorIndex = colorIndex + 1;
if (colorIndex > 2) {
colorIndex = 0;
}
switch (colorIndex) {
case 0:
color = Color.RED;
break;
case 1:
color = Color.BLUE;
break;
case 2:
color = Color.GREEN;
break;
default:
color = Color.RED;
break;
}
repaint();
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Dialog().setVisible(true);
}
});
}
}
2D
와 함께 작동하는 방법을 예입니다.
"그래픽을 메서드에 추가"하지 않지만 해당 메서드를 사용하는 사용자 인터페이스를 만듭니다. 시작하려면 JavaFX 또는 Swing 튜토리얼을 살펴 봐야합니다. – Thomas