Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ergastirio16;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- /**
- *
- * @author aifragkou
- */
- public class Game extends JFrame implements ActionListener , MouseMotionListener{
- private MyPanel panel;
- private Timer timer;
- public Game(){
- this.setBounds(0,0,800,600);
- this.setTitle("Hello unicorn");
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.panel = new MyPanel(this);
- this.add(this.panel);
- this.setVisible(true);
- this.timer = new Timer(33,this);
- this.timer.start();
- this.addMouseMotionListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // this.panel.setUnicornX(this.panel.getUnicornX()+1);
- this.repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- this.panel.setUnicornX(e.getX());
- this.panel.setUnicornY(e.getY());
- }
- private class MyPanel extends JPanel{
- private Game gameInstance;
- private BufferedImage image;
- private int unicornX,unicornY;
- public int getUnicornX() {
- return unicornX;
- }
- public void setUnicornX(int unicornX) {
- this.unicornX = unicornX;
- }
- public int getUnicornY() {
- return unicornY;
- }
- public void setUnicornY(int unicornY) {
- this.unicornY = unicornY;
- }
- public MyPanel(Game game){
- this.gameInstance = game;
- try {
- this.image = (BufferedImage)ImageIO.read(new File("C:\\Users\\aifragkou\\Downloads\\unicorn1.jpg"));
- } catch (IOException ex) {
- }
- }
- @Override
- public void paint(Graphics g){
- if(Math.random()>0.5){
- g.setColor(Color.BLACK);
- }else{
- g.setColor(Color.GREEN);
- }
- g.fillRect(0,0, this.gameInstance.getWidth(),this.gameInstance.getHeight());
- g.drawImage(image,this.getUnicornX(),this.getUnicornY(),this);
- g.setColor(Color.GREEN);
- g.drawString("Hello world", 50, 50);
- }
- }
- public static void main(String[] args){
- Game g = new Game();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement