The Artima Developer Community
Sponsored Link

Java Answers Forum
**Urgent** How do I animate an image in here?

5 replies on 1 page. Most recent reply: Sep 14, 2004 10:43 PM by Bringer_Of_Death

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Bringer_Of_Death

Posts: 6
Nickname: bod
Registered: Sep, 2004

**Urgent** How do I animate an image in here? Posted: Sep 14, 2004 10:37 PM
Reply to this message Reply
Advertisement
Okay I'm nearly done with my collage project but the problem is that I need to animate an image which the user has imported into the JPanel. After importing the user will click anywhere on the JPanel to set as waypoints for the image to travel. The user will then press the "play" button and the image will go from the starting point to the ending point.

Can someone here add in the function to animate that image into my source code here or at least provide instructions on how to do it?

I am posting 5 types of Java files here and they are : AnimatePanel.java , DrawPane.java, Protocol.java, Server.java and ServerThread.java.

Complile all of those and run the application through AnimatePanel.java. The Protocol.java, Server.java and ServerThread.java files should be ignored. Only The AnimatePanel.java and the DrawPane.java needs close attention.


Bringer_Of_Death

Posts: 6
Nickname: bod
Registered: Sep, 2004

Re: **Urgent** How do I animate an image in here? Posted: Sep 14, 2004 10:37 PM
Reply to this message Reply
//ServerThread.java file

import java.net.*;
import java.io.*;
public class ServerThread extends Thread
{
private Socket socket = null;
public PrintWriter out;
static ThreadGroup theClientThreads;
String clientid;
public ServerThread(Socket socket, ThreadGroup tg, String id)
{
super(tg, id);
this.socket = socket;
this.theClientThreads = tg;
this.clientid = id;
try
{
this.out = new PrintWriter(socket.getOutputStream(), true);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void sendToAll(String message)
{
int numThreads = theClientThreads.activeCount();
ServerThread[] listOfThreads = new ServerThread[numThreads];
theClientThreads.enumerate(listOfThreads);
for (int i = 0; i < numThreads; i++)
listOfThreads.sendMessage(message);
}
public void sendMessage(String message)
{
out.println(message);
}
public void run()
{
try
{
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String inputLine, outputLine;
Protocol p = new Protocol();
outputLine = p.processInput(null, clientid);
sendToAll(outputLine);
while ((inputLine = in.readLine()) != null)
{
outputLine = p.processInput(inputLine, clientid);
sendToAll(outputLine);
if (outputLine.equals("Bye"))
break;
}
out.close();
in.close();
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Bringer_Of_Death

Posts: 6
Nickname: bod
Registered: Sep, 2004

Re: **Urgent** How do I animate an image in here? Posted: Sep 14, 2004 10:38 PM
Reply to this message Reply
// Server.java file

import java.net.*;
import java.io.*;
import java.lang.*;
public class Server
{
private static ThreadGroup theClientThreads;
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
theClientThreads = new ThreadGroup("The Clients");
int id = 1;
try {
serverSocket = new ServerSocket(2222);
} catch (IOException e) {
System.err.println("Could not listen on port: 2222.");
System.exit(-1);
}
while (listening)
{
String clientid = "Client" + (new Integer(id)).toString();
ServerThread st;
(st = new ServerThread(serverSocket.accept(),
theClientThreads, clientid)).start();
st.sendToAll("A New Client has Connected");
id++;
}
serverSocket.close();
}
}

Bringer_Of_Death

Posts: 6
Nickname: bod
Registered: Sep, 2004

Re: **Urgent** How do I animate an image in here? Posted: Sep 14, 2004 10:39 PM
Reply to this message Reply
//Protocol.java file

import java.net.*;
import java.io.*;
public class Protocol
{
public String processInput(String theInput, String clientid)
{
if(theInput == null)
return("Greetings. You are connected to the Server.");
else if(theInput.equals("BYE"))
return("BYE");
else if(theInput.startsWith("DRAW "))
return(theInput);
else
return(clientid + ": " + theInput);
}
}

Bringer_Of_Death

Posts: 6
Nickname: bod
Registered: Sep, 2004

Re: **Urgent** How do I animate an image in here? Posted: Sep 14, 2004 10:40 PM
Reply to this message Reply
//DrawPane.java file

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.util.List;
import java.awt.geom.*;
import java.io.*;
import java.lang.*;

class DrawPane extends JPanel{

List shapeList;
Image image = null;
Vector lines = new Vector();
Vector drawn = new Vector();
Vector object = new Vector();

public DrawPane()
{
shapeList = new ArrayList();
setBackground(Color.white);
addMouseListener(new DotSelector(this));
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(int j = 0; j < shapeList.size(); j++)
{
g2.fill((Shape)shapeList.get(j)); // Ellipse2D
g2.draw((Shape)shapeList.get(j)); // Line2D

}

int np = lines.size();

// draw the current lines
g.setColor(getForeground());
for (int i=0; i < np; i++)
{
//System.out.println(drawn.elementAt(i).toString());
if(drawn.elementAt(i).toString().equals("i"))
g.drawImage((Image)lines.elementAt(i),0,0,this);
}
}

public List getShapeList()
{
return shapeList;
}

public void clear()
{
shapeList.clear();
lines.clear();
drawn.clear();
repaint();
}

public void loadImage(String filename)
{
image = Toolkit.getDefaultToolkit().getImage(filename);
lines.addElement(image);
drawn.addElement(new String("i"));
}



}


class DotSelector extends MouseAdapter
{
DrawPane dalPanel;
final int DIA = 8;
Point lastPoint;

public DotSelector(DrawPane dalp)
{
dalPanel = dalp;
}

public void mousePressed(MouseEvent e)
{
if(e.getClickCount() > 1)
{
dalPanel.clear();
return;
}
Point p = e.getPoint();
List list = dalPanel.getShapeList();
Ellipse2D dot = new Ellipse2D.Double(p.x - DIA/2, p.y - DIA/2, DIA, DIA);
list.add(dot);
if(list.size() > 1) // no line until after the first dot
{
Line2D line = new Line2D.Double(lastPoint.x, lastPoint.y, p.x, p.y);
list.add(line);
}
dalPanel.repaint();
lastPoint = p;
}

}

class DrawControls extends Panel implements ActionListener
{
DrawPane target;
DrawPane panel;


public DrawControls(DrawPane target)
{
this.target = target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);

Button clearall = new Button("Clear");
clearall.addActionListener(this);
add(clearall);

Button play = new Button("Play");
add(play);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Clear")){
target.clear();
}

}
}

Bringer_Of_Death

Posts: 6
Nickname: bod
Registered: Sep, 2004

Re: **Urgent** How do I animate an image in here? Posted: Sep 14, 2004 10:43 PM
Reply to this message Reply
/*
AnimatePane.java file
*/
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*; //for layout managers
import java.awt.event.*; //for action and window events
import java.io.*;
import java.net.*;
public class AnimatePanel extends JFrame implements ActionListener
{
protected static final String loadgraphicString = "LoadGraphic";
static JEditorPane editorPane;
static DrawControls dControls;
static PrintStream out;
static DrawPane onePanel;
static JMenuBar menuBar;
//add whiteboard
//contentPane.add(whiteboard);
//create the gui interface
public AnimatePanel()
{
super("My Client");

//Create an editor pane.
editorPane = new JEditorPane();
//Create whiteboard
onePanel = new DrawPane();
JPanel whiteboard = new JPanel();
whiteboard.setLayout(new BorderLayout());
dControls = new DrawControls(onePanel);
whiteboard.setPreferredSize(new Dimension(500,300));
whiteboard.add("Center",onePanel);
whiteboard.add("South", dControls);

//editorPane.setEditable(false);
JScrollPane editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setVerticalScrollBarPolicy(
JScrollPa ne.VERTICAL_SCROLLBAR_ALWAYS);
editorScrollPane.setPreferredSize(new Dimension(250, 145));
editorScrollPane.setMinimumSize(new Dimension(50, 50));
//put everything in a panel
JPanel contentPane = new JPanel();
BoxLayout box = new BoxLayout(contentPane, BoxLayout.Y_AXIS);
contentPane.setLayout(box);
//add whiteboard
contentPane.add(whiteboard);
//Create the menu bar
menuBar = new JMenuBar();
setJMenuBar (menuBar);
//Build the first menu
JMenu menu = new JMenu ("File");
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);
// a group of JMenuItems
JMenuItem menuItem = new JMenuItem("Load Graphic", KeyEvent.VK_L);
menuItem.setActionCommand(loadgraphicString);
menuItem.addActio nListener(this);
menu.add(menuItem);
setContentPane(contentPane);
}
//listen for actions being performed and process them
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(loadgraphicString))
{
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog (this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
onePanel.loadImage(file.getAbsolutePath());
}
}
}

//run the client
public static void main(String[] args) {
JFrame frame = new AnimatePanel();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
//sames as previous client,
//set up connection and then listen for messages from the Server
String socketIP = "127.0.0.1";
//the IP address of the machine where the server is running
Socket theSocket = null;
//communication line to the server
out = null;
//for message sending
BufferedReader in = null;
//for message receiving
try
{
theSocket = new Socket(socketIP, 2222);
//try to connect
out = new PrintStream(theSocket.getOutputStream());
//for client to send messages
//onePanel.out = out;
in = new BufferedReader(new
InputStreamReader(theSocket.getInputStream()));
BufferedRead er userIn = new BufferedReader(new
InputStreamReader(System.in));
String fromServer;
while ((fromServer = in.readLine()) != null)
{
//appendText(fromServer);
if (fromServer.equals("BYE"))
{
//appendText("Connection Closed");
break;
}
}
out.close(); //close all streams
in.close();
theSocket.close(); //close the socket
}
catch (UnknownHostException e) //if the socket cannot be openned
{
System.err.println("Cannot find " + socketIP);
System.exit(1);
}
catch (IOException e) //if the socket cannot be read or written
{
System.err.println("Could not make I/O connection with " + socketIP);
System.exit(1);
}
}
}



Well there's all of the code and I will be thankful if someone here could modify it so that I'll be able to animate the image. If you have the time please do it as soon as possible because I am running out of time.

Thank you.

Flat View: This topic has 5 replies on 1 page
Topic: Looking for a decent IRC and IM client in Java Previous Topic   Next Topic Topic: converting mouse icon to hour glass

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use