stacker
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| stacker [2023/05/11 13:56] – frchris | stacker [2023/05/19 10:13] (current) – frchris | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Stacker ====== | ====== Stacker ====== | ||
| [[https:// | [[https:// | ||
| - | Box | ||
| - | BoxTester | + | [[https:// |
| + | <code java Box.java> | ||
| + | import java.awt.*; | ||
| + | |||
| + | /** | ||
| + | * Write a description of class Box here. | ||
| + | * | ||
| + | * @author (your name) | ||
| + | * @version (a version number or a date) | ||
| + | */ | ||
| + | public class Box | ||
| + | { | ||
| + | // instance variables - replace the example below with your own | ||
| + | | ||
| + | private int x,y,width, direction, leftLimit, rightLimit ; | ||
| + | private boolean falling; | ||
| + | | ||
| + | | ||
| + | |||
| + | /** | ||
| + | * Constructor for objects of class Box | ||
| + | */ | ||
| + | public Box(int x, int y, int width) | ||
| + | { | ||
| + | // initialise instance variables | ||
| + | this.x =x; | ||
| + | this.y =y; | ||
| + | direction = 1; | ||
| + | this.width = width; | ||
| + | leftLimit = x; | ||
| + | rightLimit = x+6*width; | ||
| + | falling=false; | ||
| + | } | ||
| + | |||
| + | public void draw(Graphics g) | ||
| + | { | ||
| + | // put your code here | ||
| + | g.setColor(Color.BLUE.darker()); | ||
| + | g.fillRect(x, | ||
| + | } | ||
| + | | ||
| + | public void move() | ||
| + | { | ||
| + | if(direction ==0) | ||
| + | return; | ||
| + | int next = 20*direction; | ||
| + | | ||
| + | | ||
| + | if ( x+next > rightLimit | ||
| + | { | ||
| + | direction = -1; | ||
| + | } | ||
| + | if ( x+next < leftLimit | ||
| + | { | ||
| + | direction = 1; | ||
| + | } | ||
| + | | ||
| + | if (falling){ | ||
| + | y += next; | ||
| + | }else{ | ||
| + | x += next; | ||
| + | } | ||
| + | } | ||
| + | public void stop(){ | ||
| + | direction =0; | ||
| + | falling=true; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java BoxTester.java> | ||
| + | |||
| + | import java.awt.Color; | ||
| + | import java.awt.Font; | ||
| + | import java.awt.Graphics; | ||
| + | import java.awt.event.ActionEvent; | ||
| + | import java.awt.event.ActionListener; | ||
| + | import java.awt.event.KeyEvent; | ||
| + | import java.awt.event.KeyListener; | ||
| + | import javax.swing.JFrame; | ||
| + | import javax.swing.JPanel; | ||
| + | import javax.swing.Timer; | ||
| + | |||
| + | public class BoxTester extends JPanel implements KeyListener, | ||
| + | { | ||
| + | public static int WIDTH=800; | ||
| + | public static int HEIGHT=800; | ||
| + | private Font titleFont, regularFont; | ||
| + | private int x; | ||
| + | private Timer timer1, timer2; | ||
| + | private Box b; | ||
| + | |||
| + | public BoxTester() | ||
| + | { | ||
| + | |||
| + | // | ||
| + | titleFont = new Font(" | ||
| + | regularFont = new Font(" | ||
| + | x=0; | ||
| + | b = new Box(75,175, 50); | ||
| + | timer1 = new Timer(20, this); //1000=1 seconds | ||
| + | //timer2 = new Timer(2000, this); //1000=1 seconds | ||
| + | timer1.start(); | ||
| + | | ||
| + | |||
| + | } | ||
| + | public static void main(String[] args) { | ||
| + | BoxTester app= new BoxTester(); | ||
| + | JFrame window = new JFrame(" | ||
| + | window.setSize(WIDTH, | ||
| + | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | window.getContentPane().add(app); | ||
| + | window.addKeyListener(app); | ||
| + | // | ||
| + | window.setVisible(true); | ||
| + | |||
| + | } | ||
| + | public void paintComponent(Graphics g){ | ||
| + | super.paintComponent(g); | ||
| + | g.setColor(Color.WHITE); | ||
| + | g.fillRect(0, | ||
| + | g.setColor(Color.BLUE); | ||
| + | g.setFont(titleFont); | ||
| + | g.drawString(" | ||
| + | g.setColor(Color.BLACK); | ||
| + | g.setFont(regularFont); | ||
| + | g.drawString(" | ||
| + | b.draw(g); | ||
| + | |||
| + | } | ||
| + | // update is a workaround to cure Windows screen flicker problem | ||
| + | public void update(Graphics g){ | ||
| + | paint(g); | ||
| + | } | ||
| + | |||
| + | // These 3 methods need to be declares to implement the KeyListener Interface | ||
| + | @Override | ||
| + | public void keyTyped(KeyEvent e) {} | ||
| + | |||
| + | @Override | ||
| + | public void keyPressed(KeyEvent e) {} | ||
| + | |||
| + | @Override | ||
| + | public void keyReleased(KeyEvent e) { | ||
| + | | ||
| + | } | ||
| + | @Override | ||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | |||
| + | if (e.getSource()==timer1){ | ||
| + | | ||
| + | x++; | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
stacker.1683827773.txt.gz · Last modified: 2023/05/11 13:56 by frchris