0
저는 재미있는 것처럼 새로운 자바로, 패널에 그릴려고합니다. 나는 1 개의 패널을 가지고 있으며 배경을 색칠 했으므로 어디에 있는지 볼 수 있습니다. 내가 동그라미, 타원 등을 그려하려고하지만 그 결과는 패널이 자바 패널에 나타나지 않는다는 것입니다.자바 패널의 2D 개체 그리기
나는 어떤 도움을 위해 greatful 일 것입니다!
그래서 내 코드는 여기에 표시됩니다 :import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class app extends JFrame implements ActionListener{
boolean rectangle1=false;
boolean circle1=false;
boolean polygon1=false;
boolean ellipse1=false;
String x,y;
JTextField input1;
JTextField input2;
JButton rectangle;
JButton circle;
JButton polygon;
JButton ellipse;
JLabel label1,label2,label3;
Mypanel mypanel;
public app(){
setSize(1000,1000);
setLayout(new FlowLayout());
setVisible(true);
JPanel mypanel =new JPanel();
mypanel.setPreferredSize(new Dimension(600,600));
mypanel.setBackground(Color.blue);
rectangle=new JButton("rectangle");
circle=new JButton("circle");
polygon=new JButton("polygon");
ellipse=new JButton("ellipse");
rectangle.addActionListener(this);
circle.addActionListener(this);
polygon.addActionListener(this);
ellipse.addActionListener(this);
JLabel label1=new JLabel("my paint application");
JLabel label2=new JLabel("select line color");
JLabel label3=new JLabel("select fill color");
input1=new JTextField(4);
input2=new JTextField(4);
add(label1);
add(rectangle);
add(circle);
add(ellipse);
add(polygon);
add(label2);
add(input1);
add(label3);
add(input2);
add(mypanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
x=input1.getText();
y=input2.getText();
if(e.getSource()==rectangle)
{
rectangle1= true;
repaint();
}
if(e.getSource()==circle)
{
circle1= true;
repaint();
}
if(e.getSource()==polygon)
{
polygon1= true;
repaint();
}
if(e.getSource()==ellipse)
{
ellipse1= true;
repaint();
}
}
public static void main(String args[]){
new app();
}
class Mypanel extends JPanel{
public void paint(Graphics g){
if(rectangle1==true){
rectangle1=false;
g.setColor(Color.red);
g.drawRect(10, 10,20,20);
g.setColor(Color.black);
g.fillRect(11, 11,20,20);
}
if(circle1==true){
circle1=false;
g.setColor(Color.red);
g.drawOval(250, 20, 40,40);
g.setColor(Color.black);
g.fillOval(249, 19,40,40);
}
if(polygon1==true){
polygon1=false;
int xpoints[]={10,20,170,10};
int ypoints[]={20,40,140,20};
g.setColor(Color.red);
g.fillPolygon(xpoints,ypoints,4);
}
if(ellipse1==true){
ellipse1=false;
g.setColor(Color.red);
g.drawOval(250, 20, 40,40);
g.setColor(Color.black);
g.fillOval(249, 19,40,40);
}
}
}
}