62,627
社区成员
发帖
与我相关
我的任务
分享
public class Circle {
private int x,y;
public Circle(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Jcomponent extends JComponent {
List<Circle> list;
public List<Circle> getList() {
return list;
}
public void setList(List<Circle> list) {
this.list = list;
}
public Jcomponent() {
super();
// TODO Auto-generated constructor stub
setSize(600, 500);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
g.setColor(Color.red);
if(list!=null) {
for (Circle circle : list) {
g.drawOval(circle.getX(), circle.getY(), 100, 100);
}
}
}
}
public class Jpanel extends JPanel {
public Jpanel() {
super();
// TODO Auto-generated constructor stub
setLayout(null);
setSize(600, 500);
setBackground(Color.yellow);
}
}
public class Jframe extends JFrame {
static Jpanel jp=new Jpanel();
static Jcomponent jc=new Jcomponent();
public Jframe() {
// TODO Auto-generated constructor stub
setTitle("Gui");
setSize(600,500);
setLayout(null);
setBackground(Color.yellow);
jp.add(jc);
getContentPane().add(jp);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String arg0[]) {
List<Circle> list=new ArrayList<Circle>();
new Jframe();
Scanner sc=new Scanner(System.in);
System.out.println("请输入圆心坐标x和y以逗号分开,直接按回车结束,注:(50<x<550,50<y<450)");
String str=sc.nextLine();
while(str!="") {
String[] s=str.split(",");
int x=Integer.valueOf(s[0]);
int y=Integer.valueOf(s[1]);
Circle c=new Circle(x-50, y-50);
list.add(c);
jc.setList(list);
jc.repaint();
System.out.println("请输入圆心坐标x和y以逗号分开,直接按回车结束,注:(50<x<550,50<y<450)");
str=sc.nextLine();
}
System.exit(0);
}
}