Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Updating Garphics of the JPanel

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Updating Garphics of the JPanel

    package main;
     
    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.util.*;
     
    public class Window {
     
    	static Color Soviet = new Color(176, 23, 31); //Red
    	static Color Union = new Color(205, 173, 0); //Yellow
    	static JFrame window;
    	static JPanel panel;
    	static GroupLayout layout;
    	static String KGB1 = "Komitet Gosudarstevenno Bezopasnosti";
    	static String KGB2 = "KGB - Instant Messaging";
    	static String KGB3 = "Dedicated to Julia the Russian";
    	static JLabel Title1, Title2, Title3, Title4;
    	static Calendar time;
    	static boolean loop1 = true;
    	static Graphics2D g2D;
     
    	static public String Time(){
    		time = Calendar.getInstance();
    		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    		return sdf.format(time.getTime());
    	}
     
    	static public void Update(){
    		panel.revalidate();
     
    		Title4 = new JLabel(Time());
    		Title4.setForeground(Union);
    		Title4.setBounds(10, 70, 50, 20);
    		panel.add(Title4);
    	}
     
    	static public void Clear(Graphics g){
    		g2D = (Graphics2D) g;
    		g2D.fillRect(0, 0, 400, 400);
    	}
     
    	public static void main(String s[]){
     
    		window = new JFrame("Instant Messaging v1.0");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setSize(400, 400);
    		window.setResizable(false);
    		window.setVisible(true);
     
    		panel = new JPanel();
    		panel.setSize(window.getSize());
    		panel.setBackground(Soviet);
    		panel.setLayout(null);
    		window.add(panel);
     
    		Title1 = new JLabel(KGB1);
    		Title1.setForeground(Union);
    		Title1.setBounds(75, 10, 250, 20);
    		panel.add(Title1);
     
    		Title2 = new JLabel(KGB2);
    		Title2.setForeground(Union);
    		Title2.setBounds(125, 30, 200, 20);
    		panel.add(Title2);
     
    		Title3 = new JLabel(KGB3);
    		Title3.setForeground(Union);
    		Title3.setBounds(110, 50, 200, 20);
    		panel.add(Title3);
     
    		while(loop1){
    			Update();
    			Clear(g2D);
    			panel.repaint();
    		}
    	}
    }

    I'm making a small chat program for my friend with encrypting and decrypting messages. Right now I'm just creating the GUI and I will add the TCP later. I can't figure out how to update the String Time() without the label overlapping the last String. Thanks for the help.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Updating Garphics of the JPanel

    Well in your update method you're constantly creating a new JLabel instead of "updating" one. That could be the culprit.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Updating Garphics of the JPanel

    I fixed the problem:

    package main;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import java.text.*;
    import java.util.*;
     
    public class Window {
     
    	static JFrame window;
    	static Panels panel;
    	static Paint paint;
    	static boolean loop1 = true;
    	static boolean Login = true;
    	static boolean Join = false;
    	static boolean Create = false;
    	static boolean Friends = false;
    	static boolean Account = false;
    	static boolean Settings = false;
    	static Color red = new Color(176, 23, 31);
    	static Color yellow = new Color(205, 173, 0); 
     
    	static public void Frame(){
    		window = new JFrame("Instant Messaging v1.0");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setSize(400, 400);
    		window.setResizable(false);
    		window.setLocationRelativeTo(null);
    		window.setVisible(true);
    	}
     
    	static public void State(){
    		panel = new Panels();
    		window.add(panel.Login());
    		window.setJMenuBar(panel.Menu());
    	}
     
    	public static void main(String s[]){
    		paint = new Paint();
    		Frame();
    		State();
     
    		while(loop1){
    			panel.Time();
    		}
    	}
    }

    package main;
     
    import java.awt.*;
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
    public class Paint extends JPanel{
     
    	@Override
    	public void paint(Graphics g){
    		super.paint(g);
    		g.setClip(0, 0, 400, 400);
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.setColor(new Color(205, 173, 0));
    		g2d.fillRect(0,0,200, 200);
    	}
    }

    The program is compiled without an error, but the yellow rectangle that should occupy 1/4 of the panel is not painting onto the panel.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Updating Garphics of the JPanel

    You will need to post the missing class Panels() to be sure, but I do not see where the Paint() paint is ever added to a container. I also see that you are setting the JFrame visible before adding all of it's contents. Be sure everything has been added prior to calling setVisible(true) on the frame.
    Methods should begin with a lower case letter.
    Paint, Window, and Frame are not very well chosen class names to be used in Java.

    If you have any more questions post enough code to compile so we can see what is going on instead of guessing.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Updating Garphics of the JPanel

    Sorry, should have known to do that. Thanks for the tip, I'm changing the method names right now. I don't want to add the graphics into a container, I thought I could just paint onto the panel itself, but I'm not sure.

    package main;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import java.text.*;
    import java.util.*;
     
    public class Window {
     
    	static JFrame window;
    	static Panels panel;
    	static Paint paint;
    	static boolean loop1 = true;
    	static boolean Login = true;
    	static boolean Join = false;
    	static boolean Create = false;
    	static boolean Friends = false;
    	static boolean Account = false;
    	static boolean Settings = false;
    	static Color red = new Color(176, 23, 31);
    	static Color yellow = new Color(205, 173, 0); 
     
    	static public void Frame(){
    		window = new JFrame("Instant Messaging");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setSize(400, 400);
    		window.setResizable(false);
    		window.setLocationRelativeTo(null);
    		window.setVisible(true);
    	}
     
    	static public void State(){
    		panel = new Panels();
    		window.add(panel.Login());
    		window.setJMenuBar(panel.Menu());
    	}
     
    	public static void main(String s[]){
    		paint = new Paint();
    		Frame();
    		State();
     
    		while(loop1){
    			panel.Time();
    		}
    	}
    }

    package main;
     
    import java.awt.*;
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
    public class Paint extends JPanel{
     
    	static final long serialVersionUID = 1L;
     
    	/*
    	@Override
    	public void paint(Graphics g){
    		super.paint(g);
    		g.setClip(0, 0, 400, 400);
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.setColor(new Color(205, 173, 0));
    		g2d.fillRect(0,0,200, 200);
    	}
    	*/
    }

    package main;
     
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.*;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
     
    import javax.swing.*;
     
    public class Panels {
     
    	static Calendar time;
    	JPanel Login, Join, Create, Friends, Account, Settings;
    	JLabel Label1, Label2, Label3, Label4, Label5, Label6, Time;
    	JButton Button1, Button2;
    	JTextField TextField1, TextField2;
    	static JMenuBar menuBar;
    	static JMenu menu1, menu2;
    	static JMenuItem item1, item2, item3, item4, item5, item6;
    	static String String1 = "Komitet Gosudarstevenno Bezopasnosti";
    	static String String2 = "KGB - Instant Messaging";
    	static String String3 = "Dedicated to Julia the Russian";
    	static Color red = new Color(176, 23, 31);
    	static Color yellow = new Color(205, 173, 0); 
     
    	public JPanel Login(){
    		Login = new JPanel();
    		Login.setBackground(red);
    		Login.setLayout(null);
    		Login.setOpaque(true);
    		Login.setSize(400, 400);
     
    		//Labels
    		Label1 = new JLabel(String1);
    		Label1.setForeground(yellow);
    		Label1.setBounds(75, 10, 250, 20);
    		Login.add(Label1);
     
    		Label2 = new JLabel(String2);
    		Label2.setForeground(yellow);
    		Label2.setBounds(125, 30, 200, 20);
    		Login.add(Label2);
     
    		Label3 = new JLabel(String3);
    		Label3.setForeground(yellow);
    		Label3.setBounds(110, 50, 200, 20);
    		Login.add(Label3);
     
    		Label4 = new JLabel("Username:");
    		Label4.setForeground(yellow);
    		Label4.setBounds(30, 90, 100, 20);
    		Login.add(Label4);
     
    		Label5 = new JLabel("Password:");
    		Label5.setForeground(yellow);
    		Label5.setBounds(160, 90, 100, 20);
    		Login.add(Label5);
     
    		Label6 = new JLabel("By -- v1.0");
    		Label6.setForeground(yellow);
    		Label6.setBounds(10, 320, 160, 20);
    		Login.add(Label6);
     
    		Time = new JLabel();
    		Time.setForeground(yellow);
    		Time.setBounds(340, 320, 50, 20);
    		Login.add(Time);
     
    		//Buttons
    		UIManager.put("Button.select", red);
    		UIManager.put("Button.border", BorderFactory.createLineBorder(yellow, 3));
    		Button1 = new JButton("Login");
    		Button1.setBackground(yellow);
    		Button1.setForeground(red);
    		Button1.setFocusPainted(false);
    		Button1.setBounds(290, 110, 75, 20);
    		Login.add(Button1);
     
    		//TextField
    		TextField1 = new JTextField();
    		TextField1.setBackground(yellow);
    		TextField1.setForeground(red);
    		TextField1.setBounds(30, 110, 100, 20);
    		Login.add(TextField1);
     
    		TextField2 = new JTextField();
    		TextField2.setBackground(yellow);
    		TextField2.setForeground(red);
    		TextField2.setBounds(160, 110, 100, 20);
    		Login.add(TextField2);
     
    		TextField2 = new JTextField();
     
    		return Login;
    	}
     
    	public JPanel Join(){
    		Join = new JPanel();
    		return Join;
    	}
     
    	public JPanel Create(){
    		Create = new JPanel();
    		return Create;
    	}
     
    	public JPanel Friends(){
    		Friends = new JPanel();
    		return Friends;
    	}
     
    	public JPanel Account(){
    		Account = new JPanel();
    		return Account;
    	}
     
    	public JPanel Settings(){
    		Settings = new JPanel();
    		return Settings;
    	}
     
    	public JMenuBar Menu(){
    		menuBar = new JMenuBar();
    		UIManager.put("PopupMenu.border", BorderFactory.createLineBorder(yellow, 3));
    		UIManager.put("MenuItem.selectionBackground", yellow);
    		UIManager.put("MenuItem.selectionForeground", red);
    		UIManager.put("MenuItem.acceleratorForeground", yellow);
    		UIManager.put("MenuItem.acceleratorSelectionForeground", red);
    		UIManager.put("Menu.selectionBackground", yellow);
    		UIManager.put("Menu.selectionForeground", red);
     
            menu1 = new JMenu("Server");
            menu1.setForeground(yellow);
            menuBar.add(menu1);
     
            item1 = new JMenuItem("Join");
            item1.setForeground(yellow);
            item1.setBackground(red);
            item1.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
     
                }
            });
            item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J, ActionEvent.ALT_MASK));
            item1.getAccessibleContext().setAccessibleDescription("Join");
            menu1.add(item1);
     
            item2 = new JMenuItem("Create");
            item2.setForeground(yellow);
            item2.setBackground(red);
            item2.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK));
            menu1.add(item2);
     
            item3 = new JMenuItem("Friends");
            item3.setForeground(yellow);
            item3.setBackground(red);
            item3.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.ALT_MASK));
            menu1.add(item3);
     
            menu2 = new JMenu("Options");
            menu2.setForeground(yellow);
            menuBar.add(menu2);
     
            item4 = new JMenuItem("Account");
            item4.setForeground(yellow);
            item4.setBackground(red);
            item4.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK));
            menu2.add(item4);
     
            item5 = new JMenuItem("Settings");
            item5.setForeground(yellow);
            item5.setBackground(red);
            item5.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
            menu2.add(item5);
     
            item6 = new JMenuItem("Updates");
            item6.setForeground(yellow);
            item6.setBackground(red);
            item6.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, ActionEvent.ALT_MASK));
            menu2.add(item6);
     
            menuBar.setBackground(red);
            menuBar.setBorderPainted(false);
     
            return menuBar;
    	}
     
    	public void Time(){
    		time = Calendar.getInstance();
    		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    		Time.setText(sdf.format(time.getTime()));
    	}
    }

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Updating Garphics of the JPanel

    Quote Originally Posted by Damian3395 View Post
    I'm changing the method names right now.
    Be sure to post the revised code with new questions so everyone is looking at the same code.
    Quote Originally Posted by Damian3395 View Post
    I don't want to add the graphics into a container,
    They have to be if you plan to show them, more below.
    Quote Originally Posted by Damian3395 View Post
    I thought I could just paint onto the panel itself, but I'm not sure.
    Yes, you can get the content pane of the jframe and then get it's graphics and then draw on it. Rather that that I usually take a slightly different route:

    Make a top level frame (JFrame for example)
    Make a component to draw on (JPanel, JApplet for example)
    Override paintComponent(Graphics g) in the JPanel and do your drawing there.
    Add the JPanel to the JFrame, or set the JPanel as the content pane of the JFrame.

    This way the JPanel can be a class that extends JPanel and house other methods used to draw on that panel, as a stand alone class. Kind of gives it the black-box in OOP.

  7. The Following User Says Thank You to jps For This Useful Post:

    Damian3395 (May 19th, 2013)

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Updating Garphics of the JPanel

    I'm sorry, but I tried to create it in the way you described in your post, but I couldn't make it work. I tried to google methods to use Graphics g in separate class files, but no dice. I have tried many ways, but all have failed. I'm stuck. Also in my panels class, I started creating the GUI layout for my join panel, when I added Label1 it worked, but when I added JTextField, only a grey frame popped up.

  9. #8
    Junior Member
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Updating Garphics of the JPanel

    package main;
     
    import java.awt.*;
     
    import javax.swing.*;
     
    public class Window {
     
    	static JFrame window;
    	static Panels panel;
    	static Window main;
    	static Paint paint;
    	static boolean loop1 = true;
    	static boolean Login = true;
    	static boolean Join = false;
    	static boolean Create = false;
    	static boolean Friends = false;
    	static boolean Account = false;
    	static boolean Settings = false;
    	static Color red = new Color(176, 23, 31);
    	static Color yellow = new Color(205, 173, 0); 
     
    	static public void frame(){
    		window = new JFrame("Instant Messaging");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setSize(400, 400);
    		window.setResizable(false);
    		window.setLocationRelativeTo(null);
    		window.setVisible(true);
    	}
     
    	static public void state(){
    		panel = new Panels();
    		window.add(panel.Login());
    		//window.add(panel.Join());
    		//window.add(panel.Create());
    		//window.add(panel.Friends());
    		//window.add(panel.Account());
    		//window.add(panel.Settings());
    		window.setJMenuBar(panel.Menu());
    		panel.drawTest();
    	}
     
    	static public void main(String s[]){
    		frame();
    		state();
     
    		while(loop1){
    			panel.Time();
    		}
    	}
    }

    package main;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.event.*;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
     
    import javax.swing.*;
     
    public class Panels {
     
    	static Calendar time;
    	JPanel Login, Join, Create, Friends, Account, Settings;
    	JLabel Label1, Label2, Label3, Label4, Label5, Label6, Label7, Time;
    	JButton Button1, Button2;
    	JTextField TextField1;
    	JPasswordField PasswordField1;
    	static JMenuBar menuBar;
    	static JMenu menu1, menu2, menu3;
    	static JMenuItem item1, item2, item3, item4, item5, item6, item7, item8, item9;
    	static String String1 = "Komitet Gosudarstevenno Bezopasnosti";
    	static String String2 = "KGB - Instant Messaging";
    	static String String3 = "Dedicated to Julia the Russian";
    	static Color red = new Color(176, 23, 31);
    	static Color yellow = new Color(205, 173, 0); 
    	static Paint Graphics;
     
    	public JPanel Login(){
    		Login = new JPanel();
    		Login.setBackground(red);
    		Login.setLayout(null);
    		Login.setOpaque(true);
    		Login.setSize(400, 400);
     
    		//Labels
    		Label1 = new JLabel(String1);
    		Label1.setForeground(yellow);
    		Label1.setBounds(75, 10, 250, 20);
    		Login.add(Label1);
     
    		Label2 = new JLabel(String2);
    		Label2.setForeground(yellow);
    		Label2.setBounds(125, 30, 200, 20);
    		Login.add(Label2);
     
    		Label3 = new JLabel(String3);
    		Label3.setForeground(yellow);
    		Label3.setBounds(110, 50, 200, 20);
    		Login.add(Label3);
     
    		Label4 = new JLabel("Username:");
    		Label4.setForeground(yellow);
    		Label4.setBounds(30, 90, 100, 20);
    		Login.add(Label4);
     
    		Label5 = new JLabel("Password:");
    		Label5.setForeground(yellow);
    		Label5.setBounds(160, 90, 100, 20);
    		Login.add(Label5);
     
    		Label6 = new JLabel("By -- v1.0");
    		Label6.setForeground(yellow);
    		Label6.setBounds(10, 320, 160, 20);
    		Login.add(Label6);
     
    		Label7 = new JLabel("News:");
    		Label7.setForeground(yellow);
    		Label7.setBounds(180, 180, 50, 20);
    		Login.add(Label7);
     
    		Time = new JLabel();
    		Time.setForeground(yellow);
    		Time.setBounds(340, 320, 50, 20);
    		Login.add(Time);
     
    		//Buttons
    		UIManager.put("Button.select", red);
    		UIManager.put("Button.border", BorderFactory.createLineBorder(yellow, 3));
    		Button1 = new JButton("Login");
    		Button1.setBackground(yellow);
    		Button1.setForeground(red);
    		Button1.setFocusPainted(false);
    		Button1.setBounds(290, 110, 75, 20);
    		Login.add(Button1);
     
    		Button2 = new JButton("Check For Updates");
    		Button2.setBackground(yellow);
    		Button2.setForeground(red);
    		Button2.setFocusPainted(false);
    		Button2.setBounds(30, 140, 335, 30);
    		Login.add(Button2);
     
    		//TextField
    		UIManager.put("TextField.border", BorderFactory.createLineBorder(yellow, 3));
    		TextField1 = new JTextField();
    		TextField1.setBackground(yellow);
    		TextField1.setForeground(red);
    		TextField1.setFocusable(true);
    		TextField1.setBounds(30, 110, 100, 20);
    		Login.add(TextField1);
     
    		//PasswordField
    		UIManager.put("PasswordField.border", BorderFactory.createLineBorder(yellow, 3));
    		PasswordField1 = new JPasswordField();
    		PasswordField1.setBackground(yellow);
    		PasswordField1.setForeground(red);
    		PasswordField1.setBounds(160, 110, 100, 20);
    		Login.add(PasswordField1);
     
    		//drawTest();
     
    		return Login;
    	}
     
    	public JPanel Join(){
    		Join = new JPanel();
    		Join.setBackground(red);
    		Join.setLayout(null);
    		Join.setOpaque(true);
    		Join.setSize(400, 400);
     
    		//Labels
    		Label1 = new JLabel("Search (Server Name, Friends, Settings, Topic, etc...");
    		Label1.setForeground(yellow);
    		Label1.setBounds(10, 10, 300, 20);
    		Join.add(Label1);
     
    		return Join;
    	}
     
    	public JPanel Create(){
    		Create = new JPanel();
    		return Create;
    	}
     
    	public JPanel Friends(){
    		Friends = new JPanel();
    		return Friends;
    	}
     
    	public JPanel Account(){
    		Account = new JPanel();
    		return Account;
    	}
     
    	public JPanel Settings(){
    		Settings = new JPanel();
    		return Settings;
    	}
     
    	// Add This After Logging In
    	public JMenuBar Menu(){
    		menuBar = new JMenuBar();
    		UIManager.put("PopupMenu.border", BorderFactory.createLineBorder(yellow, 3));
    		UIManager.put("MenuItem.selectionBackground", yellow);
    		UIManager.put("MenuItem.selectionForeground", red);
    		UIManager.put("MenuItem.acceleratorForeground", yellow);
    		UIManager.put("MenuItem.acceleratorSelectionForeground", red);
    		UIManager.put("Menu.selectionBackground", yellow);
    		UIManager.put("Menu.selectionForeground", red);
     
            menu1 = new JMenu("Server");
            menu1.setForeground(yellow);
            menuBar.add(menu1);
     
            item1 = new JMenuItem("Join");
            item1.setForeground(yellow);
            item1.setBackground(red);
            item1.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
     
                }
            });
            item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J, ActionEvent.ALT_MASK));
            item1.getAccessibleContext().setAccessibleDescription("Join");
            menu1.add(item1);
     
            item2 = new JMenuItem("Create");
            item2.setForeground(yellow);
            item2.setBackground(red);
            item2.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK));
            menu1.add(item2);
     
            item3 = new JMenuItem("Friends");
            item3.setForeground(yellow);
            item3.setBackground(red);
            item3.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.ALT_MASK));
            menu1.add(item3);
     
            menu2 = new JMenu("Options");
            menu2.setForeground(yellow);
            menuBar.add(menu2);
     
            item4 = new JMenuItem("Account");
            item4.setForeground(yellow);
            item4.setBackground(red);
            item4.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK));
            menu2.add(item4);
     
            item5 = new JMenuItem("Settings");
            item5.setForeground(yellow);
            item5.setBackground(red);
            item5.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
            menu2.add(item5);
     
            item6 = new JMenuItem("Updates");
            item6.setForeground(yellow);
            item6.setBackground(red);
            item6.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, ActionEvent.ALT_MASK));
            menu2.add(item6);
     
            //Specialize Based on Login
            menu3 = new JMenu("Admin");
            menu3.setForeground(yellow);
            menuBar.add(menu3);
     
            item7 = new JMenuItem("Members");
            item7.setForeground(yellow);
            item7.setBackground(red);
            item7.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
            menu3.add(item7);
     
            item8 = new JMenuItem("Servers");
            item8.setForeground(yellow);
            item8.setBackground(red);
            item8.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
            menu3.add(item8);
     
            item9 = new JMenuItem("Options");
            item9.setForeground(yellow);
            item9.setBackground(red);
            item9.addActionListener(new ActionListener(){
            	@Override
            	public void actionPerformed(ActionEvent e){
     
            	}
            });
            item9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
            menu3.add(item9);
     
            menuBar.setBackground(red);
            menuBar.setBorderPainted(false);
     
            return menuBar;
    	}
     
    	public void Time(){
    		time = Calendar.getInstance();
    		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    		Time.setText(sdf.format(time.getTime()));
    	}
     
    	public void drawTest(){
    		Graphics g = Login.getGraphics();
     
    		g.setColor(yellow);
    		g.fillRect(10, 300, 32, 32);
     
    		Login.paint(g);
    	}
    }

    I really like this method of using Graphics, but I think I might be adding it to the frame at the wrong time because the rectangle is still not painting onto the panel.

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Updating Garphics of the JPanel

    Some suggestions as I read the code;
    Window is the name of an existing java class and should not be used as the name of a different java class.
    Multiple variable declarations out scope their needs. (too many fields and not enough local variables)
    Read the following code sample:
    Window window = new Window();
    window.frame();
    window.state();
    window.time();
    What just happened to that window in this sample program? Read the next sample:
    MyWindow window = new MyWindow();
    window.getPlainOldJFrame();
    window.addPrettyDecorations();
    window.displayClock();
    By reading that code I can guess the code probably displays a clock in a decorative frame. The idea is that method names should suggest their purpose to the reader. Not that MyWindow is an excellent name for a class either, but at least it is not the name of a java class. Class names should describe the object the class represents, as the term "Window" does, but "Window" has been used.
    As I begin to read the second class posted, I realize it is as long as the minute hand on BigBen, so my last suggestion is to cut out the complexity for forum samples. You don't need 40 buttons, 2 or 3 is plenty for a sample with multiple, or just one if the question is about using a button to trigger an action. Leave out as much unnecessary code as possible when trying to understand a concept, and leave the code untouched when trying to correct an error.

Similar Threads

  1. Problem with updating JPanel.
    By alegra in forum AWT / Java Swing
    Replies: 0
    Last Post: July 17th, 2012, 04:41 AM
  2. Updating JPanel on JTabbedPane with a Resize Event
    By rboone2020 in forum AWT / Java Swing
    Replies: 6
    Last Post: June 22nd, 2012, 11:32 AM
  3. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM