KeyAdapterPaint.java
Код: Выделить всё
package com.videogame.control;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import com.videogame.image.ImageOnGui;
import com.videogame.movementcaracter.Motion;
public class KeyEventAdapterPaint extends KeyAdapter {
private PaintComponent imagePaint;
private JFrame window;
private Image imageLeft;
private Image imageRight;
private int x;
public KeyEventAdapterPaint(JFrame window,Image imageLeft,Image imageRight) {
super();
this.imageLeft=imageLeft;
this.imageRight=imageRight;
this.imagePaint=new PaintComponent(0,0);
x=0;
}
public void putPanel(JFrame window) {
imagePaint.setLayout(new BorderLayout());
imagePaint.setPreferredSize(new Dimension(2000, 150));
imagePaint.setDoubleBuffered(true);
window.add(imagePaint);
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
x=x+1;
imagePaint.setX(x);
imagePaint.setImage(imageRight);
imagePaint.repaint();
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
x=x-1;
imagePaint.setX(x);
imagePaint.setImage(imageLeft);
imagePaint.repaint();
}
}
}
Код: Выделить всё
package com.videogame.control;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PaintComponent extends JLabel{
private static final long serialVersionUID = 1L;
private Image image;
private int x;
private int y;
public PaintComponent(int x,int y) {
this.image=null;
this.x=x;
this.y=y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(getImage(), x, y, null);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
}
Код: Выделить всё
package com.videogame.control;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public abstract class Control {
private JFrame window;
private KeyListener keyControl;
public Control(JFrame window) {
this.setWindow(window);
}
public abstract void putDirection();
public JFrame getWindow() {
return window;
}
public void setWindow(JFrame window) {
this.window = window;
}
public KeyListener getKeyControl() {
return keyControl;
}
public void setKeyControl(KeyListener keyControl) {
this.keyControl = keyControl;
}
}
Код: Выделить всё
package com.videogame.control;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import com.videogame.movementcaracter.Motion;
public class ControlLeft extends Control {
public ControlLeft(JFrame window, KeyListener keyControl) {
super(window);
setKeyControl(keyControl);
}
@Override
public void putDirection() {
getWindow().addKeyListener(getKeyControl());
}
}
Код: Выделить всё
package com.videogame.image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.net.URL;
public class ImageOnGui extends Image {
private ImageIcon imageIc;
private JLabel jlabel;
public ImageOnGui() {
imageIc=new ImageIcon();
jlabel=new JLabel();
}
public ImageOnGui(String url){
URL gifurl;
gifurl=ImageOnGui.class.getResource("/"+url);
setImageIcon(new ImageIcon(gifurl));
setJlabel(new JLabel(getImageIcon()));
}
public ImageIcon getImageIcon() {
return imageIc;
}
public void setImageIcon(ImageIcon imageIc) {
this.imageIc = imageIc;
}
public JLabel getJlabel() {
return jlabel;
}
public void setJlabel(JLabel jlabel) {
this.jlabel = jlabel;
}
@Override
public void print(JFrame window) {
// TODO Auto-generated method stub
}
}
Код: Выделить всё
package com.videogame.image;
import javax.swing.JFrame;
public abstract class Image {
public Image() {
}
public abstract void print(JFrame window);
}
Код: Выделить всё
package com.videogame.controlInit;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JFrame;
import com.videogame.image.ImageOnGui;
import com.videogame.control.Control;
import com.videogame.control.ControlLeft;
import com.videogame.control.KeyEventAdapterPaint;
public class Init {
public static void main(String[] args) {
JFrame window = new JFrame("ventana");
window.setSize(new Dimension(2000, 2000));
ArrayList controls = new ArrayList();
ImageOnGui imageLeft=new ImageOnGui("manWalkingLeft.gif");
ImageOnGui imageRight=new ImageOnGui("manWalkingRight.gif");
KeyEventAdapterPaint keyP=new KeyEventAdapterPaint(window,imageLeft.getImageIcon().getImage(),imageRight.getImageIcon().getImage());
controls.add(new ControlLeft(window,keyP));
for (Control con : controls) {
con.putDirection();
}
imageLeft.print(window);
keyP.putPanel(window);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
Вы должны поместить любые 2 gif в исходный/основной/ресурсы по вашему выбору для проверки кода.
Также вы поместите пакет com.videogame.control;com.videogame.image;com.videogame.controlInit в папку src/main/java
Любой Помогите! Большое спасибо.
Подробнее здесь: https://stackoverflow.com/questions/797 ... keyadapter
Мобильная версия