473,789 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Application Structure


Hello. I am a beginner at this Java stuff, so bear with me.

I have been writing an imaging application as a way of learning Java. I

have got little bits working and tied them together in an application. But

now I am looking to tune, or correctly model, the application use threads to

improve performance.

What I am looking for is if I have structured my application in a logical

way and if I have where would it be useful to apply threads. Below is the

code of the application if someone could take a quick look and see if I have

made any major blunders with my structure and where they would apply

threads. Thanks for taking the time to read.


import java.awt.*;
import java.io.*;
import java.awt.event. *;
import java.awt.geom.* ;
import java.awt.image. *;
import java.awt.event. ActionEvent;
import java.awt.event. ActionListener;
import javax.swing.*;
import javax.swing.UIM anager;
import java.util.Linke dList;
import java.awt.image. BufferedImage;
import javax.imageio.* ;
//start of new image processor class
class newImgProc2 extends JComponent{
private BufferedImage source, destination;
public JComboBox options;
public int startX, startY, endX, endY, ix, iy, rectH, rectW;
public LinkedList lLsUndo = new LinkedList();
public int undoFlag = 0;
public JScrollPane iScroll;
public boolean swap = false;

public newImgProc2( BufferedImage image ) {//extend buffered image

to include undo handler

source = destination = image;
setBackground(C olor.white);
setLayout(new BorderLayout()) ;
// create a panel to hold the combo box
JPanel controls = new JPanel();
JPanel nuCont = new JPanel();

//use my button creator class to create a button
ButtCreator butt1 = new

ButtCreator("my Bright","graphi cs/bright.gif","gr aphics/roll.gif","Make

Brighter");
ButtCreator butt2 = new

ButtCreator("my Scale","graphic s/scale.gif","gra phics/roll.gif","Scal e

Down");
ButtCreator bLoad = new

ButtCreator("my Load","graphics/load.gif","grap hics/roll.gif","Load a File");
ButtCreator bSave = new

ButtCreator("my Save","graphics/save.gif","grap hics/roll.gif","Save a File");
ButtCreator bDark = new

ButtCreator("my Dark","graphics/dark.gif","grap hics/roll.gif","Make Darker");
ButtCreator bRot = new

ButtCreator("my Rotate","graphi cs/rotate.gif","gr aphics/roll.gif","Rota te the

image");
ButtCreator bUndo = new

ButtCreator("my Undo","graphics/undo.gif","grap hics/roll.gif","Undo ");
ButtCreator bRedo = new

ButtCreator("my Redo","graphics/redo.gif","grap hics/roll.gif","Redo ");
controls.add(bu tt1);
controls.add(bD ark);
controls.add(bu tt2);
controls.add(bL oad);
controls.add(bS ave);
controls.add(bR ot);
nuCont.add(bUnd o);
nuCont.add(bRed o);

add(controls, BorderLayout.NO RTH);
add(nuCont, BorderLayout.SO UTH);

//------
//add an event handler
//add the event listener to the object
ProcButt pB = new ProcButt();
butt1.addAction Listener(pB);
butt2.addAction Listener(pB);
bLoad.addAction Listener(pB);
bSave.addAction Listener(pB);
bDark.addAction Listener(pB);
bUndo.addAction Listener(pB);
bRedo.addAction Listener(pB);
bRot.addActionL istener(pB);
}
//undo method
public void updateUndoAr(Bu fferedImage img){
lLsUndo.add(und oFlag,img);
undoFlag = undoFlag+1;
if(lLsUndo.size ()>undoFlag){
for (int i=undoFlag;i<=l LsUndo.size();i ++){
lLsUndo.remove( i);
}
}
}

public void undoUndoAr(){
try{
System.out.prin tln("before undo "+undoFlag) ;
if(undoFlag-1>=0){
undoFlag = undoFlag-1;
}

BufferedImage last =

(BufferedImage) lLsUndo.get(und oFlag);
setTheImage(las t);
System.out.prin tln("after undo "+undoFlag) ;
}
catch( IndexOutOfBound sException e){
Toolkit.getDefa ultToolkit().be ep();
JPanel warn = new JPanel();
JOptionPane.sho wMessageDialog( warn, "ERROR: Undo

Error!\n\t\tNot hing left to undo!\n"+e);
}
}

public void redoUndoAr(){
try{
System.out.prin tln("f before redo "+undoFlag) ;
undoFlag = undoFlag+1;
System.out.prin tln("f after redo "+undoFlag) ;
BufferedImage last = (BufferedImage) lLsUndo.get(und oFlag);
setTheImage(las t);

}
catch( IndexOutOfBound sException e){
Toolkit.getDefa ultToolkit().be ep();
JPanel warn = new JPanel();
JOptionPane.sho wMessageDialog( warn, "ERROR: Redo

Error!\n\t\tNot hing more to redo!\n"+e);
}
}

//class procButt implements ActionListener, UndoableEditLis tener{
class ProcButt implements ActionListener{

public void actionPerformed (ActionEvent e){
String b = e.getActionComm and();//teh name of the

button

BufferedImageOp op = null;
if(b.equals("my Bright")){
op = new RescaleOp(1.5f, 0, null);
}
else if(b.equals("my Undo")){
undoUndoAr();
}
else if(b.equals("my Redo")){
redoUndoAr();
}
else if(b.equals("my Scale")){

op = new

AffineTransform Op(AffineTransf orm.getScaleIns tance(.75, .75), null);

}
else if(b.equals("my Load")){
getImage();
}
else if(b.equals("my Rotate")){
op = new

AffineTransform Op(AffineTransf orm.getRotateIn stance(Math.PI / 6), null);
}
else if(b.equals("my Dark")){
op = new RescaleOp(.5f, 0, null);
}
else if(b.equals("my Save")){
try{
outputFile();
}
catch(Exception exp){
System.out.prin tln(exp);
}
}

if (op != null){
updateUndoAr(de stination);
destination = op.filter(sourc e, null);
source = destination;
}
repaint();

}
}
//class to deal with painting the thing
public void paintComponent( Graphics g) {
int imageWidth = destination.get Width();
int imageHeight = destination.get Height();
int width = getSize().width ;
int height = getSize().heigh t;

g.drawImage(des tination,0, 0, null);

//start listeners

addMouseListene r(new MouseAdapter(){

public void mousePressed(Mo useEvent e){

startX = e.getX();
startY = e.getY();
}

public void mouseReleased(M ouseEvent e){
updateUndoAr(de stination);
BufferedImage my;

if(swap == true){
my = source.getSubim age(ix,iy,rectW ,rectH);
swap = false;
}
else{
my =

source.getSubim age(startX,star tY,rectW,rectH) ;
swap = false;
}

source = my;
destination = source;
repaint();
}
});

addMouseMotionL istener(new MouseMotionAdap ter(){

public void mouseDragged(Mo useEvent e){
repaint();

ix = e.getX();
iy = e.getY();

if(ix>startX){
rectW = ix - startX;
}
else{
rectW = startX - ix;
swap = true;
}

if(iy>startY){
rectH = iy - startY;
}
else{
rectH = startY - iy;
swap = true;
}

Graphics g2 = getGraphics();
if(swap == true){
g2.drawRect(ix, iy,rectW,rectH) ;
}
else{
g2.drawRect(sta rtX,startY,rect W,rectH);
}
}
});
//end list
}
// temp output file bit
public void outputFile() throws Exception{
//start of the output stuff
FileOutputStrea m fo = new FileOutputStrea m("graphics/myImg.jpg");
BufferedOutputS tream bo = new BufferedOutputS tream(fo);

ImageIO.write(d estination, "jpeg", bo);

bo.close();
//finding the size of teh image in memory
ByteArrayOutput Stream baos = new ByteArrayOutput Stream();
ImageIO.write(d estination, "jpeg", baos);

System.out.prin tln("data in mem = "+baos.size ());
baos.close();

}
// EOF temp outupt file bit

// load file from users HD method
public void getImage(){
JFileChooser dlg = new JFileChooser();
//disable the file dialog from showing all
dlg.setAcceptAl lFileFilterUsed (false);
dlg.addChoosabl eFileFilter(new DialogFilter()) ;

int r = dlg.showDialog( this, "Load Image");

if( r == JFileChooser.CA NCEL_OPTION){
return;
}
try{
File f = dlg.getSelected File();
java.net.URL url = f.toURL();
ImageIcon icn = new ImageIcon(url);

System.out.prin tln(icn.getImag eLoadStatus());

//gets the size of the local file
long sizeLocal = f.length();
System.out.prin tln("local size of file from dialog

"+sizeLocal );
Image fi = icn.getImage();

// draw the Image into a BufferedImage
int w = fi.getWidth(nul l), h = fi.getHeight(nu ll);
//buffImage is being passed to the image processor class
BufferedImage fBuff = new BufferedImage(w , h,

BufferedImage.T YPE_INT_RGB);
Graphics2D imageGraphics = fBuff.createGra phics();

setTheImage(fBu ff);
scaleTheImage(f Buff);
//end paste from main
}
catch(Exception e){
System.out.prin tln("could not load file");
}

}
//end of load file method


private Rectangle getAffectedArea (int oldX, int oldY, int newx, int newy,

int width, int height)
{
int x = Math.min(oldX, newx);
int y = Math.min(oldY, newy);
int w = (Math.max(oldX, newx) + width) - x;
int h = (Math.max(oldY, newy) + height) - y;
return new Rectangle(x, y, w, h);
}

//if the image is being sent in from an extended class, then reset

it here
public void setTheImage(Buf feredImage iImg){
source = iImg;
destination = source;
repaint();
}

public void scaleTheImage(B ufferedImage iImg){
BufferedImageOp op;

int w = iImg.getWidth() ;
int h = iImg.getHeight( );
op = new

AffineTransform Op(AffineTransf orm.getScaleIns tance(.5, .5), null);
destination = op.filter(sourc e, null);
source = destination;

}

//so an external class can get the image
public BufferedImage retTheImage(){
return source;
}

}
//end of new image processor class
//*************** *******
class outTheImage2 extends newImgProc2{
private BufferedImage theImage;

public outTheImage2(Bu fferedImage theImage){
super(theImage) ;
scaleTheImage() ;

}

public void scaleTheImage() {
//get the current image from the parent
BufferedImage thisImg = super.retTheIma ge(), destImg;
int w = thisImg.getWidt h();
int h = thisImg.getHeig ht();
BufferedImageOp op;
//transform the image
op = new AffineTransform Op(AffineTransf orm.getScaleIns tance(.5,

..5), null);
destImg = op.filter(thisI mg, null);
//set the image back to the parent
super.setTheIma ge(destImg);

}
}


//creates the frame of the whole application
class ImageProcCanvas 2 extends JFrame{

public ImageProcCanvas 2(outTheImage2 thisImage, int w, int h){

JFrame.setDefau ltLookAndFeelDe corated(true);
try{
UIManager.setLo okAndFeel("com. sun.java.swing. plaf.windows.Wi ndowsLookAndFee l

" );
}
catch(Exception e){
System.out.prin tln(e);
}
JFrame frame = new JFrame("OGRe Image Processor");

frame.getConten tPane().add(thi sImage);
int myH = h +200;
frame.setSize(w , myH);
frame.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );
frame.setVisibl e(true);

}

}
//*************** *******
public class BufferedImage00 2 {

public static void main(String args[]){
String filename = "graphics/desk001.jpg";
String outFileName = "outImg.jpg ";

// get the image from hd or could be url

ImageIcon icon = new ImageIcon(filen ame);
Image i = icon.getImage() ;
// draw the Image into a BufferedImage
int w = i.getWidth(null ), h = i.getHeight(nul l);
//buffImage is being passed to the image processor class
BufferedImage buffImage = new BufferedImage(w , h,

BufferedImage.T YPE_INT_RGB);

//create the image
Graphics2D imageGraphics = buffImage.creat eGraphics();
imageGraphics.d rawImage(i, 0, 0, null);
//get the height of the buffered image
int width = buffImage.getWi dth()+40;
int height = buffImage.getHe ight()+80;
//scale the buffered image
outTheImage2 i1 = new outTheImage2(bu ffImage);
//set up a new frame to deal with the whole app
ImageProcCanvas 2 newOgre = new ImageProcCanvas 2(i1, width, height);
}
}
//*************** *******
There are two other objects too. One for creating the buttons and one for

filtering the dialog to show only images on the users HD. I think this

maybe too much to take in in one go.. but thanks for taking the time to read

this far.

cheers
Martin

--

---------------
http://www.nonstoploop.co.uk/ - dynamic websites
http://www.rossanobacchin.be/ - fine art
http://www.weycameras.co.uk/ - photographic supplies
---------------
Jul 17 '05 #1
0 2599

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
555
by: Shantanu Bhattacharya | last post by:
Hi, I have a 2-tier application that allows the end user to create a form containing controls of the user's choice. The same user can then populate the database by entering data created using the form he created. I have to convert this 2-tier application to a 3-tier application. Since the form is created on the fly, all the information related to the controls is also stored in the database. So, there is a lot of
1
3392
by: Earl Teigrob | last post by:
Background: When I create a ASP.NET control (User or custom), it often requires security to be set for certain functionality with the control. For example, a news release user control that is comprised of a DataGrid may have separate permissions for adding, deleting and updating a news item. Problem Up until now, I have been implementing security directly inside the control. I will test directly against the security model to see if...
21
2181
by: Chris | last post by:
I'm trying to get an existing VS.NET project up on my Win2003 server and I get the following error (on the actual website page): "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS." I've been told by the previous developer that the website needs to be set as a "application". I...
6
4613
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application restarts and all active sessions are terminated. This error is also described in detail here:...
1
2628
by: Christopher A. Kelly | last post by:
I need to figure out how to control any 3rd party application by simulating mouse click and key presses. I need to take a partial window title and get the handle then be able to send the necessary input to the application, I have been trying for weeks to figure this out and can't. In trade i am posting a master volume class that i modified and added functions to. Imports System
2
2013
by: A.Carter | last post by:
I am developing a windows application with Visual Studio 2003 using C#. The application is complete so naturally I went to create a setup package. I added a setup project to the solution and I went through the appropriate steps in attaching the solution dependencies. After completing the setup package, I attempted to install the application on a co-worker's computer. Usually, the installation consist of several screens which directs the...
3
1419
by: ThunderMusic | last post by:
Hi, I use remoting in a Windows app... this remoting call a ServerA and using ServerA get an object (marshaled) from ServerB... if I call a method on the object gotten from ServerB and this ServerB has shut down it's service, I get an exception and this exception stop the application event if it's inside a try-catch structure... I mean, it does exactly the same as if it was not in a try-catch structure. Is there a reason it's behaving...
0
1299
by: markric | last post by:
Good day, I have an ASP.NET application / website with the following type of structure: + wwwroot + company site + masterpages + product1 + product2
35
2220
by: salad | last post by:
I have an application written in MS-Access. It is a complete application that manages the day-to-day operations of a business. The program is nearly ready to be used in other customer sites. I am wondering if any of you have advice on supporting an application. Since it has never had any outside exposure, what I don't want is to make a bunch of sales and not be able to support the issues that arise. I believe as kinks are worked out...
0
2215
by: =?Utf-8?B?SkhhbGV5?= | last post by:
Our system is: IIS Server: dual Intel Xeon 2.80 GHz, 4 GB Ram Windows Server 2003 SP2 IIS 6.0 SQL Server: dual Intel Xeon 2.80 GHz, 4 GB Ram (separate server) Windows Server 2003 SP2 SQL Server 2000 We are having some problems with a website we are developing, and had some
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.