Results 1 to 13 of 13
Thread: program problem
- 05-16-2008, 11:40 AM #1
Member
- Join Date
- May 2008
- Posts
- 24
- Rep Power
- 0
program problem
i just want to load two images in the frame but iam getting only one image at a time can u give me logic for writing multiple images in frame at same time and also give the logic for generating random images for this multiple images
import java.awt.*;
import javax.swing.*;
class a extends JComponent implements Runnable
{
Image q[]=new Image[2];
int f=0;
int x=200,y=200;
int c;
public void paint(Graphics g)
{
Image r=q[f];
g.drawImage(r,x,y,this);
}
public void run()
{
q[0]=new ImageIcon("red.png").getImage();
q[1]=new ImageIcon("blue.png").getImage();
try
{
while(true)
{
f=(f+1)%q.length;
repaint();
Thread.sleep(1000);
}}
catch(InterruptedException m)
{
}
}
}
class b
{
public static void main(String args[])
{
a x=new a();
JFrame s= new JFrame(" GAME ");
s.getContentPane().add(x);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setVisible(true);
s.setSize(800,800);
s.setResizable(false);
(new Thread(x)).start();
}
}
- 05-16-2008, 11:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Randomly change means, display one image after the other?
- 05-16-2008, 11:58 AM #3
Member
- Join Date
- May 2008
- Posts
- 24
- Rep Power
- 0
random
generating both the images randomly i a frame example i hav red and blue
randomly generate possible combinations in frame red red ,red blue, blue red, blue blue
- 05-16-2008, 12:01 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So randomly select the array index of you images. Use Random class. Got it?
- 05-16-2008, 12:08 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
According to your application do it as follows.
Java Code:public void run() { q[0] = new ImageIcon("images\\logo.gif").getImage(); q[1] = new ImageIcon("images\\RADAR.png").getImage(); try { while(true) { //f = ( f + 1 ) % q.length; f = randomIndex(); repaint(); Thread.sleep(1000); } } catch(InterruptedException m) { } } public int randomIndex() { Random rr = new Random(); return rr.nextInt(2); // number of images you used }
- 05-16-2008, 12:15 PM #6
Member
- Join Date
- May 2008
- Posts
- 24
- Rep Power
- 0
paint
in paint method when i give repaint then it draws only one image i,e q[0]
after f ==1 then it only draws q[1] i just want to draw both q[0] and q[1] at the same time can u give me the logic i will make x=x+200 for q[1] so that the images will be side by side
- 05-16-2008, 12:17 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-16-2008, 12:20 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-16-2008, 12:29 PM #9
Member
- Join Date
- May 2008
- Posts
- 24
- Rep Power
- 0
program
see if there is a mistake in telling i apolozise but iam telling u frankly my idea is all about
i jst want to display both the different images at the same time but when iam trying to do that it is displaying any one of the images i just want to display both the images at same time and i also want to generate two random images at same time for i will just give u the picture
- 05-16-2008, 12:34 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, you can try something different like this. Use two labels in two locations you want. Then add images there randomly selected. I don't think it is difficult.
- 05-16-2008, 06:56 PM #11
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.util.Random; import javax.swing.*; public class RandomImagesTest extends JPanel { BufferedImage[] images; Random seed = new Random(); int leftIndex = 0; int rightIndex = 1; RandomImagesTest() { makeImages(); new Thread(runner).start(); } protected void paintComponent(Graphics g) { super.paintComponent(g); int x1 = 50; int y1 = 50; g.drawImage(images[leftIndex], x1, y1, this); int x2 = x1 + images[leftIndex].getWidth() + 20; int y2 = y1; g.drawImage(images[rightIndex], x2, y2, this); } private Runnable runner = new Runnable() { long delay = 1000; boolean animate = true; public void run() { while(animate) { try { Thread.sleep(delay); } catch(InterruptedException e) { animate = false; } int n = images.length; leftIndex = seed.nextInt(n); rightIndex = seed.nextInt(n); //System.out.printf("leftIndex = %d rightIndex = %d%n", // leftIndex, rightIndex); repaint(); } } }; private void makeImages() { Color[] colors = { Color.red, Color.blue }; int w = 40, h = 40; int type = BufferedImage.TYPE_INT_RGB; images = new BufferedImage[colors.length]; for(int i = 0; i < images.length; i++) { images[i] = new BufferedImage(w, h, type); Graphics2D g2 = images[i].createGraphics(); g2.setBackground(colors[i]); g2.clearRect(0,0,w,h); g2.dispose(); } } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new RandomImagesTest()); f.setSize(300,300); f.setLocation(200,200); f.setVisible(true); } }
- 05-16-2008, 07:19 PM #12
Member
- Join Date
- May 2008
- Posts
- 24
- Rep Power
- 0
assigning
how can i assign image object to g.drawimage for example
Image q=new Image;
g.drawImage(q,x,y,this);
Image m=new Image();
m=g.drawImage(q,x,y,this);
- 05-16-2008, 08:07 PM #13
The main problem with doing it this way is that the images may change with every system-initiated repaint event. It is better to assign the images in your event code, viz, in your Runnable implementation, as shown above.Java Code:protected void paintComponent(... ... Image q=images[seed.nextInt(images.length)]; g.drawImage(q,x,y,this); Image m=images[seed.nextInt(images.length)]; g.drawImage(m,x,y,this); }
Similar Threads
-
Problem with my first Struts program....please help me
By sireesha in forum Web FrameworksReplies: 5Last Post: 10-16-2011, 04:19 PM -
Problem with First Hibernate program
By sireesha in forum JDBCReplies: 11Last Post: 05-05-2008, 04:19 PM -
Two problem for my rssi calculation program
By iamchoilan in forum Advanced JavaReplies: 3Last Post: 04-25-2008, 03:49 PM -
Problem with my program HelloWorld
By trill in forum New To JavaReplies: 1Last Post: 08-05-2007, 05:32 PM -
getting problem in compiling java program?
By sathish04021984 in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks