Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1705 > unrolled thread

Re: How to draw a simple

Started by"visionset" <visionset@THRWHITE.remove-dii-this>
First post2011-04-27 15:34 +0000
Last post2011-04-27 15:34 +0000
Articles 12 — 5 participants

Back to article view | Back to comp.lang.java.gui

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How to draw a simple "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
    Re: How to draw a simple "TheBigPJ" <thebigpj@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
      Re: How to draw a simple "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
      Re: How to draw a simple "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
        Re: How to draw a simple "TheBigPJ" <thebigpj@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
      Re: How to draw a simple "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
        Re: How to draw a simple "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
          Re: How to draw a simple "TheBigPJ" <thebigpj@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
            Re: How to draw a simple "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
          Re: How to draw a simple "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
            Re: How to draw a simple "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
              Re: How to draw a simple "Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000

#1705 — Re: How to draw a simple

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
SubjectRe: How to draw a simple
Message-ID<Gsi6i.5145$xU4.5069@newsfe1-gui.ntli.net>
  To: comp.lang.java.gui

"visionset" <spam@ntlworld.com> wrote in message 
news:Bqi6i.838$E9.4@newsfe6-gui.ntli.net...
>
> "TheBigPJ" <TheBigPJ@gmail.com> wrote in message 
> news:1180283348.916553.87450@p47g2000hsd.googlegroups.com...
>> Good Morning/Evening
>>
>> Could someone please show me how to draw just a simple line from say
>> 1,1 to 25,25 using the 2d package?
>>
>
> package test; ...

On 2nd thoughts, what do you mean by 2d package? Are we talking a non-JDK 
package?

-- 
Mike W

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1708

From"TheBigPJ" <thebigpj@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<1180286099.759072.108450@p77g2000hsh.googlegroups.com>
In reply to#1705
  To: comp.lang.java.gui
Hello again,

Yes thats exactly how I wish to do it. But when I use that code it
merely gives me an empty JFrame....no line does appear...is this maybe
because I am using java 1.4?

Thank you,
Peter

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1709

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<f9j6i.188692$jt2.164114@newsfe13.lga>
In reply to#1708
  To: comp.lang.java.gui
TheBigPJ wrote:
> Hello again,
> 
> Yes thats exactly how I wish to do it. But when I use that code it
> merely gives me an empty JFrame....no line does appear...is this maybe
> because I am using java 1.4?
> 
> Thank you,
> Peter
> 

No it's because his code doesn't work.

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

public class LineTest extends JPanel {
     public LineTest() {
         setPreferredSize(new Dimension(100,100));
     }

     public void paintComponent(Graphics g) {
         g.drawLine(0,0,25,25);
     }

     public static void main(String... args) {
         JFrame f = new JFrame();
         f.add(new LineTest());
         f.pack();
         f.setVisible(true);
     }
}

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1710

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<16j6i.2794$J15.1453@newsfe3-gui.ntli.net>
In reply to#1708
  To: comp.lang.java.gui

"TheBigPJ" <TheBigPJ@gmail.com> wrote in message 
news:1180286099.759072.108450@p77g2000hsh.googlegroups.com...
> Hello again,
>
> Yes thats exactly how I wish to do it. But when I use that code it
> merely gives me an empty JFrame....no line does appear...is this maybe
> because I am using java 1.4?
>

I'm so sorry, since Java1.4 the explicit calls to add a content pane to a 
JFrame have been hidden.
Much time later I'd completely forgotten that it did actually have one.
Here is the content pane that does the drawing (MyPanel).
Sorry about that - poor show.

import java.awt.Graphics;
import javax.swing.JFrame;

import javax.swing.JPanel;


public class TestLine extends JFrame {


    public TestLine() {

        add(new MyPanel());

        setBounds(0,0,100,100);

        setVisible(true);

    }


    private static class MyPanel extends JPanel {


        protected void paintComponent(Graphics g) {

                g.drawLine(0,0,25,25);

        }

    }


    public static void main(String... args) {

        new TestLine();

    }

}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1711

From"TheBigPJ" <thebigpj@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<1180287419.607240.271180@q66g2000hsg.googlegroups.com>
In reply to#1710
  To: comp.lang.java.gui
Hello again,

Its fine, I am very grateful for all your help.

Dont mean to push on and bug you further, I am using currently BlueJ
(if you have heard of that...its a nice basic enviroment). The code
provided compiles fine (after a couple of tweaks: need one more } and
String...args should be String args[]) however when I try to run the
code I get the following error:

java.lang.Error: Do not use TestLine.add() use
TestLine.getContentPane().add() instead
java.lang.Error: Do not use TestLine.add() use
TestLine.getContentPane().add() instead
java.lang.Error: Do not use TestLine.add() use
TestLine.getContentPane().add() instead

I have tried a couple of things, however im getting myself into a
pickle...and think its time to ask what is wrong?

Thank you,
Peter

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1712

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<4659c977$0$8715$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1708
  To: comp.lang.java.gui
TheBigPJ wrote:
> 
> Yes thats exactly how I wish to do it. But when I use that code it
> merely gives me an empty JFrame....no line does appear...is this maybe
> because I am using java 1.4?

There were a number of problems with the original program.

  o Painting directly on a top-level window isn't a brilliant idea - can 
be obscured by child components, there are insets to worry about, it 
cannot be used as a component, etc.
  o Swing should be used from the Event Dispatch Thread, not other 
threads, such as the main thread.
  o Extending JFrame, JPanel, JApplet, etc., is a bad idea. Don't use 
inheritance more than necessary. Extend JComponent to do the painting, 
but do most of the work outside.
  o Setting location (via bounds) of a top-level is peculiar.
  o It's good form to allow frames to be closed, and the program to exit 
appropriately.
  o Using ... in the declaration of main is obscure.
  o If you are using 1.5+ (1.4 is in end of life), use @Override to 
really make sure you are overriding methods.

public class LineTest extends javax.swing.JComponent {
     public LineTest() {
         setPreferredSize(new java.awt.Dimension(100, 100));
         setForeground(java.awt.Color.BLACK);
     }
     @Override
     protected void paintComponent(java.awt.Graphics g) {
         g.drawLine(0,0,25,25);
     }

     public static void main(String[] args) {
         java.awt.EventQueue.invokeLater(new Runnable() {
                 public void run() {
                     runEDT();
                 }
         });
     }
     private static void runEDT() {
         assert java.awt.EventQueue.isDispatchThread();

         javax.swing.JFrame frame = new javax.swing.JFrame("LineTest");
         frame.setDefaultCloseOperation(
             javax.swing.JFrame.EXIT_ON_CLOSE
         );
         frame.add(new LineTest());
         frame.pack();
         frame.setVisible(true);
     }
}

Tom Hawtin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1713

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<q5k6i.4730$zL6.561@newsfe7-gui.ntli.net>
In reply to#1712
  To: comp.lang.java.gui

"Tom Hawtin" <usenet@tackline.plus.com> wrote in message 
news:4659c977$0$8715$ed2619ec@ptn-nntp-reader02.plus.net...
> TheBigPJ wrote:
>>
>> Yes thats exactly how I wish to do it. But when I use that code it
>> merely gives me an empty JFrame....no line does appear...is this maybe
>> because I am using java 1.4?
>
> There were a number of problems with the original program.

Yes, but do you think the OP is at a stage to take on board your post?
Admittedly I didn't help trotting off that rubbish off the top of my head 
:-/

-- 
Mike W

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1714

From"TheBigPJ" <thebigpj@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<1180292331.717362.11530@o5g2000hsb.googlegroups.com>
In reply to#1713
  To: comp.lang.java.gui
Hello again,

I have done a 'Java Foundations course' which basically covers
http://www.samspublishing.com/library/content.asp?b=STY_Java2_24hours&seqNum=154&rl=1
to 'Hour 14', Some file reading/writing, error handling and
inheritance. I know threads exist but dont fully know how to use them.

I understand that majority of things being said here and fully
appreciate everyones help.

I suppose the biggest barrier I currently have is that I have done my
course in 1.4 and its nearly a dead version. 1.5 is surely what I
should be learning, however being I am currently enrolled in a list of
Java courses I dont know if we will remain in 1.4 or be making a
transition into 1.5. So to play it safe I shall be continuing in 1.4.

Is there a way to draw a line within a JPanel and not use
'paintComponent(java.awt.Graphics g)' ?

I want to aim for having a 400x200 screen with a 200x200 JPanel on the
left (WEST) of my screen with many lines in etc... and a 200x200
JPanel on the right (EAST) with buttons and labels etc...

Thank you,
Peter

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1717

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<XQm6i.9936$RP4.2343@newsfe2-win.ntli.net>
In reply to#1714
  To: comp.lang.java.gui

"TheBigPJ" <TheBigPJ@gmail.com> wrote in message 
news:1180292331.717362.11530@o5g2000hsb.googlegroups.com...
> Hello again,
>
> I have done a 'Java Foundations course' which basically covers
> http://www.samspublishing.com/library/content.asp?b=STY_Java2_24hours&seqNum=154&rl=1
> to 'Hour 14', Some file reading/writing, error handling and
> inheritance. I know threads exist but dont fully know how to use them.
>
> I understand that majority of things being said here and fully
> appreciate everyones help.
>
> I suppose the biggest barrier I currently have is that I have done my
> course in 1.4 and its nearly a dead version. 1.5 is surely what I
> should be learning, however being I am currently enrolled in a list of
> Java courses I dont know if we will remain in 1.4 or be making a
> transition into 1.5. So to play it safe I shall be continuing in 1.4.

Don't lose too much sleep over it.  Although 1.5 was probably the greatest 
leap forward in Java history.
Most code is still pre 1.5 API and the priciples remain the same.  Generics 
is the only real biggy to worry about.

>
> Is there a way to draw a line within a JPanel and not use
> 'paintComponent(java.awt.Graphics g)' ?

Why wouldn't you want to do it that way?
That is the way to do it.
Unless you want to render an gif/png/jpg image to display your requirements?

>
> I want to aim for having a 400x200 screen with a 200x200 JPanel on the
> left (WEST) of my screen with many lines in etc... and a 200x200
> JPanel on the right (EAST) with buttons and labels etc...
>

I don't see how those requirements preclude the use of paintComponent?

-- 
Mike W

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1715

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<4659f6a6$0$8717$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1713
  To: comp.lang.java.gui
visionset wrote:
> "Tom Hawtin" <usenet@tackline.plus.com> wrote in message 
> news:4659c977$0$8715$ed2619ec@ptn-nntp-reader02.plus.net...
>> There were a number of problems with the original program.
> 
> Yes, but do you think the OP is at a stage to take on board your post?

I hoped more people than the original poster would read my message...

But taking my points:

  o Painting directly on a top-level window isn't a brilliant idea - can 
be obscured by child components, there are insets to worry about, it 
cannot be used as a component, etc.

Yup, given that painting to a top-level component has been shown, need 
to know it's actually a really bad idea and it should not be copied.

  o Swing should be used from the Event Dispatch Thread, not other 
threads, such as the main thread.

Standard boilerplate for every Swing application. Something you need to 
start with (although actually possibly not if you are using BlueJ - I 
don't know).

  o Extending JFrame, JPanel, JApplet, etc., is a bad idea. Don't use 
inheritance more than necessary. Extend JComponent to do the painting, 
but do most of the work outside.

Yup. There's a lot of really crap tutorial code. Don't do it!

  o Setting location (via bounds) of a top-level is peculiar.

Given that the bad code had been shown, it's important not to copy that.

  o It's good form to allow frames to be closed, and the program to exit 
appropriately.

It's a PITA not to.

  o Using ... in the declaration of main is obscure.

Yup. ... serves no purpose and is going to cause confusion. Best to 
ignore that code and do the normal thing.

  o If you are using 1.5+ (1.4 is in end of life), use @Override to 
really make sure you are overriding methods.

It's bad enough for an experienced programmer that hasn't realised he 
(or she) hasn't overridden a method. Do this right from the start.

Tom Hawtin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1716

From"visionset" <visionset@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<TMm6i.3905$rW2.682@newsfe7-win.ntli.net>
In reply to#1715
  To: comp.lang.java.gui

"Tom Hawtin" <usenet@tackline.plus.com> wrote in message 
news:4659f6a6$0$8717$ed2619ec@ptn-nntp-reader02.plus.net...
>  o If you are using 1.5+ (1.4 is in end of life), use @Override to really 
> make sure you are overriding methods.
>
> It's bad enough for an experienced programmer that hasn't realised he (or 
> she) hasn't overridden a method. Do this right from the start.
>

Yes that tip at least I shall do from now on.
Although Eclipse lets you know anyway.

The intent of my original code was purely to answer
the question in as short a piece of code as possible.
I mean any example is going to miss out essential stuff,
since an example is just that. Annoyingly I fluffed even that.

-- 
Mike W

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1723

From"Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Message-ID<J8K6i.45$n25.12@newsfe02.lga>
In reply to#1716
  To: comp.lang.java.gui

visionset wrote:

> since an example is just that. Annoyingly I fluffed even that.
>
My experience. Fluffed examples often precipitate useful discussion
and good code examples from readers that wouldn't have otherwise
responded. Useful tactic. Appreciate your willingness to respond to
a novice's request for assistance. Thanks.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web