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 3 of 3

Thread: New paint method? Please help?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default New paint method? Please help?

    Hi guys. So i create little program. So window is in full screen. But i need help with paint method. I think I this method not understeand fully. So code:
     
    package Test;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.*;
    import java.util.Random;
    public class Main extends JFrame {
    Graphics g;
    	public static void main(String[]args) {
     
    		DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
     
    		Main main = new Main();
     
    		main.run(dm);
     
     
    }
     
    	public Main() {
    		addKeyListener(new KeysListener());
    	}
     
     
    	public void run(DisplayMode dm) {
    		setBackground(Color.black);
    		setForeground(Color.pink);
    		setFont(new Font("Arial",Font.BOLD,40));
     
    		Screen s = new Screen();
     
    		try {
    			s.setFullScreenWindow(dm, this);
    			try {
    			Thread.sleep(30000);
    			}catch(Exception e) {
     
    			}
    		}finally{
    			s.restoreWindow();
    		}
     
     
    	}
     
    	public void paint(Graphics g) {
     
    		g.fillRect(300, 300, 250, 40);
    	}
     
    	public void repaint() {
    		Random rand = new Random(400);
    		g.setColor(Color.RED);
    		g.fillRect(rand.nextInt(), rand.nextInt(), 200, 50);
    		System.out.println("ADSdas");
    	}
     
     
     
    	public class KeysListener implements KeyListener {
    		Graphics g2 = null;
     
    		public void keyPressed(KeyEvent e) {
    		int s = e.getKeyCode();
    			if(s ==e.VK_O) {
    				System.exit(0);
    			}
     
    			if (s == e.VK_Z) {
    			repaint();
    			}
    		}
     
    		@Override
    		public void keyReleased(KeyEvent e) {
     
    		}
     
    		@Override
    		public void keyTyped(KeyEvent arg0) {
    			// TODO Auto-generated method stub
     
    		}
    	}
     
     
    }


    So, paint method works perfectly. He paint rectangle. Good. But I dont know how using ,,paint2'' method. I need when you press z button, ,,paint2'' method draw Oval in random cordination. So instead ,,paint2'' method i using repaint() but i dont know nothing about paint methods and so on. So you can tell me how using ,,paint2'' method? Thanks.. Now when i run program, i get a lot of error..

    Thanks for help..
    P.S i thought about new object Graphics g2...


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: New paint method? Please help?

    First, if you want to use the Full-Screen Exclusive Mode API, please, at least follow the official tutorial:
    Lesson: Full-Screen Exclusive Mode API (The Java™ Tutorials > Bonus)

    And second, your code is not appropriate for several conceptual/technical reasons. Just one: it's generally not a good idea to paint directly into a JFrame.

    So please, start from the ground, reading good stuff about this API.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    xkendzu (March 13th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New paint method? Please help?

    I follow this full screen tutorial by youtube thenewboston. Here Screen class:

    package Test;
    import java.awt.*;
     
    import javax.swing.*;
     
    public class Screen extends JFrame {
     
    	private GraphicsDevice vc;
     
    	public Screen() {
    		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		vc = env.getDefaultScreenDevice();
    	}
     
    	public void setFullScreenWindow(DisplayMode dm, JFrame window) {
     
    		window.setUndecorated(true);
    		window.setResizable(false);
    		vc.setFullScreenWindow(window);
     
    		if (dm != null && vc.isDisplayChangeSupported()) {
    			try {
    				vc.setDisplayMode(dm);
    			}catch(Exception e) {
     
    			}
    		}
     
    	}
     
    	public Window returnWindow() {
    		return vc.getFullScreenWindow();
     
    	}
     
    	public void restoreWindow() {
    		Window w = vc.getFullScreenWindow();
     
    		if (w != null) {
    			w.dispose();
    		}
     
    		vc.setFullScreenWindow(null);
     
    	}
     
     
     
    }

    I think all is good.

    --- Update ---

    I little bit read about this. And this help. Thanks!

Similar Threads

  1. Can't call paint() method from another class
    By Huw in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 18th, 2012, 02:15 AM
  2. Challenge: I need an animation loop outside of the paint method.
    By Spidey1980 in forum Java Theory & Questions
    Replies: 28
    Last Post: August 20th, 2011, 09:20 AM
  3. Inheriting Applet and paint() GUI Method Logic?
    By Jakesta42 in forum Object Oriented Programming
    Replies: 1
    Last Post: August 8th, 2011, 06:58 AM
  4. [SOLVED] How do you get this fountain to animate with paint method?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 48
    Last Post: May 31st, 2011, 04:00 PM
  5. Replies: 3
    Last Post: April 18th, 2010, 10:08 AM