View Full Version : Java programmers look inside!
Slimfast
May 23rd, 2005, 06:26 PM
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!
mprielozny
May 23rd, 2005, 06:32 PM
not applet,
can you at least use swing?
Slimfast
May 23rd, 2005, 06:35 PM
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.
mprielozny
May 23rd, 2005, 06:58 PM
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();
}
mprielozny
May 23rd, 2005, 07:00 PM
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
);
}
}
}
mprielozny
May 23rd, 2005, 07:01 PM
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);
Slimfast
May 23rd, 2005, 07:08 PM
I need all that to display one lousy picture? yowch
mprielozny
May 25th, 2005, 03:51 AM
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);
chatbox
May 25th, 2005, 04:48 AM
Damn, that's still close to a dozen lines to display a pic!
fwhc022883
May 25th, 2005, 10:14 AM
Damn, that's still close to a dozen lines to display a pic!
Some of those lines are just for visual enhancements such as the setBorder and the setBounds method. I just squeeze it into 4 lines.
Slimfast
May 25th, 2005, 11:08 AM
i got it working! Thanks a lot guys!
deep
May 25th, 2005, 11:24 AM
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);
Java seems so efficient suddenly. :|
mprielozny
May 25th, 2005, 04:39 PM
Damn, that's still close to a dozen lines to display a pic!
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.
temporalillusion
May 25th, 2005, 07:26 PM
do(whatImThinking);
Nyte
May 25th, 2005, 07:46 PM
I need all that to display one lousy picture? yowch
Thats 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.
chatbox
May 25th, 2005, 10:10 PM
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.
Firstly,no, thank God for that! At this time in computing history, I don't even care about the 2000 lines from 10-15 years ago. It just doesn't contribute to the languages of today's computing.
Secondly, I do appreciate the fact that I don't have to write 2000 lines of code, but that doesn't stop me from not accecpting (or dislike) the 9 lines of code. i.e. Me appreciating something doesn't mean I can't request a further improvement. That's how technology improves over time, by people like us demanding more.
cheukiecfu
May 26th, 2005, 11:12 AM
Thats 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.and the java virtual machine is so slowwwwwww
oh well.. it's easy to run on different OS..
temporalillusion
May 26th, 2005, 12:26 PM
and the java virtual machine is so slowwwwwww
oh well.. it's easy to run on different OS..
Lol, haven't looked at Java since 1995?
Great to see decade old myths and generalizations are alive and well.
mprielozny
May 26th, 2005, 01:18 PM
and the java virtual machine is so slowwwwwww
oh well.. it's easy to run on different OS..
this is a typical reply of someone who has not done any research on it.
java is faster than gcc and definately anything microsoft comes up with.
the only faster compiler is Intel C optimized.
http://java.sys-con.com/read/45250.htm?CFID=250121&CFTOKEN=10FD8F3A-B270-12F8-D0E710D4113217C3
http://cpp.student.utwente.nl/benchmark/
as we all know memory is cheap now and as memory will get faster the faster will Java get.