문제
마우스를 클릭한 위치에서 선물이 아래로 떨어지는 문제입니다 이를 해결하는 다음 프로그램에 대해 빈칸을 채우세요
코드 빈칸 채우기
import javax.swing.*;
public class PresentDropGUIMain
{
public static void main(String[] args)
{
final String background = "C:\\Users\\user\\Downloads\\JAVA-main\\Chapter13\\image\\background.jpg";
frame = new ( "선물 배달" );
frame.getContentPane().add( new PresentDropPanel( background ) );
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation( .EXIT_ON_CLOSE );
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.;
public class PresentDropPanel {
protected <Present> presents;
protected Image background;
public PresentDropPanel( String background ) {
this.presents = new <Present>();
this.background = new ( background ).();
setPreferredSize( new Dimension( 400, 400 ) );
setFocusable( true );
requestFocus();
this.( new () {
@Override
public void ( event ) {
presents.add( new Present( event.(), event.() ) );
}
} );
timer = new ( 50, new () {
@Override
public void ( event ) {
for( Present present : presents )
present.drop();
();
}
} );
timer.();
}
@Override
public void ( Graphics g ) {
super.paintComponent( g );
g.( background, 0, 0, null );
for( Present present : presents )
present.(g);
}
}
import java.awt.*;
public class Present
{
int x, y;
public Present( int x, int y ) {
this.x = x;
this.y = y;
}
public void drop() {
this.y += 10;
}
public void ( Graphics g ) {
g.setColor( Color.red );
g.fillRect( x, y, 10, 10 );
g.fillRect( x+20, y, 10, 10 );
g.fillRect( x, y+20, 10, 10 );
g.fillRect( x+20, y+20, 10, 10 );
g.setColor( new Color( 0, 100, 0 ) );
g.fillRect( x+10, y, 10, 30 );
g.fillRect( x, y+10, 30, 10 );
}
}