Results 1 to 7 of 7

Thread: Expert opinion

  1. #1
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Expert opinion

    Hi

    i need to start a project (canvas) with some functions:

    Must have several different shapes (triangle, rectangle, lines, elipses, etc).

    1 - Do i need to create one class for each one?

    2- Must have a list of all of them, do i use hashmap or table??

    3 - Each object can move or change colors, i need to create a method for that, but do i have to create a heritage class?

    I have started with just two classes, the main (canvas) and other one (objects)
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    
    /**
     * Definição de Objetos.
     * 
     * @gmc 
     * @0.1
     */
    public class Objetos
    {
        private JFrame frame;
        private JanelaPane g;
        private Graphics2D graphic;
        private Color backgroundColor;
        private Image canvasImage;
        
        public Objetos(String titulo)
         {
            this(titulo, 300, 300, Color.white);        
        }
        
         public Objetos(String titulo, int largura, int altura)
        {
            this(titulo, largura, altura, Color.white);
        }
        
         public Objetos(String titulo, int largura, int altura, Color bgColor)
        {
            frame = new JFrame();
            g = new JanelaPane();
            frame.setContentPane(g);
            frame.setTitle(titulo);
            g.setPreferredSize(new Dimension(largura, altura));
            backgroundColor = bgColor;
            frame.pack();
        }
        
        public void setVisible(Boolean visble)
        {
            frame.setVisible(true);
        }
        
        protected void draw(Shape shape)
        {
            graphic.draw(shape);
            g.repaint();
            
        }
        
        protected void fill(Shape shape)
        {
            graphic.draw(shape);
            g.repaint();
            
        }
        
        protected void apagar(Shape shape)
        {
            Color original = graphic.getColor();
            graphic.setColor(backgroundColor);
            graphic.fill(shape);
            graphic.setColor(original);
            g.repaint();
        }
        
        //Definições dos objetos existentes
        
        public void linhas(int x, int y, int altura, int largura)
        {
            //fill(new Line(x, y, altura, largura));
        }
        
        public void drawLine(int x, int y, int altura, int largura)
        {
            graphic.drawLine(x,y,altura,largura);
            g.repaint();
        }
        
        
        
        //Jframe
        public class JanelaPane extends JPanel
        {
            public void paint(Graphics g)
            {
                g.drawImage(canvasImage, 0, 0, null);
            }
        }
        
    }
    Can i continue like this?

    Thanks
    Last edited by alphasil; 12-10-2012 at 03:37 PM. Reason: Error

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

    Default Re: Expert opinion

    Please go through the Forum Rules, particularly the third paragraph. Also go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly (to change the subject line, click 'Go Advanced').

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

  3. #3
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Expert opinion

    Ok

    Sorry, now it is ok ?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    
    /**
     * Definição de Objetos.
     * 
     * @gmc 
     * @0.1
     */
    public class Objetos
    {
        private JFrame frame;
        private JanelaPane g;
        private Graphics2D graphic;
        private Color backgroundColor;
        private Image canvasImage;
        
        public Objetos(String titulo)
         {
            this(titulo, 300, 300, Color.white);        
        }
        
         public Objetos(String titulo, int largura, int altura)
        {
            this(titulo, largura, altura, Color.white);
        }
        
         public Objetos(String titulo, int largura, int altura, Color bgColor)
        {
            frame = new JFrame();
            g = new JanelaPane();
            frame.setContentPane(g);
            frame.setTitle(titulo);
            g.setPreferredSize(new Dimension(largura, altura));
            backgroundColor = bgColor;
            frame.pack();
        }
        
        public void setVisible(Boolean visble)
        {
            frame.setVisible(true);
        }
        
        protected void draw(Shape shape)
        {
            graphic.draw(shape);
            g.repaint();
            
        }
        
        protected void fill(Shape shape)
        {
            graphic.draw(shape);
            g.repaint();
            
        }
        
        protected void apagar(Shape shape)
        {
            Color original = graphic.getColor();
            graphic.setColor(backgroundColor);
            graphic.fill(shape);
            graphic.setColor(original);
            g.repaint();
        }
        
        //Definições dos objetos existentes
        
        public void linhas(int x, int y, int altura, int largura)
        {
            //fill(new Line(x, y, altura, largura));
        }
        
        public void drawLine(int x, int y, int altura, int largura)
        {
            graphic.drawLine(x,y,altura,largura);
            g.repaint();
        }
        
        
        
        //Jframe
        public class JanelaPane extends JPanel
        {
            public void paint(Graphics g)
            {
                g.drawImage(canvasImage, 0, 0, null);
            }
        }
        
    }
    Any help please?

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Expert opinion

    I don't see your question. What is it again?

  5. #5
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Expert opinion

    Sorry, i forgot

    So, i must make a Canvas with some functions, the first one (adding lines) to the canvas doesn't work.

    My main class

    Java Code:
    import java.awt.Color;
    /**
     * Janela principal de funcionamento do programa.
     * 
     * @gmc
     * @0.1 
     */
    
    public class JanelaPrincipal   
    {
        private Objetos jan;
    
        public JanelaPrincipal()
        {
            jan = new Objetos("Trabalho GMC", 600, 500);        
            jan.setVisible(true);
            
        }
        
        public void Desenhar()
        {
           jan.setVisible(true);
           jan.setFundo(Color.blue);
         
        }
     
    }
    my canvas class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    
    /**
     * Definição de Objetos.
     * 
     * @gmc 
     * @0.1
     */
    public class Objetos
    {
        private JFrame frame;
        private JanelaPane g;
        private Graphics2D graphic;
        private Color backgroundColor;
        private Image canvasImage;
        
        public Objetos(String titulo)
         {
            this(titulo, 300, 300, Color.white);        
        }
        
         public Objetos(String titulo, int largura, int altura)
        {
            this(titulo, largura, altura, Color.white);
        }
        
         public Objetos(String titulo, int largura, int altura, Color bgColor)
        {
            frame = new JFrame();
            g = new JanelaPane();
            frame.setContentPane(g);
            frame.setTitle(titulo);
            g.setPreferredSize(new Dimension(largura, altura));
            backgroundColor = bgColor;
            frame.pack();
        }
        
          public void setVisible(boolean visible)
        {
            if(graphic == null) {
                // first time: instantiate the offscreen image and fill it with
                // the background color
                Dimension size = g.getSize();
                canvasImage = g.createImage(size.width, size.height);
                graphic = (Graphics2D)canvasImage.getGraphics();
                graphic.setColor(backgroundColor);
                graphic.fillRect(0, 0, size.width, size.height);
                graphic.setColor(Color.black);
            }
            frame.setVisible(true);
        }
    
        /**
         * Fornece informacao sobre a visibilidade do Canvas
         * @return  true se o Canvas esta visivel, false caso nao esteja
         */
        public boolean isVisible()
        {
            return frame.isVisible();
        }
        
        
        //Definição de cores e de fundos
        
        public void setFundo(Color nova)
        {
           graphic.setColor(nova);
            
        }
        
        public Color getFundo()
        {
            return graphic.getColor();
        }
        
        public void setCorFundo(Color nova)
        {
            backgroundColor = nova;
            graphic.setBackground(nova);
        }
        
        public Color getCorFundo()
        {
            return backgroundColor;
        }
        
        
        //Fim
        private class JanelaPane extends JPanel
        {
            public void paint(Graphics g)
            {
                linhas obj = new linhas();
                obj.paint(g);
                
            }
        }
            
    }
    My line class
    Java Code:
    import java.awt.*;
    /**
     * Write a description of class linhas here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class linhas extends Canvas
    {
        private String nome;
        private Color cores;
        private int altura;
        private int largura;
        private int posicaoX;
        private int posicaoY;
        /**
         * Constructor for objects of class linhas
         */
        public void linhas(String nome,Color cores,int altura, int largura, int posicaoX, int posicaoY)
        {
            this.nome =nome;
            this.cores=cores;
            this.altura=altura;
            this.largura=largura;
            this.posicaoX=posicaoX;
            this.posicaoY=posicaoY;
        }
        
        public void paint(Graphics g)
            {
                g.drawLine(10,10,10,10);
                g.setColor(Color.blue);
            }
    }
    I can't get my line in the canvas...

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

    Default Re: Expert opinion

    Some recommendations
    • Avoid mixing AWT and Swing - Swing is newer - I'd recommend using a JPanel and not Canvas (AWT).
    • I would recommend going through some basic tutorials for how to paint using AWT or Swing ( for instance Trail: 2D Graphics (The Java™ Tutorials) ). For instance don't call getGraphics() on a component unless you know what you are doing.
    • I would recommend posting an SSCCE - there is tons of code unrelated to your question and you are asking unpaid volunteers to wade through it all.

  7. #7
    alphasil is offline Member
    Join Date
    Jan 2012
    Posts
    33
    Rep Power
    0

    Default Re: Expert opinion

    Thanks DoWhile

    The main problem is the assignment must have canvas.

    Then, a lot of differents shapes with some functions

    regards

Similar Threads

  1. Need a experienced Java dev opinion ! :O
    By santa in forum New To Java
    Replies: 7
    Last Post: 06-02-2011, 12:58 PM
  2. need opinion on public static final etc ...
    By mac in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 10:54 AM
  3. Need help from java expert
    By javaseek in forum New To Java
    Replies: 15
    Last Post: 10-14-2008, 07:17 PM

Posting Permissions

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