Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void paint(Graphics g){
- g.drawLine(......
- ....
- //along with all of my other draw commands
- }
- boolean drawHello = true;
- boolean drawWorld = false;
- protected void paintComponent(Graphics g) {
- super.paintCompoent(g);
- if (drawHello)
- g.drawString("Hello", 50, 50);
- if (drawWorld)
- g.drawString("World", 10, 10);
- }
- public void actionPerformed(ActionEvent e) {
- drawWorld = true;
- repaint();
- }
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- public class AddSquares {
- private int R = 0;
- private int G = 0;
- private int B = 0;
- private int xLoc = 0;
- private int yLoc = 0;
- List<Square> squares = new ArrayList<>();
- private JButton addSquare = new JButton("Add Square");
- private RectsPanel panel = new RectsPanel();
- public AddSquares() {
- addSquare.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- Color color = new Color(R, G, B);
- squares.add(new Square(xLoc, yLoc, color));
- panel.repaint();
- R += 10;
- G += 20;
- B += 30;
- xLoc += 20;
- yLoc += 20;
- }
- });
- JFrame frame = new JFrame("Draw Squares");
- frame.add(panel, BorderLayout.CENTER);
- frame.add(addSquare, BorderLayout.SOUTH);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- }
- private class RectsPanel extends JPanel {
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- for (Square square : squares) {
- square.drawSquare(g);
- }
- }
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(250, 250);
- }
- }
- private class Square {
- int x = 0;
- int y = 0;
- Color color;
- public Square(int x, int y, Color color) {
- this.x = x;
- this.y = y;
- this.color = color;
- }
- public void drawSquare(Graphics g) {
- g.setColor(color);
- g.fillRect(x, y, 75 ,75);
- }
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- AddSquares addSquares = new AddSquares();
- }
- });
- }
- }
- import javax.swing.*;
- import java.awt.*;
- import java.awt.image.*;
- import java.awt.event.*;
- class PaintAnyTime {
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- new PaintAnyTime();
- }
- });
- }
- final BufferedImage image = (
- new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB)
- );
- final JFrame frame = new JFrame();
- final JLabel label = new JLabel(new ImageIcon(image));
- final MouseAdapter drawer = new MouseAdapter() {
- Graphics2D g2D;
- @Override
- public void mousePressed(MouseEvent me) {
- g2D = image.createGraphics();
- g2D.setColor(Color.BLACK);
- }
- @Override
- public void mouseDragged(MouseEvent me) {
- g2D.fillRect(me.getX(), me.getY(), 3, 3);
- label.repaint();
- }
- @Override
- public void mouseReleased(MouseEvent me) {
- g2D.dispose();
- g2D = null;
- }
- };
- PaintAnyTime() {
- label.setPreferredSize(
- new Dimension(image.getWidth(), image.getHeight())
- );
- label.addMouseListener(drawer);
- label.addMouseMotionListener(drawer);
- frame.add(label);
- frame.pack();
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement