Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Java / Лекции / lection5.docx
Скачиваний:
70
Добавлен:
15.03.2015
Размер:
230.33 Кб
Скачать

5.3.3. Класс Graphics

За рисование графических примитивов в компонентах ComponentиJComponentотвечает объект классаGraphics.

Метод

Описание

Пример:

import java.awt.*;

import javax.swing.*;

public class ForegroundWindow extends JFrame {

public class DrawingPanel extends JPanel {

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawLine(20, 20, 100, 100);

g.drawOval(50, 30, 100, 150);

g.drawRect(90, 80, 150, 100);

g.drawRoundRect(130, 30, 80, 160, 20, 40);

g.drawString("Строка", 100, 100);

}

}

public ForegroundWindow() {

super("Пример окна верхнего уровня");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 250);

add(new DrawingPanel());

setVisible(true);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new ForegroundWindow().setVisible(true);

}

});

}

}

import java.awt.*;

import javax.swing.*;

public class ForegroundWindow extends JFrame {

public class DrawingPanel extends JPanel {

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(Color.GREEN);

g.fillOval(50, 30, 100, 150);

g.setColor(Color.BLUE);

g.fillRect(90, 80, 150, 100);

g.setColor(Color.RED);

g.fillRoundRect(130, 30, 80, 160, 20, 40);

}

}

public ForegroundWindow() {

super("Пример окна верхнего уровня");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 250);

add(new DrawingPanel());

setVisible(true);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new ForegroundWindow().setVisible(true);

}

});

}

}

import java.awt.*;

import javax.swing.*;

public class ForegroundWindow extends JFrame {

public class DrawingPanel extends JPanel {

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int xPoints[] = new int[50];

int yPoints[] = new int[50];

for(int i = 0; i < 50; i++) {

xPoints[i] = i * 6;

yPoints[i] =

(int)(80*Math.sin(i * Math.PI / 12)) + 150;

}

g.drawPolyline(xPoints, yPoints, 50);

}

}

public ForegroundWindow() {

super("Пример окна верхнего уровня");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 300);

add(new DrawingPanel());

setVisible(true);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new ForegroundWindow().setVisible(true);

}

});

}

}

5.3.4. Класс Color.

import java.awt.*;

import javax.swing.*;

public class ForegroundWindow extends JFrame {

public class DrawingPanel extends JPanel {

protected void paintComponent(Graphics g) {

super.paintComponent(g);

for(int i = 0; i < 256; i += 16)

for(int j = 0; j < 256; j+= 16) {

g.setColor(new Color(j, 128, i));

g.fillRect(i, j, 16, 16);

}

}

}

public ForegroundWindow() {

super("Пример окна верхнего уровня");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 300);

add(new DrawingPanel());

setVisible(true);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new ForegroundWindow().setVisible(true);

}

});

}

}

Соседние файлы в папке Лекции