3 Replies - 641 Views - Last Post: 14 October 2012 - 07:18 PM Rate Topic: -----

#1 qafarova  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-October 12

java gui domino code and algorithms

Posted 14 October 2012 - 12:43 PM

this code public class {BufferedImageMouseDrag extends JFrame} error ,but I do not known this error.And my assignment name "domino",please give me "domino" code in java (gui)

import java.awt.Color;
	import java.awt.Container;
	import java.awt.Graphics;
	import java.awt.Graphics2D;
	import java.awt.Image;
	import java.awt.MediaTracker;
	import java.awt.event.MouseEvent;
	import java.awt.event.MouseMotionAdapter;
	import java.awt.event.WindowAdapter;
	import java.awt.event.WindowEvent;
	import java.awt.image.BufferedImage;
	 
	import javax.swing.JFrame;
	import javax.swing.JPanel;
	import javax.swing.border.TitledBorder;

	 /**
 *
 * @author lala
 */public class main{
          public class BufferedImageMouseDrag extends JFrame{
	  DisplayCanvas canvas;
	 
	  public BufferedImageMouseDrag() {
	    super();
    Container container = getContentPane();
	 
	    canvas = new DisplayCanvas();
	    container.add(canvas);
	 
	    addWindowListener(new WindowAdapter() {
	      public void windowClosing(WindowEvent e) {
	        System.exit(0);
	      }
	    });
	    setSize(450, 400);
    setVisible(true);
	  }
	 
	  public static void main(String arg[]) {
	    new BufferedImageMouseDrag();
	  }
	}
	class DisplayCanvas extends JPanel {
	  int x = 100;
	  int y = 300;
	 
	 
	  BufferedImage bi;
	 
	  DisplayCanvas() {
	    setBackground(Color.white);
	    setSize(450, 400);
	    addMouseMotionListener(new MouseMotionHandler());
	 
	    Image image = getToolkit().getImage("C:\\Documents and Settings\\%username%\\My Documents\\Java\\GUI Stuff\\src\\dominoe.gif");
	 
	    MediaTracker mt = new MediaTracker(this);
	    mt.addImage(image, 1);
	    try {
	      mt.waitForAll();
	    } catch (Exception e) {
	      System.out.println("Exception while loading image.");
	    }
	 
	    if (image.getWidth(this) == -1) {
      System.out.println("no gif file");
	      System.exit(0);
	    }
	 
	    bi = new BufferedImage(image.getWidth(this), image.getHeight(this),
	        BufferedImage.TYPE_INT_ARGB);
	    Graphics2D big = bi.createGraphics();
	    big.drawImage(image, 0, 0, this);
	  }
	 
	  public void paintComponent(Graphics g) {
	    super.paintComponent(g);
	    Graphics2D g2D = (Graphics2D) g;
	 
	    g2D.drawImage(bi, x, y, this);
	  }
	 
	  class MouseMotionHandler extends MouseMotionAdapter {
	    public void mouseDragged(MouseEvent e) {
	      x = e.getX()- 13;
	      y = e.getY() - 25;
	      repaint();
	    }
	  }
}}


*Edited: please :code:

This post has been edited by pbl: 14 October 2012 - 07:12 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: java gui domino code and algorithms

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: java gui domino code and algorithms

Posted 14 October 2012 - 01:22 PM

Please post your code in code tags and let us know what you need help with.
Was This Post Helpful? 1
  • +
  • -

#3 qafarova  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-October 12

Re: java gui domino code and algorithms

Posted 14 October 2012 - 02:09 PM

View PostGregBrannon, on 14 October 2012 - 01:22 PM, said:

Please post your code in code tags and let us know what you need help with.


I not domino code so I want help me(I live in Azerbaijan,Sorry I have bad english )
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java gui domino code and algorithms

Posted 14 October 2012 - 07:18 PM

Works for me and give this correctly indented
P.S. learn to put your different classes in different .java files

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class BufferedImageMouseDrag extends JFrame{
	DisplayCanvas canvas;

	public BufferedImageMouseDrag() {
		super();
		Container container = getContentPane();

		canvas = new DisplayCanvas();
		container.add(canvas);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		setSize(450, 400);
		setVisible(true);
	}

	public static void main(String arg[]) {
		new BufferedImageMouseDrag();
	}
}
class DisplayCanvas extends JPanel {
	int x = 100;
	int y = 300;


	BufferedImage bi;

	DisplayCanvas() {
		setBackground(Color.white);
		setSize(450, 400);
		addMouseMotionListener(new MouseMotionHandler());

		Image image = getToolkit().getImage("C:\\Documents and Settings\\%username%\\My Documents\\Java\\GUI Stuff\\src\\dominoe.gif");

		MediaTracker mt = new MediaTracker(this);
		mt.addImage(image, 1);
		try {
			mt.waitForAll();
		} catch (Exception e) {
			System.out.println("Exception while loading image.");
		}

		if (image.getWidth(this) == -1) {
			System.out.println("no gif file");
			System.exit(0);
		}

		bi = new BufferedImage(image.getWidth(this), image.getHeight(this),
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D big = bi.createGraphics();
		big.drawImage(image, 0, 0, this);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2D = (Graphics2D) g;

		g2D.drawImage(bi, x, y, this);
	}

	class MouseMotionHandler extends MouseMotionAdapter {
		public void mouseDragged(MouseEvent e) {
			x = e.getX()- 13;
			y = e.getY() - 25;
			repaint();
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1