잔글 (Chirho님이 사용자:Cerulean/메모장/java 문서를 넘겨주기를 만들지 않고 사용자:하늘/메모장/java 문서로 이동했습니다: "Cerulean" 사용자의 이름을 "하늘"(으)로 바꿀 때 문서를 자동으로 이동했습니다) |
편집 요약 없음 |
||
17번째 줄: | 17번째 줄: | ||
코드 쓰고 F5 | 코드 쓰고 F5 | ||
<syntaxhighlight lang="java"> | |||
package code; | |||
import java.awt.BorderLayout; | |||
import java.awt.Color; | |||
import java.awt.Graphics; | |||
import java.awt.event.ActionEvent; | |||
import java.awt.event.ActionListener; | |||
import javax.swing.JButton; | |||
import javax.swing.JFrame; | |||
import javax.swing.JPanel; | |||
public class Test extends JFrame implements ActionListener { | |||
class DrawPanel extends JPanel { | |||
public int positionX; public int positionY; | |||
public void paintComponent(Graphics g) { | |||
g.setColor(Color.BLACK); | |||
g.drawOval(positionX, positionY, 100, 100); | |||
} | |||
} | |||
DrawPanel drawPanel; | |||
int moveOffset; | |||
public Test9_ppt () { | |||
setSize(800, 500); | |||
setLayout(new BorderLayout()); | |||
JButton upButton = new JButton("Up"); | |||
upButton.addActionListener(this); | |||
add(upButton, "North"); | |||
JButton downButton = new JButton("Down"); | |||
downButton.addActionListener(this); | |||
add(downButton , "South"); | |||
JButton rightButton = new JButton("Right"); | |||
rightButton.addActionListener(this); | |||
add(rightButton, "East"); | |||
JButton leftButton = new JButton("Left"); | |||
leftButton.addActionListener(this); | |||
add(leftButton, "West"); | |||
drawPanel = new DrawPanel(); | |||
getContentPane().add(drawPanel); | |||
moveOffset = 20; | |||
setVisible(true); | |||
} | |||
public void actionPerformed(ActionEvent e) { | |||
String command = e.getActionCommand(); | |||
final class Vector{ | |||
static int X; static int Y; | |||
} | |||
switch (command) { | |||
case "Up": | |||
Vector.Y = -1; break; | |||
case "Down": | |||
Vector.Y = 1; break; | |||
case "Left": | |||
Vector.X = -1; break; | |||
case "Right": | |||
Vector.X = 1; break; | |||
} | |||
new Thread(() -> { | |||
for (int i = 0; i < moveOffset; i++) { | |||
try { | |||
Thread.sleep(i); | |||
} catch (InterruptedException e1) { | |||
e1.printStackTrace(); | |||
} | |||
drawPanel.positionY += Vector.Y; | |||
drawPanel.positionX += Vector.X; | |||
getContentPane().repaint(); | |||
} | |||
Vector.X = 0; | |||
Vector.Y = 0; | |||
}).start(); | |||
} | |||
public static void main(String[] args) { | |||
Test9_ppt window = new Test(); | |||
} | |||
} | |||
//오옹 나이스 | |||
</syntaxhighlight> |
2022년 6월 19일 (일) 21:07 판
sudo pacman -S jdk-openjdk
sudo pacman -S code
F1 누르고
> settings json
intellicode 설치하고
java.home 입력해서 자동으로 뜨는 거 확인
"java.jdt.ls.java.home": "/usr/lib/jvm/java-17-openjdk/"
코드 쓰고 F5
package code;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame implements ActionListener {
class DrawPanel extends JPanel {
public int positionX; public int positionY;
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.drawOval(positionX, positionY, 100, 100);
}
}
DrawPanel drawPanel;
int moveOffset;
public Test9_ppt () {
setSize(800, 500);
setLayout(new BorderLayout());
JButton upButton = new JButton("Up");
upButton.addActionListener(this);
add(upButton, "North");
JButton downButton = new JButton("Down");
downButton.addActionListener(this);
add(downButton , "South");
JButton rightButton = new JButton("Right");
rightButton.addActionListener(this);
add(rightButton, "East");
JButton leftButton = new JButton("Left");
leftButton.addActionListener(this);
add(leftButton, "West");
drawPanel = new DrawPanel();
getContentPane().add(drawPanel);
moveOffset = 20;
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
final class Vector{
static int X; static int Y;
}
switch (command) {
case "Up":
Vector.Y = -1; break;
case "Down":
Vector.Y = 1; break;
case "Left":
Vector.X = -1; break;
case "Right":
Vector.X = 1; break;
}
new Thread(() -> {
for (int i = 0; i < moveOffset; i++) {
try {
Thread.sleep(i);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
drawPanel.positionY += Vector.Y;
drawPanel.positionX += Vector.X;
getContentPane().repaint();
}
Vector.X = 0;
Vector.Y = 0;
}).start();
}
public static void main(String[] args) {
Test9_ppt window = new Test();
}
}
//오옹 나이스