0% found this document useful (0 votes)
17 views1 page

Trabalhando Com Formas 2D

The document defines three Java classes to draw a rectangle on a panel within a frame window. The DrawTest class contains the main method and creates a DrawFrame object. The DrawFrame class extends JFrame and adds a DrawPanel. The DrawPanel class extends JPanel and overrides the paintComponent method to draw a rectangle using Graphics2D.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Trabalhando Com Formas 2D

The document defines three Java classes to draw a rectangle on a panel within a frame window. The DrawTest class contains the main method and creates a DrawFrame object. The DrawFrame class extends JFrame and adds a DrawPanel. The DrawPanel class extends JPanel and overrides the paintComponent method to draw a rectangle using Graphics2D.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Import java.awt.

*;

Import java.awt.geom.*;

Import javax.swing.*;

public class DrawTest {

public static void main (String[] args){


DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

// Um frame que contém um painel com desenhos

class DrawFrame extends JFrame{

public DrawFrame(){
setTitle("DrawTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

//adiciona um painel ao frame

DrawPanel panel = new DrawPanel();


add(panel);
}

public static final int DEFAULT_WIDTH = 400;


public static final int DEFAULT_HEIGHT = 400;
}

// Um painel que exibe um retângulo


class DrawPanel extends JPanel{

public void paintComponent(Graphics g){


super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;

//desenha o retangulo
double leftX = 100;
double topY = 100;
double width = 200;
double height = 150;

Rectangle2D rect = new Rectangle2D.Double(leftX,topY,width,height);


g2.draw(rect);
}
}

You might also like