Create Dynamic Ladder with Java | Learn to Code Java Graphics

  • Context: Java 
  • Thread starter Thread starter life4menu
  • Start date Start date
  • Tags Tags
    Shapes
Click For Summary
SUMMARY

The discussion focuses on creating a dynamic ladder using Java's Graphics API, specifically through the implementation of a `LadderConstruction` class that draws rectangles representing rungs. The code utilizes a while loop to draw and animate the rectangles, with the `clearRect()` method employed to make previously drawn rectangles disappear. Key components include the use of `Graphics`, `Graphics2D`, and a `Timer` for animation. The solution provided effectively addresses the user's requirement for rectangles to flash off as new ones are drawn.

PREREQUISITES
  • Java programming knowledge, specifically with Swing and AWT libraries
  • Understanding of the Graphics class and its methods in Java
  • Familiarity with event handling in Java using ActionListener
  • Basic knowledge of object-oriented programming concepts in Java
NEXT STEPS
  • Explore Java Swing for advanced GUI development
  • Learn about animation techniques in Java using the Timer class
  • Investigate the use of double buffering to improve rendering performance
  • Study the implementation of custom painting in Java applications
USEFUL FOR

Java developers, particularly those interested in graphical user interface design and animation, as well as educators teaching Java graphics programming concepts.

life4menu
Messages
1
Reaction score
0
Hi,

I have a program that draws four rectangles at a time using a while loop.
this is my code.

[code = java]



import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


/**
* @author Akshintala
*/

/**
* @param args
*/

public class LadderConstruction extends JPanel implements ActionListener {

/**
*
*/
private static final long serialVersionUID = 1L;

public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;

g.setColor( Color.BLACK );
g2.setStroke(new BasicStroke(4));

g.drawLine(300,300,950,300); // Upper side wall
g.drawString("R",960,300); // Label Upper

g.drawLine(300,500,950,500); // Lower side wall
g2.setStroke(new BasicStroke(8));
g.drawLine(300,400,950,400); // Middle ridge
g.drawString("L",960,500); // Label Upper


g.setColor( Color.GRAY);
g2.setStroke(new BasicStroke(2));
g.drawString("27",298,295); // Label 1
g.drawString("27",298,515); // Label 1

g.drawLine(300,300,300,395); // Upper rung 1
g.drawLine(300,405,300,500); // Lower rung 1
g.drawString("26",323,295); // Label 2
g.drawString("26",321,515); // Label 2

g.drawLine(325,300,325,395); // Upper rung 2
g.drawLine(325,405,325,500); // Lower rung 2
g.drawString("25",348,295); // Label 3
g.drawString("25",346,515); // Label 3

g.drawLine(350,300,350,395); // Upper rung 2
g.drawLine(350,405,350,500); // Lower rung 3
g.drawString("24",373,295); // Label 4
g.drawString("24",371,515); // Label 4

g.drawLine(375,300,375,395); // Upper rung 4
g.drawLine(375,405,375,500); // Lower rung 4
g.drawString("23",398,295); // Label 5
g.drawString("23",397,515); // Label 5

g.drawLine(400,300,400,395); // Upper rung 4
g.drawLine(400,405,400,500); // Lower rung 5
g.drawString("22",423,295); // Label 6
g.drawString("22",421,515); // Label 5

g.drawLine(425,300,425,395); // Upper rung 6
g.drawLine(425,405,425,500); // Lower rung 6
g.drawString("21",448,295); // Label 7
g.drawString("21",446,515); // Label 7

g.drawLine(450,300,450,395); // Upper rung 7
g.drawLine(450,405,450,500); // Lower rung 7
g.drawString("20",473,295); // Label 8
g.drawString("20",472,515); // Label 7

g.drawLine(475,300,475,395); // Upper rung 8
g.drawLine(475,405,475,500); // Lower rung 8
g.drawString("19",498,295); // Label 9
g.drawString("19",496,515); // Label 9

g.drawLine(500,300,500,395); // Upper rung 9
g.drawLine(500,405,500,500); // Lower rung 9
g.drawString("18",520,295); // Label 10
g.drawString("18",518,515); // Label 9

g.drawLine(525,300,525,395); // Upper rung 10
g.drawLine(525,405,525,500); // Lower rung 10
g.drawString("17",545,295); // Label 11
g.drawString("17",543,515); // Label 11

g.drawLine(550,300,550,395); // Upper rung 11
g.drawLine(550,405,550,500); // Lower rung 11
g.drawString("16",570,295); // Label 12
g.drawString("16",568,515); // Label 12

g.drawLine(575,300,575,395); // Upper rung 12
g.drawLine(575,405,575,500); // Lower rung 12
g.drawString("15",595,295); // Label 13
g.drawString("15",593,515); // Label 13

g.drawLine(600,300,600,395); // Upper rung 13
g.drawLine(600,405,600,500); // Lower rung 13
g.drawString("14",620,295); // Label 14
g.drawString("14",618,515); // Label 14

g.drawLine(625,300,625,395); // Upper rung 14
g.drawLine(625,405,625,500); // Lower rung 14
g.drawString("13",645,295); // Label 15
g.drawString("13",643,515); // Label 15

g.drawLine(650,300,650,395); // Upper rung 15
g.drawLine(650,405,650,500); // Lower rung 15
g.drawString("12",670,295); // Label 16
g.drawString("12",668,515); // Label 16

g.drawLine(675,300,675,395); // Upper rung 16
g.drawLine(675,405,675,500); // Lower rung 16
g.drawString("11",695,295); // Label 17
g.drawString("11",693,515); // Label 17

g.drawLine(700,300,700,395); // Upper rung 17
g.drawLine(700,405,700,500); // Lower rung 17
g.drawString("10",720,295); // Label 18
g.drawString("10",718,515); // Label 18

g.drawLine(725,300,725,395); // Upper rung 18
g.drawLine(725,405,725,500); // Lower rung 18
g.drawString("09",745,295); // Label 19
g.drawString("09",743,515); // Label 19

g.drawLine(750,300,750,395); // Upper rung 19
g.drawLine(750,405,750,500); // Lower rung 19
g.drawString("08",770,295); // Label 20
g.drawString("08",768,515); // Label 20

g.drawLine(775,300,775,395); // Upper rung 20
g.drawLine(775,405,775,500); // Lower rung 20
g.drawString("07",795,295); // Label 21
g.drawString("07",793,515); // Label 21

g.drawLine(800,300,800,395); // Upper rung 21
g.drawLine(800,405,800,500); // Lower rung 21
g.drawString("06",820,295); // Label 22
g.drawString("06",818,515); // Label 22

g.drawLine(825,300,825,395); // Upper rung 22
g.drawLine(825,405,825,500); // Lower rung 22
g.drawString("05",845,295); // Label 23
g.drawString("05",843,515); // Label 23

g.drawLine(850,300,850,395); // Upper rung 23
g.drawLine(850,405,850,500); // Lower rung 23
g.drawString("04",870,295); // Label 24
g.drawString("04",868,515); // Label 24

g.drawLine(875,300,875,395); // Upper rung 24
g.drawLine(875,405,875,500); // Lower rung 24
g.drawString("03",895,295); // Label 25
g.drawString("03",893,515); // Label 25

g.drawLine(900,300,900,395); // Upper rung 25
g.drawLine(900,405,900,500); // Lower rung 25
g.drawString("02",920,295); // Label 26
g.drawString("02",918,515); // Label 26

g.drawLine(925,300,925,395); // Upper rung 26
g.drawLine(925,405,925,500); // Lower rung 26
g.drawString("01",945,295); // Label 27
g.drawString("01",943,515); // Label 27

g.drawLine(950,300,950,395); // Upper rung 27
g.drawLine(950,405,950,500); // Lower rung 27


int x = 270;
int y= 350;
int yl = 450;
int x1 = 320;
int x2 = 370;

while(x<320){
x = x+25;
// System.out.println(x);
g.setColor(Color.CYAN);
g.drawRect(x,y,10,5);
g.drawRect(x,yl,10,5);
g.drawRect(x, y, 10, 5);
g.drawRect(x, yl, 10, 5);
g.fillRect(x, y, 10, 5);
g.fillRect(x, yl, 10, 5);


}


while(x1<370){
x1 = x1+25;
g.setColor(Color.CYAN);
g.drawRect(x1,y,10,5);
g.drawRect(x1,yl,10,5);
g.drawRect(x1, y, 10, 5);
g.drawRect(x1, yl, 10, 5);
g.fillRect(x1, y, 10, 5);
g.fillRect(x1, yl, 10, 5);
g.clearRect(x1, y, 10, 5);
g.clearRect(x1, yl, 10, 5);
}
while(x2<420){
x2 = x2+25;
g.setColor(Color.CYAN);
g.drawRect(x2,y,10,5);
g.drawRect(x2,yl,10,5);
g.drawRect(x2, y, 10, 5);
g.drawRect(x2, yl, 10, 5);
}

}

private JPanel ladder;
private Timer t = new Timer(1000,this)

public LadderConstruction( ) {

t.setInitialDelay(074);
ladder = new JPanel();
this.add(ladder);
setSize(500,500);
this.setVisible(true);
t.start( );
}

public void actionPerformed(ActionEvent ae) {

LadderConstruction lc= new LadderConstruction();

lc.repaint();

// lc.repaint();
t.stop();
}

public static void main (String args[]){

LadderConstruction lc= new LadderConstruction();

JFrame frame = new JFrame("Ladder Rung Sensor");
//
frame.add(lc);
frame.setSize(500,500);
frame.setVisible(true);
// frame.setLocationRelativeTo(null);
}
}
[/code]

I am drawing four rectangles using following code
Java:
while(x<320){
				x = x+25;
				//		System.out.println(x);
				g.setColor(Color.CYAN);
				g.drawRect(x,y,10,5);
				g.drawRect(x,yl,10,5);
				g.drawRect(x, y, 10, 5);
				g.drawRect(x, yl, 10, 5);
				g.fillRect(x, y, 10, 5);
				g.fillRect(x, yl, 10, 5);
}

Then later I print the next four rectangles by just changing my x coordinates. during this I want to make the previous rectangles disappear or flash off or whatever.

Please help me.
Thanks a lot.
 
Technology news on Phys.org


Hi there,

Thank you for sharing your code and explaining your problem. It seems like you are trying to create a ladder with rungs that appear and disappear one at a time. To make the previous rectangles disappear, you can use the Graphics class's `clearRect()` method. This method takes in the coordinates of the rectangle you want to clear and removes it from the screen.

Here is an example of how you can modify your code to make the previous rectangles disappear:

// loop through all the rectangles
while(x<320){
x = x+25;
// draw the rectangles
g.setColor(Color.CYAN);
g.drawRect(x,y,10,5);
g.drawRect(x,yl,10,5);
g.drawRect(x, y, 10, 5);
g.drawRect(x, yl, 10, 5);
g.fillRect(x, y, 10, 5);
g.fillRect(x, yl, 10, 5);

// clear the previous rectangles
g.clearRect(x-25, y, 10, 5);
g.clearRect(x-25, yl, 10, 5);
g.clearRect(x-25, y, 10, 5);
g.clearRect(x-25, yl, 10, 5);
}

I hope this helps. Good luck with your project!



[Your Title]
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K