java mvc pattern과 swing/gui를 이용하는데 어떻게 해야할지를 모르겠어요
조회수 797회
java8을 이용해서 이런 application이 뜨게 만들어야 하는데요, mvc pattern을 이용해서 코딩을 하는게 처음이라 어떻게 해야할지 감이 잘 안잡히네요. slider이랑 button을 넣는건 간단한데, 저 중간에 들어가는 icon을 어떻게 해야 변형을 할 수 있는지, 그리고 처음에 창이 열렸을때 저렇게 yellow triangle icon을 나오게 세팅할 수 있는지가 궁금합니다. new Icon object를 instantiate/create해야하는건지 아니면 new rectangle object를 create해야하는건지도 잘 모르겠어요. Rectangle, Triangle, Circle은 각자 class가 따로 있고 다 첨부하면 너무 길어져서 Rectangle.java만 첨부했습니다. 그리고 view class도 첨부했어요. setcolor()은 model class에 있습니다.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class View {
private JFrame frame;
private JPanel panel1;
private JPanel panel2;
private JSlider slider;
private static final int slider_MIN = 20;
private static final int slider_MAX = 150;
private static final int slider_init = 20;
private JButton rectangleButton = new JButton("rectangle");
private JButton TriangleButton = new JButton("triangle");
private JButton CircleButton = new JButton("circle");
private JButton RedButton = new JButton("red");
private JButton BlueButton = new JButton("blue");
private JButton YellowButton = new JButton("yellow");
public View() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.getContentPane().setLayout(new BorderLayout());
panel1 = new JPanel();//default is flowlayout so no setLayout
panel1.add(rectangleButton);
rectangleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Rectangle r = new Rectangle(100);//create a new rectangle icon
}
});
panel1.add(TriangleButton);
panel1.add(CircleButton);
frame.add(panel1, BorderLayout.NORTH);
panel2 = new JPanel();
panel2.add(RedButton);
RedButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.RED);//color for the image in the middle
}
});
panel2.add(BlueButton);
BlueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.BLUE);
}
});
panel2.add(YellowButton);
YellowButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setColor(Color.YELLOW);
}
});
frame.add(panel2, BorderLayout.SOUTH);
slider = new JSlider(JSlider.VERTICAL, slider_MIN, slider_MAX, slider_init);
//slider.addChangeListener(this);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if(!source.getValueIsAdjusting()) {
int fps = (int)source.getValue();
setSize(fps);//setSize for the image
}
}
});
frame.add(slider, BorderLayout.WEST)
}
}
public class Rectangle implements Icon {
private int width;
private int height;
public Rectangle(int w) {
this.width = w;
this.height = w;
}
public int getIconWidth() {
return width;
}
public int getIconHeight() {
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g;
Rectangle2D.Double rectangle = new Rectangle2D.Double(x, y, width, height);
g2.setColor(Color.BLUE);
g2.fill(rectangle);
}
public static void main(String[] args) {
Rectangle icon = new Rectangle(20);
}
}
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력