Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
Need to be able to have center panel change images
Damian Zabielski
Greenhorn
Posts: 2
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; import javax.imageio.ImageIO; import javax.swing.*; import java.io.IOException; import java.awt.image.BufferedImage; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; /* * * */ public class DVPWS{ public static void main(String[] args) throws IOException{ int choice=1; final String a="Pedestrian_Button.jpg"; final String b="Vehicle_Button.jpg"; final String c="Boat_Button.jpg"; final String d="Move_Button.jpg"; Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); double screenWidth = dim.getWidth(); double screenHeight = dim.getHeight(); double newScreenHeight=(.75*screenHeight); double newScreenWidth=(.75*screenWidth); int newScreenWidth1=(int) newScreenWidth; int newScreenHeight1=(int) newScreenHeight; JPanel panel = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JPanel panel4 = new JPanel(); final JPanel panel5=new JPanel(); panel5.setLayout(new BorderLayout()); PicturePanel p=new PicturePanel(d); final PicturePanel p1=new PicturePanel(c); PicturePanel p2=new PicturePanel(b); PicturePanel p3=new PicturePanel(a); //panel5.getScaledInstance(newScreenWidth1-600, newScreenHeight1-400, 1); // String path = "Pedestrian_Button.jpg"; // BufferedImage panel5 = ImageIO.read(new File(path)); // ImageAnimation panel5 = new ImageAnimation(image); panel.setPreferredSize(new Dimension(300, 300)); panel2.setPreferredSize(new Dimension(300, 300)); panel3.setPreferredSize(new Dimension(100, 100)); panel4.setPreferredSize(new Dimension(300, 300)); //**************************************************** JMenuBar menuBar; JMenu menu; JMenu menu2; JMenuItem menuItem; JMenuItem menuItem2; JMenuItem menuItem3; JRadioButtonMenuItem rbMenuItem; JCheckBoxMenuItem cbMenuItem; //Create the menu bar. menuBar = new JMenuBar(); //Build the first menu. menu = new JMenu("Menu"); menu.setMnemonic(KeyEvent.VK_A); menu.getAccessibleContext().setAccessibleDescription( "The only menu in this program that has menu items"); menuBar.add(menu); //a group of JMenuItems menuItem2 = new JMenuItem("Options", KeyEvent.VK_T); menuItem2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); menuItem2.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menu.add(menuItem2); menuItem = new JMenuItem("Exit"); menuItem.setMnemonic(KeyEvent.VK_B); menu.add(menuItem); menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); //Build second menu in the menu bar. menu2 = new JMenu("Help"); menu2.setMnemonic(KeyEvent.VK_N); menuBar.add(menu2); menuItem3 = new JMenuItem("Info"); menu2.add(menuItem3); menuItem3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, "Operate the DVPW System by pressing the toggle buttons to watch how the program works."); } }); //**************************************************** //MyCanvas my=new MyCanvas(); //MyCanvas2 my2=new MyCanvas2(); //my.setBackground(Color.orange); //panel5.add(my,BorderLayout.CENTER); //panel5.add(my2,BorderLayout.EAST); JButton ped= new JButton("Pedestrian"); JButton veh=new JButton("Vehicle Traffic"); JButton boat=new JButton("Boat Traffic"); JButton move=new JButton("Move"); panel2.setLayout(new GridLayout(5,1,5,5)); panel4.setLayout(new GridLayout(5,1,5,5)); panel2.add(new JLabel("Control Unit")); panel2.add(ped); ped.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { PicturePanel p1=new PicturePanel("Pedestrian_Button.jpg"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } panel5.repaint(); } }); panel2.add(veh); veh.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, "Operate the DVPW System by pressing the toggle buttons to watch how the program works."); } }); panel2.add(boat); boat.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, "Operate the DVPW System by pressing the toggle buttons to watch how the program works."); } }); panel2.add(move); move.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, "Operate the DVPW System by pressing the toggle buttons to watch how the program works."); } }); panel4.add(new JLabel("Test Unit")); panel4.add(new JButton("Pedestrian")); panel4.add(new JButton("Warning Lights")); panel4.add(new JButton("Gates")); panel4.add(new JButton("Draw Bridge")); panel.setBackground(Color.black); panel2.setBackground(Color.blue); panel3.setBackground(Color.green); panel4.setBackground(Color.red); JFrame frame = new JFrame("Drawbridge, Vehicle, and Pedestrian Walkway System"); frame.setSize(newScreenWidth1, newScreenHeight1); // Set the frame size frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout() ); frame.setResizable(false); frame.setVisible(true); frame.add(menuBar, BorderLayout.NORTH); frame.add(panel2, BorderLayout.EAST); frame.add(panel3, BorderLayout.SOUTH); frame.add(panel4, BorderLayout.WEST); frame.add(panel5, BorderLayout.CENTER); //********************************************************* panel.setVisible(true); panel2.setVisible(true); panel3.setVisible(true); panel4.setVisible(true); panel5.setVisible(true); } } import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; class PicturePanel extends JPanel{ private BufferedImage b ; public PicturePanel(String a) throws IOException{ Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); double screenWidth = dim.getWidth(); double screenHeight = dim.getHeight(); double newScreenHeight=(.75*screenHeight); double newScreenWidth=(.75*screenWidth); int newScreenWidth1=(int) newScreenWidth; int newScreenHeight1=(int) newScreenHeight; b=ImageIO.read(new File(a)); this.setMinimumSize(new Dimension(newScreenWidth1-800,newScreenHeight1-400)); } protected void paintComponent(Graphics g){ Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); double screenWidth = dim.getWidth(); double screenHeight = dim.getHeight(); double newScreenHeight=(.75*screenHeight); double newScreenWidth=(.75*screenWidth); int newScreenWidth1=(int) newScreenWidth; int newScreenHeight1=(int) newScreenHeight; super.paintComponents(g); Graphics2D g2=(Graphics2D)g; g2.drawImage(b, 0, 0, newScreenWidth1-600, newScreenHeight1-150, null); } }
Damian Zabielski
Greenhorn
Posts: 2
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The problem lies in that I do not know how to have each of the right buttons make a new image appear in the center
Paul Clapham
Sheriff
Posts: 28371
99
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Okay, but which part of that are you having a problem with? There are two parts to that question:
How to make a button cause something to happen
How to make an image appear somewhere
Friends help you move. Good friends help you move bodies. This tiny ad will help:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Question about layout
Query on ProgressBar(Help plz)
help on reading textfield on 1 class from another class and using it to create a file
unable to remove spaces between buttons(making a calculator)
JPopupMenu or JMenu - neither is quite what I'm after.
More...