-
images
import java.awt.*;
import javax.swing.*;
class d extends JComponent implements Runnable
{
private Image r;
private Image g;
int x=0;
long floor=329;
boolean t=true;
public void run()
{
r=new ImageIcon("red.png").getImage();
g=new ImageIcon("red.png").getImage();
try
{
while(t)
{
repaint();
x++;
if(x==floor)
{
t=false;
q.drawImage(g,229,x-32,this);
}
Thread.sleep(20);
}}catch(Exception e)
{
}
}
public void paint(Graphics q)
{
q.drawImage(r,229,x,this);
}}
class f
{
public static void main(String args[])
{
d v=new d();
JFrame b=new JFrame("sdfdsf");
b.getContentPane().add(v);
b.setVisible(true);
b.setSize(300,400);
(new Thread(v)).start();
}
}
i just want ot place this images one by one i just wnat ot make fist image newfloor so that other image can lie on the top of the other image
-
images building
i am having five off screen images i had load it with the help of Imageicon class and i had drawn it using paint method
i just want the images to be placed on the top of each one in vertical format like a building using threads
could u plz help with this code and and each image width and height is of 32*32
import java.awt.*;
import javax.swing.*;
class d extends JComponent implements Runnable
{
Image r;
Image g;
Image n;
Image t;
int x=0;
long floor=329;
public void run()
{
r=new ImageIcon("1.png").getImage();
g=new ImageIcon("2.png").getImage();
n=new ImageIcon("3.png").getImage();
t=new ImageIcon("4.png").getImage();
try
{
while(x!=floor)
{x++;
repaint();
Thread.sleep(10);
}
}
catch(Exception e)
{
}
}
public void paint(Graphics q)
{
q.drawImage(r,229,x,this);
}
}
class f
{
public static void main(String args[])
{
d v=new d();
JFrame b=new JFrame("sdfdsf");
b.getContentPane().add(v);
b.setVisible(true);
b.setSize(300,400);
(new Thread(v)).start();
}
}
if the code is incomplete then plz help iam stuck in this program
-
To stack the images you don't need a thread/Runnable.
Main trouble was that the letters "g" and "q" got mixed up.
Code:
import java.awt.*;
import javax.swing.*;
class dRx extends JComponent
{
Image r;
Image q;
Image n;
Image t;
int x=0;
long floor=329;
public dRx()
{
r=new ImageIcon("1.png").getImage();
q=new ImageIcon("2.png").getImage();
n=new ImageIcon("3.png").getImage();
t=new ImageIcon("4.png").getImage();
/*
String z = "images/geek/geek";
r=new ImageIcon(z+"-c---.gif").getImage();
q=new ImageIcon(z+"--g--.gif").getImage();
n=new ImageIcon(z+"---h-.gif").getImage();
t=new ImageIcon(z+"----t.gif").getImage();
*/
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
int y = getHeight() - r.getHeight(this);
System.out.println("y = " + y);
g.drawImage(r,x,y,this);
y -= r.getHeight(this);
g.drawImage(q,x,y,this);
y -= q.getHeight(this);
g.drawImage(n,x,y,this);
y -= n.getHeight(this);
g.drawImage(t,x,y,this);
}
public static void main(String[] args)
{
dRx v=new dRx();
JFrame b=new JFrame("sdfdsf");
b.getContentPane().add(v);
b.setSize(300,400);
b.setVisible(true);
}
}
-
General commet on your coding style.
Using single letters for variable makes it very hard to understand the logic of the program. Please take the time to think what a variable is used for and give it a descriptive name.
Personally I wouldn't bother trying to solve problems with this code as its too hard to follow what you are trying to do.
Another useful coding technique is to place comments in the code to describe what it is you are trying to do. Again, this code is void of comments.