Solved

Why doesn't this JFrame draw a line?

Posted on 2011-09-24
10
203 Views
Last Modified: 2012-05-12
I don't understand why the JFrame below doesn't draw a line..

Thanks

import java.awt.*;
import javax.swing.*;


public class happyFrame extends JFrame{

	public void repaint(Graphics G) {

		G.setColor(Color.blue);
		
		G.drawLine(20, 20, 200, 200);
	}
	public happyFrame () {
		
		this.setSize(200,200);
		this.setLocation(15, 150);
		this.setVisible(true);
		 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		repaint();
	}
}

Open in new window

0
Comment
Question by:beavoid
  • 8
  • 2
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36594500
It should be paint(Graphics G)
not repaint(Graphics G)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36594502
This draws the line, but in general you  want to add panel
to the frame

import java.awt.*;
import javax.swing.*;


public class happyFrame extends JFrame{

	public void paint(Graphics G) {

		G.setColor(Color.blue);
		
		G.drawLine(20, 20, 200, 200);
	}
	public happyFrame () {
		
		this.setSize(200,200);
		this.setLocation(15, 150);
		this.setVisible(true);
		 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		repaint();
	}

    public static void main(String[] args) {
        new happyFrame();
    }
)

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36594509

This would be normal way
(otherwise - without adding Panel -  it looks like it grabs as background
part of your current screen)

import java.awt.*;
import javax.swing.*;


public class happyFrame extends JFrame{


	public happyFrame () {
        Container c = this.getContentPane();
		         c.add(new MyPanel());
		this.setSize(200,200);
		this.setLocation(300, 200);
		this.setVisible(true);
		 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		repaint();
	}

    public static void main(String[] args) {
        new happyFrame();
    }
}
class MyPanel extends JPanel {
       public void paint(Graphics g) {

		g.setColor(Color.blue);

		g.drawLine(20, 20, 200, 200);
	}

}

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 47

Expert Comment

by:for_yan
ID: 36594514
And you don't need repaint() after setVisible(true);

It will paint all components when 
you execute setVisible(true).

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36594517
This is witthout repaint()
Still works the same way

import java.awt.*;
import javax.swing.*;


public class happyFrame extends JFrame{


	public happyFrame () {
        Container c = this.getContentPane();
		         c.add(new MyPanel());
		this.setSize(200,200);
		this.setLocation(15, 100);
         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);

	//	repaint();
	}

    public static void main(String[] args) {
        new happyFrame();
    }
}
class MyPanel extends JPanel {
       public void paint(Graphics g) {

		g.setColor(Color.blue);

		g.drawLine(20, 20, 200, 200);
	}

}

Open in new window

0
 

Author Comment

by:beavoid
ID: 36595310
Nice.

Thanks

Last time I did this, (AWT), I had my Frame drawn in its own class, not a satellite class.
Does it matter?
Is there a disadvantage in doing the getContentPane() thing?

If I put all my listeners on the Mypanel, does it act exactly like a Jframe?
or do I still add listeners to the JFrame?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595368
AWT works differently - you can add directly to the Frame or other top-level elemnt, you don't need to do this stuff
with getContentPane() as with Swing
I don't know about any disadvantage with it, you just need to rememeber to do it.

It does not matter where you put the listeners - in any class you want - I usuallt add listeners to the top-level
frame, they can be even added to any class - it is just a matter to have convenient access to some
GUI elements in the action methods
0
 

Author Comment

by:beavoid
ID: 36595851
Thx,
Before I do my backbuffer game rendering,

I'm going to have the game on an all-white background.
 What is the best way to clear it?
G.fillRect ?
or can I simply say G=null;  or = new Graphics()

?
Which is fastest, cleanest?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595874
when you initially create your panel
it will be all clean - if you need to clean it after you drew
something

you say
g.glearRect(0,0,this.getWidth(), this.getHeight())

But it should be in the paint method

so one of the ways to do it form outside  the method -
you have instance variable toClean in your class
and inside the piant method
you have

if(toClean)g.clearRect(0,0,...)

and ouside the paint method
when you want to clean you
say

toClean = true;
repaint();

and it will clean it






0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595953
In general when you design such things it is rarely you want to clean

First it appaers blank
then ywhen doing paint metghod it should rely on
ssome data whuch sould be instrance vraibles of the class

Say, assume you draw cards; you keep them in arraylist
This arraylist  should be instance variable and ittakes each card and draws
it in your paint

when in the course of your play you have situation that you have no cards
then your arraylist at thet moment has no elements
so your paint method should be written so, that
when your arrylist is empty - you just say
repaint()
and it should draw blank panel

So you rarely need to tdo something with special boolean
value to clean it as I wrote above
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ForLoop Example 3 48
MySQL  on Tomcat 8 45
expectj telnet failing 5 36
jboss wildfly 10.1 10 87
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question