Confused about how to use paint()

Hi, I have been working really hard to try to get the following program to work but I am really confused on how to use paint(). I do not have anyone to ask so I thought this forum could help. Anyways, here is my problem...
I am trying to recursively figure out the Sierpinski fractal. I cannot figure out if my math works or not because I do not know how to call the paint() recursively to draw my triangles.
Please have a look at the following code. Thank you!
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class DrawTriangle extends Frame {
  Point a = new Point(20,480),
        b = new Point(350,40),
        c = new Point(680,480),
        halfB,
        halfC,
        halfB2,
        halfC2,
        halfC3;
  int width;
  public DrawTriangle() {
    super("Sierpinski Fractal");
    addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent we ) {
        dispose();
        System.exit( 0 );
    setBackground( Color.white );
    setSize(700, 500);
    setVisible(true);
  public void paint(Graphics g, Point a, Point b, Point c) {
    width = c.x - a.x;
    if (width < 6){return;}
    g.setColor( Color.GREEN );
    g.drawPolygon( new int[] { a.x, b.x, c.x }, new int[] { a.y, b.y, c.y }, 3 );
    halfC.x = c.x/2;
    halfC.y = c.y;
    halfB.y = b.y/2;
    halfB.x = b.x;
    halfB2.y = halfB.y + a.y;
    halfB2.x = a.x;
    halfC2.x = halfC.x + a.x;
    halfC2.y = a.y;
    halfC3.x = halfC.x/2 + a.x;
    halfC3.y = halfB2.y;
    paint(g, a, halfC, halfB);
    paint(g, halfC3, halfC, halfB);
    paint(g, halfC2, halfC, halfB);
  public static void main(String[] args) {
     new DrawTriangle();

thanks jsalonen, your tip let me start working on the math to correct it.
I have a new problem now. My math is correct but I am having problems with the recursion. I can draw only the top , left , or right triangles. I cannot get them all to work together. See code and comments below.
Any ideas why I cant call all three of the paint()s toegther and have them work?
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class DrawTriangle extends Frame {
  Point a = new Point(20,480),
        b = new Point(350,40),
        c = new Point(680,480),
        halfA,
        halfB,
        halfC;
  int width;
  public DrawTriangle() {
    super("Sierpinski Fractal");
    addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent we ) {
        dispose();
        System.exit( 0 );
    setBackground( Color.white );
    setSize(700, 500);
    setVisible(true);
  public void paint(Graphics g)
  paint(g, a, b, c);
  public void paint(Graphics g, Point a, Point b, Point c) {
    width = c.x - a.x;
    if (width < 6){return;}
    halfA = new Point((a.x+b.x)/2, (a.y+b.y)/2);
    halfB = new Point((a.x+c.x)/2, (a.y+c.y)/2);
    halfC = new Point((b.x+c.x)/2, (b.y+c.y)/2);
    g.setColor( Color.GREEN ); //draw left triangle in green
    g.drawPolygon( new int[] { a.x, halfA.x, halfB.x }, new int[] { a.y, halfA.y, halfB.y }, 3 );
    g.setColor( Color.RED ); //draw top triangle in red
    g.drawPolygon( new int[] { b.x, halfA.x, halfC.x }, new int[] { b.y, halfA.y, halfC.y }, 3 );
    g.setColor( Color.BLUE ); //draw right triangle in blue
    g.drawPolygon( new int[] { c.x, halfB.x, halfC.x }, new int[] { c.y, halfB.y, halfC.y }, 3 );
    /*If you were to comment our two of these paint() calls the one will work correctly alone.
     *But my problem is that they do not work together! */
    //g, left point, top point, right point
    paint(g, halfA, b, halfC); //top triangle
    paint(g, halfC, halfB, c); //right triangle
    paint(g, a, halfA, halfB); //left triangle
  public static void main(String[] args) {
     new DrawTriangle();
}

Similar Messages

  • Confusing about how to use "protected"

    Hello guys,
    I don't know how the "protected" works for a long time.
    If you know what's the protected really mean,please tell me.
    I am confusing as these follows:
    I write a class named "parent",which has a protected field x.
    1. If I write a class named "son" which extends the "parent" class in the same package and then new parent() to made an instant named "son",can I use son.x to visit??
    2.If I write a class named "son" which extends the "parent" class in the ANOTHER package and then new parent() to made an instant named "son",can I use son.x to visit??(Mybe there is two satuations:one is son.x which is written in a "parent" extended class,the other satuation is son.x which is written in any class except what extends "parent" class)

    Hello Vishal,
    Let-me try help you.
    I don´t know if that´s the best solution, but when I use this class, i do as follow.
    First I create the container, and after the grid (with the "I_PARENT" parameter)
    DATA: r_container TYPE REF TO cl_gui_custom_container,
                r_grid          TYPE REF TO cl_gui_alv_grid.
      CREATE OBJECT r_container
        EXPORTING
          container_name = 'CONTAINER1'.
      CREATE OBJECT r_grid
        EXPORTING
          i_parent = r_container.
    The parameter i_parent associate the container with the grid.
    Don´t forget to built the container using the screen painter (or the screen element´s list, but the screen painter is easer).
    I hope it helps you.

  • Confused about how to display a rectangle on JFrame

    i'm a little confused about how to display a rectangle on a jframe.
    I know there is a class called Rectangle. is there a way to use it to display a rectangle on a JFrame? i couldn't find a way to do that.
    i found a way to display a rectangle in the internet:
    CODE:
    public class RectangleFrame extends JFrame {
        public RectangleFrame() {
            super("My Rectangle");
            setSize(300,400);
            setLocation(300,300);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            JButton button = new JButton("Button");
            JPanel panel = new JPanel();
            panel.add(button);
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(new Rect(), BorderLayout.CENTER);
            getContentPane().add(panel, BorderLayout.SOUTH);       
            setVisible(true);
        public static void main(String[] args) {
            new RectangleFrame();
    public class Rect extends JComponent {
        public void paint(Graphics g) {
            g.drawRect(50,50,200,200);
    }is this the only way to do that? isn't there an easier way? do i have to write this new class Rect i wrote?
    i don't really know how this Rect class i wrote works. could someone explain?
    then another question: can i put a rectangle on a JPanel or do i have to add it directly onto the ContentPane as i did above?
    i hope everything i asked is clear enough.
    thanks for your effort.

    Take a look at the 2D graphics tutorial:
    http://java.sun.com/docs/books/tutorial/2d/index.html

  • HT1695 I know nothing about how to use my new ipod and when reading the little bit of information in the folder that came with it, all I can do is turn it on, but it just powers down after a few seconds.  How do I work this thing?

    I know nothing about how to use my new ipod and when reading the little information that came with it, all I can do is turn it on.  Then in a few seconds it goes black, never having brought up a screen to do anything!  Help!  What do I do to get this thing going?

    You might consider connecting it to the charger and charging up the battery overnight.
    Please Get the iPod Touch User Manual for iOS 5

  • HT1208 Where do I find a tutorial about how to use the new iTunes? I have version 11.1.3 (8). I looked on the Apple web site but only found info extolling the virtues of iTunes, not how to use it.

    Where do I find a tutorial about how to use the new iTunes? I have version 11.1.3 (8). I looked on the Apple web site but only found info extolling the virtues of iTunes, not how to use it.

    Use it the same way as previously.
    ctrl B shows the menus.
    ctrl S shows the sidebar.
    What else do you need help with?

  • Confusion about how JSP application compile

    Hi,
    I am develop a web application using JSP. The application includes many .jsp files and many JavaBeans files in order to accomplish multiple tasks. I feel confused that how all this jsp file and JavaBeans compile together. For say, if the user wants to create a new record, which is only one of all tasks, I assume that when user submit this request, the related jsp files and javaBeans instead of all jsp files and javaBeans, will load into memory and compile together into a servlet class, then according to the form data to do whatever they need to do to create a new record. When a request regarding another task come, another group of related jsp file and JavaBeans will compile to another servlet class to do the job and so on and so forth. Am I right about how they work? Or are all jsp files and javaBeans compile together instead of grouped by task into one servlet class? Which one is true?
    Sincerely, jmling

    I dunno - that statement seems misleading, if not just plain wrong.
    JSP text files can be potentially grouped together - ie you use the include directive to include one JSP file inside another. Basically its just a "paste this file inside the first one before translating into a servlet".
    But anything apart from <%@ include %> directives are not bundled together at this phase.
    This is the way I see it happening (simplified)
    - Process <%@include%> directives to get one large .jsp file
    - The jsp file itself is translated into a servlet .java file
    - The .java file is compiled into a class file
    - The class file is run, and it generates HTML which is sent to the client
    As part of this generation it may invoke other JSP pages through jsp:include / jsp:forward commands.
    These are treated seperately - is you translate/compile each requested JSP page seperately.
    All that the JSP page produces at the end of the day is an HTML file to send to the client.
    If there is an image on the page, the generated HTML includes a tag for the image, but the JSP doesn't send the image itself (just like a static web page can contain an image)
    In summary, the phrase in your quote "the files that make up the application are all translated together..." is misleading. The translation only applies to the particular jsp file requested, and any that it includes with the include directive.
    Cheers,
    evnafets

  • HT1688 I am trying to find answers about how to use the earphones that came with my new iPhone 4s. Does this work similarly as a Bluetooth or do I need to still buy one. The printing in the manual is SOOO small, it is too hard to read!

    Does anyone know how to use the earphones( plugs) that come with iPhones?
    Mine is still fairly new and I am just learning how to use certain things.

    They're pretty straightforward. You plug them into the headset jack (the hole on the top left of your phone). You put the buds in your ears. All sounds, except for the phone ringing will now come through the earbuds.

  • Idea about how-to using 'Agilent Wireless Test Manager' interfaces in Labview ?

    Hello everyone,
    I am not sure whether it is appropriate to post this here.  
    Currently I got a Wireless Test Manager CD(Agilent E6560A) and is trying to convert some functionalities inside class 'cdmaTXMeasTests' into labview.
    I am doing the 'code domain power testing' module with labview, which is well done in that 'cdmaTXMeasTests'.
    I am wondering if any one could provide me some idea how to use that 'cdmaTXMeasTests' in my labview without rebuild the wheel.
    ( I am not sure whether that would work,  Are the libaries used by wireless test manager a  ActiveXDLL that could be used by labview ? Or there is some other way to do it or not?)
    Any idea is well appreciated,
    +Kunsheng Chen

    Good Afternoon Kunsheng Chen,
    It seems like you are trying to port your DLL.  Rather, I would suggest using a Call Library Function Node (right-click the Block Diagram, Functions>>Connectivity>>Libraries & Executables>>Call Library Function Node).
    From the Agilent website, this is a C dll so you will be able to access it using this VI in LabVIEW with no problems.  You might want to contact Agilent to see if this DLL has ActiveX components.  Alternatively, you can simply browse the list of ActiveX components on your computer.
    The following links may provide some useful information for using a DLL in LabVIEW.
    An Overview of Accessing DLLs or Shared Libraries from LabVIEW
         http://zone.ni.com/devzone/cda/tut/p/id/3009
    Call Library Function Node
         http://zone.ni.com/reference/en-XX/help/371361E-01​/glang/call_library_function/
    Using Existing C Code or a DLL in LabVIEW
         http://decibel.ni.com/content/docs/DOC-1690
    Regards,
    Charlie Piazza
    Staff Product Support Engineer, RF
    National Instruments

  • Any idea about how to use EclipceMe

    Im a new in J2Me environments and I'm using EclipceME as an IDE for it
    im able to debug n run the programme in the mobile emulator but the problems is if there is an error it is just tell u that there i an error but does not tell where is the error or in which line
    if somone tell me how to use the eclipseME to develop a mobile program or if there is any other IDE which has better feature

    Hi,
    Follow the following links to find what u need (Eclipse IDE):
    - [http://bin.adawy.googlepages.com/DevelopingJ2MEapplicationswithEclips.pdf]
    - [http://eclipseme.org/docs/|http://eclipseme.org/docs/]
    Better and powerfull IDE : Netbeans
    - [http://www.netbeans.org/kb/trails/mobility.html|http://www.netbeans.org/kb/trails/mobility.html]

  • Document about how to use each BI Bean

    Anyone know whether there is any document on how to use each BI Beans besides Bean's javadoc?

    I would recommend downloading the latest BI10g samples from the BI Beans website. Click here for downloads, demos and documentation for BI Beans 10.1.2.0.0.
    For the graph there is a lot of really good information located on the Reports OTN page. This explains how to use many of the features of the BI Beans graph and contains some really good examples of how to format a graph.
    http://www.oracle.com/technology/products/reports/htdocs/faq/Graph_FAQ_with_style.html
    I would recommend reviewing the OU course on the BI Beans Query Model as this explains the basics of how to construct and manage queries:
    Oracle9i JDeveloper: Explore the BI Beans Query Model (Development Tools)
    http://education.oracle.com/pls/web_prod-plq-dad/show_desc.redirect?dc=D18655&p_org_id=1001&lang=US&source_call=
    The BI Beans query model supports powerful and flexible query techniques for analytic applications. This course explores this query model, both from an API perspective and a user interface perspective, with the goal of illustrating advanced query techniques for custom business intelligence applications.
    BI Beans 10g provides a lot of new features. We have a viewlet that explains in detail how to use one of the key new features: JSP list tags. This viewlet demonstrates a number of list tags, such as:
    * DimensionMember
    * CascadingDimensionMember
    * AWProgram
    * Navigation
    This viewlet can be viewed directly from the link on OTN:
    http://www.oracle.com/technology/products/bib/1012/viewlets/MS%20Developing%20Executive%20Insight.html
    There are OBEs posted on OTN:
    Develop Business Intelligence Objects using OracleBI Beans
    http://www.oracle.com/technology/obe/obe_bi/bibeans/bibeans_1012/developolapobjects/lessonlist.html
    Create business intelligence objects using BI Beans. These analytic objects, such as crosstabs, graphs, and calculations, can be used in either HTML-client or Java-client applications.
    Develop Business Intelligence Applications using OracleBI Beans
    http://www.oracle.com/technology/obe/obe_bi/bibeans/bibeans_1012/buildbibeansapps/lessonlist.html
    Create BI Beans HTML-client (JSP) applications and Java-client applications.
    For more information on using BI Beans graphs with non-OLAP data sources please refer to the following JDeveloper tutorial:
    Adding a BI Beans Graph in an ADF Business Components/JSP/Struts Application
    http://www.oracle.com/technology/obe/obe9051jdev/BIBeansOBE/BIBeansOBE.htm
    Hope this helps
    Oracle Business Intelligence Beans Product Management Team
    Oracle Corporation

  • Confused about how to get link created by website

    I am fallowing a video tutorial on YouTube about how to download a YouTube video as Mp3 file.Once the user typed a YouTube link into
    texbox and clicked the download button The video file is supposed to be download . I think I am doing wrong something .I  think .I am able to get textbox  and button Id like this way
    private void button1_Click(object sender, EventArgs e)
    if (_browser.Document != null)
    var elementById = _browser.Document.GetElementById("youtube-url");
    if (elementById != null)
    elementById.SetAttribute("value", textBox1.Text);
    var htmlElement = _browser.Document.GetElementById("submit");
    if (htmlElement != null)
    htmlElement.InvokeMember("click");
    status = true;
    The problem once the video converted  the web site offers a link to get the video.I need to get the adress written bold charcters.
    How can I do it?
    <a href="/get?video_id=KMU0tzLwhbE&amp;ts_create=1423907396&amp;r=NzguMTc5LjY3Ljg1&amp;h2=4a594985c56edf3bd2da78222a79bdba&amp;s=145591"><b>Download</b></a>
    <a href="/get?video_id=KMU0tzLwhbE&amp;h=-1&amp;r=-1.1" style="display:none"><b>Download</b></a>
    <a href="/get?video_id=KMU0tzLwhbE&amp;ts_create=1423907396&amp;r=NzguMTc5LjY3Ljg1&amp;h2=4a594985c56edf3bd2da78222a79bdba&amp;s=145591"><b>Download</b></a>
    this is the web site  adress

    Hi alak41,
    From your code like following,  since we don't know that the developer how to write the ConvetVideo event in YouTube MP3 website. Maybe you should better ask the website's publishers.
    if (htmlElement != null)
    htmlElement.InvokeMember("click");
    In addition, here is a .NET library, that allows to download videos from YouTube and/or extract their audio track (currently only for flash videos).
    https://github.com/flagbug/YoutubeExtractor/
    Best regadrs,
    kristin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • I am confused about how to manage photos between my iPhone, iMac and iPad.  How do I avoid using all the storage pace on my various devices?

    How do I manage photos between my ipad, iphone and imac without using all the storage on the various devices?

    If enabled, iCloud Photo Stream ensures all new photos are sent to all your devices. Then you can simply add the ones you want to each one's Library, and delete any photos you don't want.
    One technique is to designate one device as the "master" containing all your photos - probably your iMac, since it is likely to have the greatest available storage - and make sure you have a backup strategy for it. That way, any photos you choose to keep on your iPad and iPhone are redundant copies that can be deleted as you wish, or as required should you need to restore your iOS device. If your iMac breaks and its Library irretrievably lost, the importance of its backup becomes critical. Therefore, consider having more than one backup, and more than one geographical location for them.

  • Question about How to Use Custom CellEditors

    Hi:
    I have been trying to implement something like this: I have a JPanel, and when you double-click a particular spot on it, I want a textbox to appear. Currently, I am implementing it by using an undecorated JDialog that contains a JTextField in it (such that the text field occupies all the space in the dialog, i.e. you can't even tell that there is a dialog). This seems like a hack to me.
    Another alternative that I have been thinking about is using layered panes, and hiding the textfield below the jpanel, and only bringing it to the front when the user double-clicks.
    Then, I stumbled onto custom celleditors. Now, it seems that everyone is using them in JTables, JLists, JComboBoxes, etc. and I was wondering if it is something that I can use with a JPanel.
    Can anyone comment on the implementations I am considering, as well as using custom celleditors with a JPanel?
    Thanks in advance,
    LordKelvan

    Still don't understand your requirement. Does the text field stay there or disappear after the data is entered? If it disappears, then how do you change the value, do you have to click on the exact same pixel again?
    Maybe you could use a MouseListener and then display a JOptionPane or JDialog to prompt for the information. Or if you want to display a JTextField then add a JTextField to the panel and then when the user enters the data by using the enter key or by clicking elsewhere on the GUI you remove the text field from the panel.

  • Confusion about how object interact and are aware of each other

    I have this java applet
    Two of the classes it contains are the main class with the init method and a UI class that is code for the interface.
    the main class instantiates the UI class anonymously (i think you call it that) i.e it does not put it into a named reference it just creates it by calling new without assigning it to anything.
    When I click on a button from the UI class I want to call a method in the main class...but how so?
    At first I tried to make the function i want to call in the main class static so I could easily call it from the UI class...but then it began to get complex...so i just sent a reference of the main class to the UI class via its constructer.
    Now i want the main class to update a JTable in the UI class, but how? It does not have any reference to the UI class.
    What is really the standard method of operation for this sort of thing when objects have to be calling methods of each other. How do I make them aware of each other.

    the best way to do it is like this:
    public class Main {
      //this is the main class
      private static class MyUIFrame extends JFrame {
        //create the class. It is useable within Main this way
        //static means it doesn't implicitly 'know' about the Main class. You can also
        //remove this and call Main.this to use Main's methods
    }Edited by: tjacobs01 on Apr 11, 2009 2:28 AM

  • How to use paint bucket.

    I am trying to change the background color and I USED, to be able to just click on the paint bucket and then click the background with whichever color I wanted.
    For some reason when i click nothing happens... Im getting frustrated. blah
    Please help.

    Something like this:
    1. Use the Rectangular Marquee Tool to draw the rectangular selection
    2. Select the Paint Bucket Tool
        Add a new blank layer
        (so it's easy to change later)
        Click on the foreground color chip in the toolbox and set your color for the fill
        Click inside the selection with the paint bucket tool
        Select>Deselect
    You can use the rulers to see how far an inch is in your document
    (View>Rulers)

Maybe you are looking for

  • White MacBook takes too much time to boot up

    Hi there. I got the topcase(keyboard) of my MacBook repaired. (White MacBook 2006) and after that it takes nearly 7 up to 10 seconds after pressing the power button that I first see apple logo and it boots up. I'm not sure but seems like that detecti

  • Final cut is not reading events or projects from my external hard drive, what can I do?

    Final Cut X is not reading events of projects from my external hard drive, what can i do?

  • IWDDropDownByKey - Key is shown instead of the value in View

    Hi, we found some strange behaviour when we dynamically generate some IWDDropDownByKey UI-Elements in an IWDTransparentContainer. In IExplorer and Firefox all data looks well. The values in the IWDDropDownByKey-Elements are shown as expected. In one

  • Rules for VAT register numeration

    Hi all As somes of you know in Italy it is not permit to have a numeration gap in FI VAT declaration register; the vendor and customer invoices has to have a consecutive numbers without "lost" numbers in the sequence and following the calendar date s

  • Solution manager RCA with third party applications

    Hi, Our setup is fairly huge with several third party applications being used. For every business scenario there are 2 or 3 other applications  involved with SAP. If I configure RCA for BPM will the system be able to analyze the root cause of the iss