Results 1 to 7 of 7
Thread: Expert opinion
- 12-10-2012, 11:50 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 27
- Rep Power
- 0
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)
Can i continue like this?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); } } }
ThanksLast edited by alphasil; 12-10-2012 at 02:37 PM. Reason: Error
- 12-10-2012, 02:01 PM #2
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').
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-10-2012, 02:38 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 27
- Rep Power
- 0
Re: Expert opinion
Ok
Sorry, now it is ok ?
Any help please?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); } } }
- 12-10-2012, 06:07 PM #4
Re: Expert opinion
I don't see your question. What is it again?
- 12-10-2012, 07:45 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 27
- Rep Power
- 0
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
my canvas classJava 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 line classJava 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); } } }
I can't get my line in the canvas...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); } }
- 12-10-2012, 09:40 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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.
- 12-10-2012, 09:53 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Need a experienced Java dev opinion ! :O
By santa in forum New To JavaReplies: 7Last Post: 06-02-2011, 11:58 AM -
need opinion on public static final etc ...
By mac in forum New To JavaReplies: 4Last Post: 05-20-2010, 09:54 AM -
Need help from java expert
By javaseek in forum New To JavaReplies: 15Last Post: 10-14-2008, 06:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks