not applet,
can you at least use swing?
-
May 23rd, 2005 06:26 PM #1Deal Addict




- Join Date
- May 24th, 2004
- Location
- Victoria
- Posts
- 1,196
Java programmers look inside!
Hi, for the programming course at school I need to figure out how to display a jpg image in java in an application, not applet, at the end of a game that I wrote. Can anyone help me out? thanks!
_______________
I hope Life isn't one big joke because I don't get it.
Reply With Quote
LOG IN TO THANK
No one has yet thanked Slimfast for this post.
-
Sponsored Links - Join the RedFlagDeals.com community and remove this ad.
-
May 23rd, 2005 06:32 PM #2Member
- Join Date
- Apr 12th, 2004
- Location
- Victoria BC
- Posts
- 248
Reply With Quote
LOG IN TO THANK
No one has yet thanked mprielozny for this post.
-
May 23rd, 2005 06:35 PM #3Deal Addict




- Join Date
- May 24th, 2004
- Location
- Victoria
- Posts
- 1,196
i'm ******** at swing....(I can only do really basic level programming) but can you post some demo code? the stuff i find on google won't compile..maybe i'm doing something wrong. I'm using ConTEXT and compiling stuff through there.
_______________
I hope Life isn't one big joke because I don't get it.
Reply With Quote
LOG IN TO THANK
No one has yet thanked Slimfast for this post.
-
May 23rd, 2005 06:58 PM #4Member
- Join Date
- Apr 12th, 2004
- Location
- Victoria BC
- Posts
- 248
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
/**
* Creates a new Display GUI.
*/
public class DisplayGUI extends JFrame {
JLabel arrayStatus;
JLabel clustersStatus;
JLabel sensorsStatus;
JLabel averageStatus;
JCheckBox showClusters;
JRadioButton Topology;
JRadioButton Temperature;
JRadioButton Moisture;
JRadioButton Sunlight;
JMenuItem[] TTMSMenuItem;
JScrollPane scrollPane;
Grapher graph;
JPanel graphPanel;
topologyPane topoPane;
ButtonGroup selectionGroup = new ButtonGroup();
Controller parent;
/**
* Creates a Display GUI with the given controller
* @param parent the controller
*/
public DisplayGUI(Controller parent) {
this.parent = parent;
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
show();
}
/*
init the GUI
*/
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
this.setResizable(false);
this.setTitle("Seng 330 - PROJECT");
this.setBounds(new java.awt.Rectangle(22, 22, 577,415));//);577, 407));
this.setIconImage(new ImageIcon("resources/icon_thing.gif").getImage());
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width-getWidth())/2, (d.height-getHeight())/2);
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
//=================================================
JMenuItem item = new JMenuItem("New Deployment");
item.setMnemonic(KeyEvent.VK_N);
item.addActionListener(parent.getListener());
menu.add(item);
//=================================================
item = new JMenuItem("Exit");
item.setMnemonic(KeyEvent.VK_E);
item.addActionListener(new ExitListener());
menu.add(item);
// ---------------------------------------------
JMenu menu2 = new JMenu("Settings");
menu.setMnemonic(KeyEvent.VK_S);
//=================================================
TTMSMenuItem = new JMenuItem[4];
TTMSMenuItem[0] = new JMenuItem("Topology");
TTMSMenuItem[0].setMnemonic(KeyEvent.VK_T);
TTMSMenuItem[0].addActionListener(parent.getListener());
menu2.add(TTMSMenuItem[0]);
//=================================================
TTMSMenuItem[1] = new JMenuItem("Temperature");
TTMSMenuItem[1].setMnemonic(KeyEvent.VK_P);
TTMSMenuItem[1].addActionListener(parent.getListener());
menu2.add(TTMSMenuItem[1]);
//=================================================
TTMSMenuItem[2] = new JMenuItem("Moisture");
TTMSMenuItem[2].setMnemonic(KeyEvent.VK_M);
TTMSMenuItem[2].addActionListener(parent.getListener());
menu2.add(TTMSMenuItem[2]);
//=================================================
TTMSMenuItem[3] = new JMenuItem("Sunlight");
TTMSMenuItem[3].setMnemonic(KeyEvent.VK_S);
TTMSMenuItem[3].addActionListener(parent.getListener());
menu2.add(TTMSMenuItem[3]);
// ---------------------------------------------
JMenu menu3 = new JMenu("Help");
menu.setMnemonic(KeyEvent.VK_H);
//=================================================
item = new JMenuItem("About");
item.setMnemonic(KeyEvent.VK_A);
item.addActionListener(new AboutListener());
menu3.add(item);
// ---------------------------------------------
//=================================================
JMenuBar menuBar = new JMenuBar();
menuBar.setMargin(new Insets(5,5,5,5));
menuBar.add(menu);
menuBar.add(menu2);
menuBar.add(menu3);
this.setJMenuBar(menuBar);
// ---------------------------------------------
//=================================================
JLabel jLabel1 = new JLabel();
jLabel1.setText(" Current Graph System ");
jLabel1.setOpaque(true);
getContentPane().add(jLabel1);
jLabel1.setBounds(24, -3, 125, 16);
JPanel jPanel1 = new JPanel();
jPanel1.setBorder(new javax.swing.border.EtchedBorder());
jPanel1.setBounds(4, 4, 400, 330);
getContentPane().add(jPanel1);
graphPanel = new JPanel();
graphPanel.setLayout(null);
graphPanel.setVisible(false);
graphPanel.setBounds(new java.awt.Rectangle(5, 100, 350 , 200));
graph = new Grapher();
graph.setVisible(false);
graph.setXtag("-- Time --");
graph.setBounds(new java.awt.Rectangle(0, 0, 350 , 200));
graphPanel.add(graph);
topoPane = new topologyPane(null);
topoPane.setVisible(true);
scrollPane = new JScrollPane(scrollPane.VERTICAL_SCROLLBAR_AS_NEEDE D,scrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(395,320));
scrollPane.setViewportView(graphPanel);
scrollPane.setViewportView(topoPane);
scrollPane.add(graphPanel);
jPanel1.add(scrollPane);
// ---------------------------------------------
//=================================================
jLabel1 = new JLabel();
jLabel1.setText(" Display Options ");
jLabel1.setOpaque(true);
jLabel1.setBounds(424,-3, 95, 16);
getContentPane().add(jLabel1);
showClusters = new JCheckBox();
showClusters.setText("Show Cluster Header");
showClusters.setFocusPainted(false);
showClusters.setSelected(true);
showClusters.addActionListener(parent.getListener( ));
getContentPane().add(showClusters,0);
showClusters.setBounds(416, 15, 150, 24);
Topology= new JRadioButton("Topology");
Topology.setBounds(416, 42, 150, 24);
selectionGroup.add(Topology);
Topology.setFocusPainted(false);
Topology.setSelected(true);
Topology.addActionListener(parent.getListener());
getContentPane().add(Topology,0);
Temperature= new JRadioButton("Temperature");
Temperature.setBounds(416, 62, 150, 24);
selectionGroup.add(Temperature);
Temperature.setFocusPainted(false);
Temperature.addActionListener(parent.getListener() );
getContentPane().add(Temperature,0);
Moisture= new JRadioButton("Moisture");
Moisture.setBounds(416, 82, 150, 24);
selectionGroup.add(Moisture);
Moisture.setFocusPainted(false);
Moisture.addActionListener(parent.getListener());
getContentPane().add(Moisture,0);
Sunlight= new JRadioButton("Sunlight");
Sunlight.setBounds(416, 102, 150, 24);
selectionGroup.add(Sunlight);
Sunlight.setFocusPainted(false);
Sunlight.addActionListener(parent.getListener());
getContentPane().add(Sunlight,0);
JPanel jPanel2 = new JPanel();
jPanel2 = new JPanel();
jPanel2.setBorder(new javax.swing.border.EtchedBorder());
jPanel2.setBounds(408, 4, 160, 155);
getContentPane().add(jPanel2);
// ---------------------------------------------
jLabel1 = new JLabel();
jLabel1.setText(" Legends ");
jLabel1.setOpaque(true);
getContentPane().add(jLabel1);
jLabel1.setBounds(424, 155, 60, 16);
jPanel1 = new JPanel();
jPanel1.setBorder(new javax.swing.border.EtchedBorder());
jPanel1.setBounds(408, 164, 160, 170);
getContentPane().add(jPanel1);
jLabel1 = new JLabel(new ImageIcon("resources/legend.png"));//new ImageIcon("resources/icon_thing.gif").getImage());
jLabel1.setBounds(0, 0, 200, 200);
jLabel1.setVisible(true);
jPanel1.add(jLabel1);
arrayStatus = new JLabel();
arrayStatus.setText("80x80");
arrayStatus.setBorder(new javax.swing.border.EtchedBorder());
arrayStatus.setBounds(4, 336, 60, 22);
arrayStatus.setHorizontalAlignment(javax.swing.Swi ngConstants.CENTER);
getContentPane().add(arrayStatus);
clustersStatus = new JLabel();
clustersStatus.setText("#Clusters = 415");
clustersStatus.setBorder(new javax.swing.border.EtchedBorder());
clustersStatus.setBounds(68, 336, 110, 22);
clustersStatus.setHorizontalAlignment(javax.swing. SwingConstants.CENTER);
getContentPane().add(clustersStatus);
sensorsStatus = new JLabel();
sensorsStatus.setText("#Sensors = 2415");
sensorsStatus.setBorder(new javax.swing.border.EtchedBorder());
sensorsStatus.setBounds(182, 336, 150, 22);
sensorsStatus.setHorizontalAlignment(javax.swing.S wingConstants.CENTER);
getContentPane().add(sensorsStatus);
averageStatus = new JLabel();
averageStatus.setText("Average = 15 | Max = 25 | Min = 10");
averageStatus.setBorder(new javax.swing.border.EtchedBorder());
averageStatus.setBounds(336, 336, 232, 22);
averageStatus.setHorizontalAlignment(javax.swing.S wingConstants.CENTER);
getContentPane().add(averageStatus);
this.setVisible(true);
this.repaint();
}
Reply With Quote
LOG IN TO THANK
No one has yet thanked mprielozny for this post.
-
May 23rd, 2005 07:00 PM #5Member
- Join Date
- Apr 12th, 2004
- Location
- Victoria BC
- Posts
- 248
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
cancel();
System.exit(0);
}
super.processWindowEvent(e);
}
/**
* Used to dipose of GUI
*/
void cancel() {
dispose();
}
/**
* Set the status bar with the default values.
*/
public void setBottomInfo(){
averageStatus.setText("Average = n/a | Max = n/a | Min = n/a");
Preferences p = Preferences.getInstance();
sensorsStatus.setText("#Sensors = "+p.getClusterCount()*p.getSensorsPerHead());
clustersStatus.setText("#Clusters = "+p.getClusterCount());
int cols=p.getPhysicalWidth()/ p.getClusterCoverageLength();
int rows=p.getClusterCount()/ cols;
if (p.getClusterCount()% cols!=0)rows++;
arrayStatus.setText(cols+"x"+rows);
}
public void setAverageStatus(int avg,int max,int min){
averageStatus.setText("Average = "+avg+" | Max = "+max+" | Min = "+min);
}
/**
* Set the view to the topologyPane or grapher depending on topoPaneOn value
* @param topoPaneOn set topologyPane visible is true else false
*/
public void setTopology(boolean topoPaneOn){
graphPanel.setVisible(!topoPaneOn);
graph.setVisible(!topoPaneOn);
parent.redraw_topology();
topoPane.setVisible(topoPaneOn);
scrollPane.getHorizontalScrollBar().setEnabled(top oPaneOn);
scrollPane.getHorizontalScrollBar().setVisible(top oPaneOn);
scrollPane.getVerticalScrollBar().setEnabled(topoP aneOn);
scrollPane.getVerticalScrollBar().setVisible(topoP aneOn);
}
class ExitListener implements ActionListener{
public void actionPerformed(ActionEvent ae){System.exit(0);}
}
class AboutListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
JOptionPane.showMessageDialog(
null,
"Date:March 28,2004\n"+
"Thank you\n\n"
,
"About",
JOptionPane.INFORMATION_MESSAGE
);
}
}
}
Reply With Quote
LOG IN TO THANK
No one has yet thanked mprielozny for this post.
-
May 23rd, 2005 07:01 PM #6Member
- Join Date
- Apr 12th, 2004
- Location
- Victoria BC
- Posts
- 248
especially look for something like
jPanel1 = new JPanel();
jPanel1.setBorder(new javax.swing.border.EtchedBorder());
jPanel1.setBounds(408, 164, 160, 170);
getContentPane().add(jPanel1);
jLabel1 = new JLabel(new ImageIcon("resources/legend.png"));//new ImageIcon("resources/icon_thing.gif").getImage());
jLabel1.setBounds(0, 0, 200, 200);
jLabel1.setVisible(true);
jPanel1.add(jLabel1);
Reply With Quote
LOG IN TO THANK
No one has yet thanked mprielozny for this post.
-
May 23rd, 2005 07:08 PM #7Deal Addict




- Join Date
- May 24th, 2004
- Location
- Victoria
- Posts
- 1,196
I need all that to display one lousy picture? yowch
_______________
I hope Life isn't one big joke because I don't get it.
Reply With Quote
LOG IN TO THANK
No one has yet thanked Slimfast for this post.
-
May 25th, 2005 03:51 AM #8Member
- Join Date
- Apr 12th, 2004
- Location
- Victoria BC
- Posts
- 248
definately not

tihs is enough
jPanel1 = new JPanel();
jPanel1.setBorder(new javax.swing.border.EtchedBorder());
jPanel1.setBounds(408, 164, 160, 170);
getContentPane().add(jPanel1);
jLabel1 = new JLabel(new ImageIcon("resources/legend.png"));//new ImageIcon("resources/icon_thing.gif").getImage());
jLabel1.setBounds(0, 0, 200, 200);
jLabel1.setVisible(true);
jPanel1.add(jLabel1);
Reply With Quote
LOG IN TO THANK
No one has yet thanked mprielozny for this post.
-
May 25th, 2005 04:48 AM #9
Damn, that's still close to a dozen lines to display a pic!
Reply With Quote
LOG IN TO THANK
No one has yet thanked chatbox for this post.
-
May 25th, 2005 10:14 AM #10Some of those lines are just for visual enhancements such as the setBorder and the setBounds method. I just squeeze it into 4 lines.
Originally Posted by chatbox
Reply With Quote
LOG IN TO THANK
No one has yet thanked fwhc022883 for this post.
-
May 25th, 2005 11:08 AM #11Deal Addict




- Join Date
- May 24th, 2004
- Location
- Victoria
- Posts
- 1,196
i got it working! Thanks a lot guys!
_______________
I hope Life isn't one big joke because I don't get it.
Reply With Quote
LOG IN TO THANK
No one has yet thanked Slimfast for this post.
-
May 25th, 2005 11:24 AM #12Java seems so efficient suddenly.
Originally Posted by mprielozny
_______________
Heatware 50 Positive, 0 Negative
Reply With Quote
LOG IN TO THANK
No one has yet thanked deep for this post.
-
May 25th, 2005 04:39 PM #13Member
- Join Date
- Apr 12th, 2004
- Location
- Victoria BC
- Posts
- 248
Originally Posted by chatbox
well you can go this
getContentPane().add(new JLabel(new ImageIcon("resources/legend.png")));
and i guess you never used any of the real programming languages like 10-15 years ago when displaying JPG took close to 2000 lines.
now they make it too easy for newbies they dont appriciate how easy they have it.
Reply With Quote
LOG IN TO THANK
No one has yet thanked mprielozny for this post.
-
May 25th, 2005 07:26 PM #14
Reply With Quote
LOG IN TO THANK
No one has yet thanked temporalillusion for this post.
-
May 25th, 2005 07:46 PM #15Thats how java is. If you want to do this stuff with less code, choose a different language, that or find some java libraries that will make it easier.
Originally Posted by Slimfast
Reply With Quote
LOG IN TO THANK
No one has yet thanked Nyte for this post.
Search Forums


