Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor. Made by Grzechu Rubinoff
- */
- package lab4;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.ArrayList;
- public class AwtControlDemo implements KeyListener {
- private Frame mainFrame;
- private Panel controlPanel;
- public ArrayList<Coord> coords = new ArrayList<Coord>();
- public double xShift, yShift;
- public int bufferShiftX = 500;
- public int bufferShiftY = 500;
- public AwtControlDemo(ArrayList<Coord> coordList){
- coords = coordList;
- xShift = coords.get(0).getX();
- yShift = coords.get(0).getY();
- prepareGUI();
- }
- private void prepareGUI(){
- mainFrame = new Frame("Java AWT Examples");
- mainFrame.setSize(1000,1000);
- mainFrame.setLayout(new GridLayout(1, 1));
- mainFrame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent windowEvent){
- System.exit(0);
- }
- });
- controlPanel = new Panel();
- controlPanel.setLayout(new FlowLayout());
- mainFrame.add(controlPanel);
- mainFrame.setVisible(true);
- mainFrame.addKeyListener(this);
- }
- public void showCanvasDemo(){
- controlPanel.add(new MyCanvas());
- mainFrame.setVisible(true);
- }
- @Override
- public void keyTyped(KeyEvent e) {
- char keyCode = e.getKeyChar();
- if(keyCode == 'w'){
- bufferShiftY -= 50;
- } else if(keyCode == 's'){
- bufferShiftY += 50;
- } else if(keyCode == 'a'){
- bufferShiftX -=50;
- } else if(keyCode == 'd'){
- bufferShiftX +=50;
- }
- controlPanel.removeAll();
- controlPanel.add(new MyCanvas());
- }
- @Override
- public void keyPressed(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- class MyCanvas extends Canvas {
- public MyCanvas () {
- setBackground (new Color(230, 230, 220, 255));
- setSize(1000, 1000);
- }
- public void paint (Graphics g) {
- Graphics2D g2;
- g2 = (Graphics2D) g;
- drawMap(g2);
- }
- public void drawMap(Graphics2D g2) {
- double lastX = xShift;
- double lastY = yShift;
- for (Coord coord: coords) {
- int x1 =(int) ((lastX - xShift))*2;
- int y1 =(int) ((lastY - yShift))*2;
- int x2 =(int) ((coord.getX() - xShift))*2;
- int y2 =(int) ((coord.getY()- yShift))*2;
- g2.drawLine(x1+bufferShiftX, y1+bufferShiftY, x2+bufferShiftX, y2+bufferShiftY);
- lastX = coord.getX();
- lastY = coord.getY();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement