Programas Gráficos - Programa Base 2D - Prof. Lucio Mauro Braga Machado Março - 2015 1 Criar a classe Ponto public class Ponto { private double x; private double y; private boolean pu; public Ponto(double x,double y, boolean pu){ this.x=x; this.y=y; this.pu=pu; } public Ponto(){ } } 2 Encapsular os campos da classe 3 Criar a classe Desenho import java.awt.Dimension; import java.awt.Graphics; import java.util.Vector; public class Desenho { private double xwmax, xwmin, ywmax, ywmin, xvmax, xvmin, yvmax, yvmin; private double xmax, ymax, xcoord, ycoord, xd, yd, mx, my; Vector desenho = new Vector(); public void setMundo(double x1, double y1, double x2, double y2) { xwmax = x2; ywmax = y2; xwmin = x1; ywmin = y1; } public void setDimension(Dimension d){ xmax=d.width; ymax=d.height; } public double getMouseX(){ return mx; } public double getMouseY(){ return my; } public void setMouse(int x,int y){ mx =(((x-xvmin) / ((xvmax - xvmin) / (xwmax - xwmin))) + xwmin) / xmax; my =(((y-yvmin) / ((yvmax - yvmin) / (ywmax - ywmin))) + ywmin) / ymax; } public void setViewPort(double x1, double y1, double x2, double y2) { xvmax = x2; yvmax = y2; xvmin = x1; yvmin = y1; } private void mapCoord(double x, double y) { double xv =(((x-xwmin) * ((xvmax - xvmin) / (xwmax - xwmin))) + xvmin); double yv =(((y-ywmin) * ((yvmax - yvmin) / (ywmax - ywmin))) + yvmin); xd = xv * xmax; yd = yv * ymax; } private void linhaAte(Graphics g, double x, double y) { double xi, yi; mapCoord(xcoord, ycoord); xi = xd; yi = yd; mapCoord(x, y); g.drawLine((int) xi, (int) yi, (int) xd, (int) yd); xcoord = x; ycoord = y; } private void movaPara(Graphics g, double x, double y, boolean pu) { if (pu) { xcoord = x; ycoord = y; } else { linhaAte(g, x, y); } } public void desenha(Graphics g) { Ponto p; for (int i = 0; i < desenho.size(); i++) { p = (Ponto) desenho.get(i); movaPara(g, p.getX(), p.getY(), p.isPu()); } } public void triangulo() { desenho.addElement(new Ponto(10d, 9d, true)); desenho.addElement(new Ponto(9d, 11d, false)); desenho.addElement(new Ponto(11d, 11d, false)); desenho.addElement(new Ponto(10d, 9d, false)); } public void quadrado(){ desenho.addElement(new Ponto(11, 9, true)); desenho.addElement(new Ponto(9, 9, false)); desenho.addElement(new Ponto(9, 11, false)); desenho.addElement(new Ponto(11, 11, false)); desenho.addElement(new Ponto(11, 9, false)); } public void translada(double dx, double dy) { Ponto p; for (int i = 0; i < desenho.size(); i++) { p = (Ponto) desenho.get(i); p.setX(p.getX() + dx); p.setY(p.getY() + dy); desenho.set(i, p); } } public void rotaciona(double ang) { Ponto p; for (int i = 0; i < desenho.size(); i++) { p = (Ponto) desenho.get(i); double xx = p.getX(); double yy = p.getY(); p.setX(xx * Math.cos(ang) + yy * (-Math.sin(ang))); p.setY(xx * Math.sin(ang) + yy * Math.cos(ang)); desenho.set(i, p); } } public void deforma(boolean maior) { Ponto p; for (int i = 0; i < desenho.size(); i++) { if (maior) { p = (Ponto) desenho.get(i); p.setX(((p.getX() - mx) * 0.1) + p.getX()); p.setY(((p.getY() - my) * 0.1) + p.getY()); desenho.set(i, p); } else { p = (Ponto) desenho.get(i); p.setX(p.getX() - ((p.getX() - mx) * 0.1)); p.setY(p.getY() - ((p.getY() - my) * 0.1)); desenho.set(i, p); } } } } 4 Criar a classe Painel que herda JPanel e implementa ActionListener import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Painel extends JPanel implements ActionListener { JButton bSair; JFrame tela; Desenho desenho = new Desenho(); double mx, my; public Painel(int largura, final int altura, JFrame t) { tela = t; setFocusable(true); setDoubleBuffered(true); setSize(largura, altura); inicializa(); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int tecla = e.getKeyCode(); Color cor = tela.getForeground(); tela.setForeground(tela.getBackground()); desenho.desenha(tela.getGraphics()); System.out.println(tecla); switch (tecla) { case KeyEvent.VK_LEFT: desenho.translada(-1, 0); break; case KeyEvent.VK_UP: desenho.translada(0, -1); break; case KeyEvent.VK_RIGHT: desenho.translada(1, 0); break; case KeyEvent.VK_DOWN: desenho.translada(0, 1); break; case KeyEvent.VK_W: mx = desenho.getMouseX(); my = desenho.getMouseY(); desenho.translada(-mx, -my); desenho.rotaciona(0.1); desenho.translada(mx, my); break; case KeyEvent.VK_Q: mx = desenho.getMouseX(); my = desenho.getMouseY(); desenho.translada(-mx, -my); desenho.rotaciona(-0.1); desenho.translada(mx, my); break; case KeyEvent.VK_A: desenho.deforma(true); break; case KeyEvent.VK_S: desenho.deforma(false); break; } tela.setForeground(cor); desenho.desenha(tela.getGraphics()); } }); this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { int x = e.getX(); int y = e.getY(); desenho.setMouse(x, y); } }); } @Override public void actionPerformed(ActionEvent e) { repaint(); } private void inicializa() { setBackground(Color.black); setForeground(Color.white); bSair = new JButton(); bSair.setText("Sair"); GroupLayout layout = new GroupLayout(tela.getContentPane()); tela.getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 327, Short.MAX_VALUE) .addComponent(bSair))); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(bSair) .addGap(0, 277, Short.MAX_VALUE))); bSair.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tela.dispose(); } }); desenho.setMundo(0, 0, 20, 20); desenho.setViewPort(0, 0, 1, 1); desenho.triangulo(); } @Override public void paint(Graphics g) { Dimension dim = this.getSize(); desenho.setDimension(dim); desenho.desenha(g); } } 5 Crie a classe Tela que herda JFrame import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class Tela extends JFrame{ public Tela() { setBackground(Color.black); setForeground(Color.white); setUndecorated(true); Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit(); Dimension scrSize = toolkit.getScreenSize(); setSize(scrSize.width, scrSize.height); add(new Painel(scrSize.width, scrSize.height, this)); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new Tela(); } }