Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree2Likes

Thread: what is AWT?

  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default what is AWT?

    Hi guys, I am new to Java, and to swing. I was starting to program from a book, and I was kind of confused. The books I am using are on swing, but they include
    import.java.awt??? Why? I thought that AWT is old and now no one uses it? Why does it include AWT? I am very confused. To make applications, do you need swing or AWT? Why does the code include AWT? Tyssssssm in advance guys.

  2. #2
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: what is AWT?

    bump

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: what is AWT?

    The answer is (part of) what I just posted in your other thread: start learning and coding and you'll find the answers.

    Quote Originally Posted by MW130 View Post
    Tyssssssm in advance guys.
    This is a technical forum. Please use proper words.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: what is AWT?

    Ok but could you please answer my question?

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: what is AWT?

    Can someone please actually help me with this?

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: what is AWT?

    Contains all of the classes for creating user interfaces and for painting graphics and images. A user interface object such as a button or a scrollbar is called, in AWT terminology, a component. The Component class is the root of all AWT components. See Component for a detailed description of properties that all AWT components share.

    Some components fire events when a user interacts with the components. The AWTEvent class and its subclasses are used to represent the events that AWT components can fire. See AWTEvent for a description of the AWT event model.

    A container is a component that can contain components and other containers. A con tainer can also have a layout manager that controls the visual placement of components in the container. The AWT package contains several layout manager classes and an interface for building your own layout manager. See Container and LayoutManager for more information.
    java.awt (Java 2 Platform SE v1.4.2)
    Select a class here to find out what they do ^

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: what is AWT?

    can anyone please just tell me why it includes Awt, since AWT is old? Please?

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: what is AWT?

    Quote Originally Posted by MW130 View Post
    can anyone please just tell me why it includes Awt, since AWT is old? Please?
    Because it has a lot of good features and people still use AWT.

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: what is AWT?

    Ok, but is it possible to just not import awt? and use JUST import.javax.swing.*; to make like a game? Or do you NEED AWT? Thanks for your replies!

  10. #10
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: what is AWT?

    Quote Originally Posted by MW130 View Post
    Ok, but is it possible to just not import awt? and use JUST import.javax.swing.*; to make like a game? Or do you NEED AWT? Thanks for your replies!
    I believe you can override the paintComponent() method of JPanel to use a panel as a Canvas.
    You should use Canvas for making games because you will be able to get better performance.
    I think Swing components extend something from AWT too.
    Last edited by PhQ; 01-05-2013 at 12:40 AM.

  11. #11
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: what is AWT?

    ok, so basically the answer is yes, awt is necessary?

  12. #12
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: what is AWT?

    Yes. It really depends on what you are trying to achieve.

  13. #13
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: what is AWT?

    If I want to make game, say like runescape (theoretically!), do I need to include AWT? I thought no one used it anymore :P Im so confused!

  14. #14
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: what is AWT?

    Swing evolved from AWT - in the most basic sense it uses lightweight Components as opposed to heavyweight components, the key word here is Components. You build your UI using Components. The Swing package has its own - JButton instead of Button, JTable instead of Table - if you are using Swing you are using Components that extend from JComponent (a class which extends from Component, an AWT class). All this being said, there are lots of classes within AWT that never needed to evolve - they are useful regardless of whether your UI is Swing or AWT. Paint, Font, Graphics, Color, etc...are all examples. So if you wish to use these within your project, then import them.

    Darryl's advice from your other post is spot on:
    Quote Originally Posted by DarrylBurke View Post
    Start learning and coding and you'll find the answers.
    PhQ likes this.

  15. #15
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: what is AWT?

    is there any good way you guys could recommend to learn swing? Everywhere I look they have different coding styles for swing.

  16. #16
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: what is AWT?

    Quote Originally Posted by MW130 View Post
    is there any good way you guys could recommend to learn swing? Everywhere I look they have different coding styles for swing.
    Start at the source: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    FWIW, there are different ''styles' for everything - not just Swing. But Swing (and AWT, and everything) has many important concepts to learn. It takes time, don't bite off more than you can chew, take everything one step at a time, and break things down so you can focus on the actual concepts.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: what is AWT?

    Quote Originally Posted by PhQ View Post
    You should use Canvas for making games because you will be able to get better performance.
    I don't agree with either part of that statement. Do you have a reputed reference that shows that Canvas performs better than JPanel/JComponent in game programming?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: what is AWT?

    Quote Originally Posted by DarrylBurke View Post
    I don't agree with either part of that statement. Do you have a reputed reference that shows that Canvas performs better than JPanel/JComponent in game programming?

    db
    Nope :D
    I have just seen that a lot of people use Canvas for games, so I thought that they are using Canvas for better performance.

  19. #19
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: what is AWT?

    I've made a little test to check which one renders quicker
    Here are the results:
    JComponent
    frames: 4498569 ticks: 60
    frames: 4764755 ticks: 60
    frames: 4802945 ticks: 60
    frames: 4776330 ticks: 60
    frames: 4756758 ticks: 59
    frames: 4796164 ticks: 61
    Canvas:
    frames: 716 ticks: 59
    frames: 767 ticks: 61
    frames: 775 ticks: 60
    frames: 734 ticks: 60
    frames: 740 ticks: 59
    frames: 725 ticks: 60
    Here is the code:
    JComponent
    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
    import java.util.Random;
    
    
    
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    
    
    
    public class JComp extends JComponent implements Runnable {
    
    
    
    
    	private static final long serialVersionUID = 1L;
    	Dimension size = new Dimension(500, 500);
    	private BufferedImage img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    	private int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    
    
    
    
    	private int ticks = 0;
    	private int frames = 0;
    	private boolean running = true;
    	private Random rand;
    
    
    
    
    	public JComp() {
    		rand = new Random();
    		setPreferredSize(size);
    		JFrame frame = new JFrame();
    		frame.add(this);
    		frame.pack();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    
    
    
    
    		new Thread(this).start();
    
    
    
    
    	}
    
    
    
    
    	public void paintComponent(Graphics g) {
    		g.drawImage(img, 0, 0, size.width, size.height, null);
    	}
    
    
    
    
    	public static void main(String[] args) {
    		JComp comp = new JComp();
    	}
    
    
    
    
    	public void render() {
    		repaint();
    	}
    
    
    
    
    	@Override
    	public void run() {
    		ticks = 0;
    		frames = 0;
    		long lastTime = System.nanoTime();
    		double nsPerTick = 1000000000D / 60D;
    		double delta = 0;
    
    
    
    
    		long sTimer = System.currentTimeMillis();
    
    
    
    
    		while (running) {
    			long now = System.nanoTime();
    			delta += (now - lastTime) / nsPerTick;
    			lastTime = now;
    			while (delta >= 1) {
    				tick();
    				ticks++;
    				delta -= 1;
    			}
    
    
    
    
    			render();
    			frames++;
    
    
    
    
    			if (sTimer + 1000 < System.currentTimeMillis()) {
    				System.out.println("frames: " + frames + " ticks: " + ticks);
    				frames = 0;
    				ticks = 0;
    				sTimer += 1000;
    
    
    
    
    			}
    
    
    
    
    		}
    	}
    
    
    
    
    	private void tick() {
    		for (int i = 0; i < pixels.length; i++) {
    			pixels[i] = rand.nextInt();
    		}
    	}
    
    
    
    
    }
    Canvas
    Java Code:
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
    import java.util.Random;
    
    import javax.swing.JFrame;
    
    
    public class Canv extends Canvas implements Runnable{
    	
    	private static final long serialVersionUID = 1L;
    	Dimension size = new Dimension(500, 500);
    	private BufferedImage img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    	private int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    	
    	private int ticks = 0;
    	private int frames = 0;
    	private boolean running = true;
    	private Random rand;
    	
    	public Canv(){
    		rand = new Random();
    		setPreferredSize(size);
    		JFrame frame = new JFrame();
    		frame.add(this);
    		frame.pack();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		
    		new Thread(this).start();
    	}
    
    	public void run() {
    		ticks = 0;
    		frames = 0;
    		long lastTime = System.nanoTime();
    		double nsPerTick = 1000000000D / 60D;
    		double delta = 0;
    
    		long sTimer = System.currentTimeMillis();
    
    		while (running) {
    			long now = System.nanoTime();
    			delta += (now - lastTime) / nsPerTick;
    			lastTime = now;
    			while (delta >= 1) {
    				tick();
    				ticks++;
    				delta -= 1;
    			}
    
    			render();
    			frames++;
    
    			if (sTimer + 1000 < System.currentTimeMillis()) {
    				System.out.println("frames: " + frames + " ticks: " + ticks);
    				frames = 0;
    				ticks = 0;
    				sTimer += 1000;
    
    			}
    
    		}
    	}
    	
    	private void tick() {
    		for (int i = 0; i < pixels.length; i++) {
    			pixels[i] = rand.nextInt();
    		}
    	}
    
    	private void render() {
    		BufferStrategy bs = getBufferStrategy();
    		if(bs == null){
    			createBufferStrategy(3);
    			return;
    		}
    		Graphics g = bs.getDrawGraphics();
    		
    		g.drawImage(img, 0, 0, size.width, size.height, null);
    		
    		g.dispose();
    		bs.show();
    	}
    
    	public static void main(String[] args){
    		Canv comp = new Canv();
    	}
    	
    }
    Not really sure if I've made the test fair. But I am impressed!

    what is AWT?-what.png
    <-- compontent | canvas -->
    Whoa. So is there any downsides of using JComponent instead of Canvas?
    Last edited by PhQ; 01-05-2013 at 06:47 PM.
    Sierra likes this.

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: what is AWT?

    Quote Originally Posted by PhQ View Post
    So is there any downsides of using JComponent instead of Canvas?
    None that I know of. One positive aspect is that JComponent being lightweight doesn't attach any native resources, unlike Canvas which is a heavyweight component.

    Here's some (partially) related reading: Painting in AWT and Swing

    And you really should get rid of unnecessary blank lines before posting code on a forum.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •