Graphics in Java


Tip
If you have limited resources, try to use AWT in your applications in Java.
Si tiene recursos limitados, trate de usar AWT en sus aplicaciones de Java.

Problem 1
Create a Java application called TestGraphics to draw a rectangle fill with blue and a rectangle draw with red borders.
Cree una aplicación de Java llamada TestGraphics para dibujar un rectángulo relleno con azul y un rectángulo con bordes rojos.

TestGraphicsRun

TestGraphics.java
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class TestGraphics extends JFrame
{
     public TestGraphics()
     {
          setTitle("Test Graphics");
          getContentPane().add(new ColorPanel());
     }

     public static void main(String[] args)
     {
          TestGraphics frame = new TestGraphics();
          frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          frame.setSize(300,250);
          frame.setVisible(true);
     }
}

class ColorPanel extends JPanel
{
     @Override
     public void paintComponent(Graphics g)
     {
          super.paintComponent(g); //Call the original paintComponent
          //
          int rectWidth = getWidth() - 20;
          int rectHeight = getHeight() - 20;
          //__________________________________________
          g.setColor(new Color(255, 0, 0)); //Set the color to Red
          g.drawRect(10, 10, rectWidth, rectHeight);
          //_______________________________________________
          g.setColor(new Color(0, 0, 255)); //Set the color to Blue
          g.fill3DRect(20, 20, rectWidth-20, rectHeight-20, false);
     }
}


Tip
The most popular commands to draw are:
  • One line drawLine
  • Several lines drawPolyline
  • Ellipse (círculo) drawOval and fillOval
  • An ellipse arc drawArc and fillArc
  • One polygon drawPolygon and fillPolygon
  • One rectangle drawRect, fillRect, rounRect, fillRoundRect
  • One image drawImage
  • text drawString

Los comando más populares para dibujar son:
  • Una líinea drawLine
  • Varias líneas drawPolyline
  • Elipse (círculo) drawOval y fillOval
  • Un arco de una elipse drawArc y fillArc
  • Un polygon drawPolygon y fillPolygon
  • Un rectángulo drawRect, fillRect, rounRect, fillRoundRect
  • Una imágen drawImage
  • Texto drawString

getFontMetrics

The graphics object has the getFontMetrics method to get several metrics of the font, such as: height, ascend, descent, etc. These metrics are used to calculate the perfect spacing when drawing text.
El objeto de gráficos tiene el método getFontMetrics para obtener métricas de la fuente, tales como: altura, ascendente, descendente, etc. Estas métricas pueden ser usadas para calcular el espacio perfecto cuando se dibuja texto.

Problem 2
Create a Java application called SpiderJ to produce the drawing shown below. Use lines to draw the figure.
Cree una aplicación de Java llamada SpiderJ para producir el dibujo mostrado debajo. Use líneas para dibujar la figura.

SpiderJRun

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home