도면 상쇄 그림 :사용자 정의 JPanel에 내가 응용 프로그램을 실행하려면 Windows 8.1에 넷빈즈 (JDK 1.7)를 사용하고
- 는 400x400 크기
JPanel
의paintComponent()
에 draw a frog에Graphics
객체를 사용에게; - 디스플레이 목적으로 패널을
JFrame
에 추가합니다.
이것은 매우 간단한 작업이어야하지만이 응용 프로그램의 특정 동작을 이해하는 데 문제가 있습니다.
여기에 문제가 있습니다. 창이 처음 표시 될 때 패널이 표시되지 않아야하는 오프셋 (아래 이미지)으로 표시됩니다. 내가 왜 이런 일을 이해하기 위해 노력하고있어이를 해결하는 방법 : 화살표 키 중 하나 후
도면이 왼쪽으로 5 개 픽셀을 난민 도착 키보드를 누르면/우/상/하위. 그러나 이것이 발생하면 JPanel
전체가 윈도우의 (0,0) 좌표로 이동 한 것 같습니다 (처음부터 예상 한 것입니다). 따라서 그림을 이동하는 것보다 훨씬 더 많이 이동해야합니다 :
나는 패널 가장자리에 검은 색 테두리를 칠해 문제가 더욱 분명 해지도록했습니다. 여기
는 Short, Self Contained, Correct (Compilable), Example입니다 :KAfrog.java :
import javax.swing.SwingUtilities;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class KAfrog
{
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
} // end of anonymous innerclass
); // end of invokeLater
}
private static void createAndShowGUI()
{
System.out.println("Created GUI on EDT? " + SwingUtilities.isEventDispatchThread());
Window janela = new Window("Khan Academy Frog");
janela.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} // end of anonymous innerclass
); // end of addWindowListener
}
}
Window.java : 이런 일이 발생하고 그것을 해결하는 방법을 왜
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
class DrawingPanel extends JPanel
{
private int x = 50, y = 50;
public DrawingPanel() {
super();
setBorder(new LineBorder(Color.BLACK));
}
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void setX(int x) {
if (x < 0 || x >= 400)
return;
this.x = x;
repaint();
}
public void setY(int y) {
if (y < 0 || y >= 400)
return;
this.y = y;
repaint();
}
public int getX() { return this.x; }
public int getY() { return this.y; }
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("DrawingPanel::paintComponent: drawing at x:" + x + " y:" + y);
System.out.println("DrawingPanel::paintComponent: panel size " + getWidth() + "x" + getHeight());
// face
Color green = new Color(30, 204, 91);
g.setColor(green);
g.fillOval(x, y, 200, 100);
// mouth
g.setColor(Color.BLACK); // g.setColor(new Color(0, 0, 0));
g.fillOval(x+25, y+25, 150, 50);
// left eye
g.setColor(green);
g.fillOval(x+30, y-30, 45, 45); // background
g.setColor(Color.WHITE);
g.fillOval(x+35, y-25, 35, 35); // white eye sockets
g.setColor(Color.BLACK);
g.fillOval(x+48, y-15, 10, 10); // black eyes pretos
// right eye
g.setColor(green);
g.fillOval(x+110, y-30, 45, 45); // background
g.setColor(Color.WHITE);
g.fillOval(x+115, y-25, 35, 35); // white eye sockets
g.setColor(Color.BLACK);
g.fillOval(x+128, y-15, 10, 10); // black eyes
}
}
public class Window extends JFrame
{
DrawingPanel frog_panel;
public Window(String title)
{
super(title);
frog_panel = new DrawingPanel();
addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
frog_panel.setX(frog_panel.getX()-5);
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
frog_panel.setX(frog_panel.getX()+5);
if (e.getKeyCode() == KeyEvent.VK_UP)
frog_panel.setY(frog_panel.getY()-5);
if (e.getKeyCode() == KeyEvent.VK_DOWN)
frog_panel.setY(frog_panel.getY()+5);
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}
);
add(frog_panel);
pack();
setVisible(true);
System.out.println("Window::Window: size is " + getWidth() + "x" + getHeight());
}
}
사람이 알고 있나요 ?
+1 대단히 감사합니다. – karlphillip