Advertisement
fabioceep

Java: Interface com Imagem

Feb 20th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package interfacesgraficas;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class InterfacesGraficas implements ActionListener{
  8.     JFrame frame;
  9.    
  10.     public static void main(String[] args) {
  11.         InterfacesGraficas gui = new InterfacesGraficas();
  12.         gui.go();
  13.     }
  14.         public void go(){
  15.             frame = new JFrame();
  16.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17.            
  18.             JButton button = new JButton("clique aqui");
  19.             button.addActionListener(this);
  20.            
  21.             MyDrawPanel drawPanel = new MyDrawPanel();
  22.            
  23.             frame.getContentPane().add(BorderLayout.SOUTH, button);
  24.             frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
  25.             frame.setSize(300, 300);
  26.             frame.setVisible(true);
  27.         }
  28.     @Override
  29.         public void actionPerformed(ActionEvent event){
  30.             frame.repaint();
  31.         }
  32.    
  33.     }
  34.  
  35. ===================================================================================================================================
  36. package interfacesgraficas;
  37.  
  38. import javax.swing.*;
  39. import java.awt.*;
  40.  
  41. public class MyDrawPanel extends JPanel{
  42.     @Override
  43.     public void paintComponent(Graphics g){
  44.         /* Exemplo pg261
  45.         g.setColor(Color.orange);
  46.         g.fillRect(20,50,100,100);
  47.         */
  48.         Image image = new ImageIcon("C:\\Users\\fabio\\Documents\\NetBeansProjects\\mavenproject2\\InterfacesGraficas\\src\\interfacesgraficas\\pilha.jpg").getImage();
  49.         g.drawImage(image, 3,4, this);
  50.        
  51.     }
  52. }
  53. ==================================================================================================================================
  54. package interfacesgraficas;
  55.  
  56. import javax.swing.*;
  57. import java.awt.*;
  58.  
  59. public class MyDrawPanel extends JPanel{
  60.     @Override
  61.     public void paintComponent(Graphics g){
  62.         /* Exemplo pg261
  63.         g.setColor(Color.orange);
  64.         g.fillRect(20,50,100,100);
  65.         */
  66.        
  67.         /* Exemplo de uma imagem
  68.         Image image = new ImageIcon("C:\\Users\\fabio\\Documents\\NetBeansProjects\\mavenproject2\\InterfacesGraficas\\src\\interfacesgraficas\\pilha.jpg").getImage();
  69.         g.drawImage(image, 3,4, this);
  70.         */
  71.         /* Exemplo de um circulo que muda de cor */
  72.         Graphics2D g2d = (Graphics2D) g;
  73.         int red = (int) (Math.random() * 255);
  74.         int green = (int) (Math.random() * 255);
  75.         int blue = (int) (Math.random() * 255);
  76.        
  77.         Color startColor = new Color(red, green, blue);
  78.        
  79.         red = (int) (Math.random() * 255);
  80.         green = (int) (Math.random() * 255);
  81.         blue = (int) (Math.random() * 255);
  82.        
  83.         Color endColor = new Color(red, green, blue);
  84.         GradientPaint gradient = new GradientPaint(70, 70, startColor,150,150, endColor);
  85.         g2d.setPaint(gradient);
  86.         g2d.fillOval(70, 70, 100, 100);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement