내 게임의 로그인 화면에서 작업 중입니다. 나는 총 2 개의 이미지를 가지고있다. 하나는 스플래시 스크린 샷이고 다른 하나는 배경 이미지입니다. BufferedImages를 사용하여 이미지를 화면에 렌더링합니다.단추의 크기를 조정하지 않고 단추를 Canvas에 어떻게 추가합니까?
내가 얻는 문제는 캔버스에 표준 단추를 추가하면 단추가 전체 창을 차지하고 분명히 그럴 필요가 없다는 것입니다.
나는 사진을 게시 하겠지만 슬프게도 나는 그렇게하기에는 "충분한 평판"이 없다. 여기에 내 코드를 살펴 비록입니다 :
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class Login extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 495;
private static final int HEIGHT = 307;
private static final int SCALE = 2;
private final Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
private BufferedImage splash = new BufferedImage(315, 177, BufferedImage.TYPE_INT_RGB);
public int[] splashPixels = ((DataBufferInt) splash.getRaster().getDataBuffer()).getData();
private Thread thread;
public static boolean isRunning = false;
JFrame frame;
MainMenu menu;
Splash splashscreen;
Button login;
Button register;
TextField username;
private Login() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
exc.printStackTrace();
}
frame = new JFrame("Game Login");
menu = new MainMenu(WIDTH, HEIGHT, "/login/login_screen.png");
splashscreen = new Splash(315, 177, "/login/splash.png");
frame.setSize(size);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
setPreferredSize(size);
frame.add(this);
login = new Button("Login");
login.setBounds(0, 0, getWidth(), getHeight());
frame.add(login);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void begin() {
createBufferStrategy(2);
thread = new Thread(this);
thread.start();
isRunning = true;
}
private void finish() throws InterruptedException {
isRunning = false;
thread.join();
}
private void updateLogin() {
for (int a = 0; a < pixels.length; a++) {
pixels[a] = menu.pixels[a];
}
}
private void renderLogin() {
BufferStrategy buffer = getBufferStrategy();
Graphics gfx = buffer.getDrawGraphics();
for (int a = 0; a < splashPixels.length; a++) {
splashPixels[a] = splashscreen.splashPixels[a];
}
gfx.drawImage(image, 0, 0, getWidth(), getHeight(), null);
gfx.drawImage(splash, 320, 37, 625, 340, null);
gfx.setColor(Color.WHITE);
gfx.drawString("Game Co © 2013", 3, (getHeight() - 4));
gfx.drawString("\"Game\" is a trademark of Blah-Blah-Blah.", (getWidth() - 268), (getHeight() - 3));
gfx.dispose();
buffer.show();
}
public void run() {
while (isRunning == true) {
updateLogin();
renderLogin();
}
try {
finish();
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}
public static void main(String[] args) {
Login login = new Login();
login.begin();
}
}
다시 한번, 내 유일한 문제는 내가 확대 버튼을 점점 계속 것입니다.
미리 감사드립니다. 사람들이 바쁘다는 것을 알고 있습니다. 답을 찾아보고 내 질문에 답변하는 데 시간을내어 주셔서 감사합니다.
P. 누구든지 AWT 암호 필드를 만드는 방법을 알고 있습니까? 나는 또한 그것을 필요로 할 것이다. ;)
왜 AWT와 스윙을 혼합하고 있습니까? 이것은 의미가 없으며 불필요하게 할 수있는 일을 제한하도록 강요합니다. 대신 모든 Swing 구성 요소를 사용해야하는 이유는 무엇입니까? –