How To Make Draw An Object Aprear And Desapear With Left Click In Java Jpanel
Here are some very simple examples which prove how to pigment outside paintComponent
.
The drawing actually happens on a coffee.awt.image.BufferedImage
, and we can practice that anywhere, as long every bit we're on the Issue Dispatch Thread. (For discussion of multithreading with Swing, see here and here.)
So, I'm overriding paintComponent
, but just to paint the image on to the panel. (I too paint a niggling swatch in the corner.)
This way the drawing is actually permanent, and Swing is able to repaint the panel if it needs to without causing a trouble for usa. We could as well do something similar save the prototype to a file easily, if nosotros wanted to.
import javax.swing.*; import java.awt.*; import coffee.awt.image.*; import java.awt.outcome.*; /** * Holding left-click draws, and * right-clicking cycles the color. */ grade PaintAnyTime { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new PaintAnyTime(); } }); } Color[] colors = {Color.scarlet, Color.bluish, Colour.black}; int currentColor = 0; BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); Graphics2D imgG2 = img.createGraphics(); JFrame frame = new JFrame("Paint Any Fourth dimension"); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics grand) { super.paintComponent(g); // Creating a copy of the Graphics // and then whatever reconfiguration we practice on // it doesn't interfere with what // Swing is doing. Graphics2D g2 = (Graphics2D) k.create(); // Drawing the prototype. int w = img.getWidth(); int h = img.getHeight(); g2.drawImage(img, 0, 0, due west, h, zilch); // Drawing a swatch. Colour color = colors[currentColor]; g2.setColor(color); g2.fillRect(0, 0, 16, 16); g2.setColor(Color.black); g2.drawRect(-1, -1, 17, 17); // At the end, we dispose the // Graphics re-create we've created g2.dispose(); } @Override public Dimension getPreferredSize() { return new Dimension(img.getWidth(), img.getHeight()); } }; MouseAdapter drawer = new MouseAdapter() { boolean rButtonDown; Signal prev; @Override public void mousePressed(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { prev = e.getPoint(); } if (SwingUtilities.isRightMouseButton(due east) && !rButtonDown) { // (This just behaves a picayune better // than using the mouseClicked event.) rButtonDown = true; currentColor = (currentColor + 1) % colors.length; panel.repaint(); } } @Override public void mouseDragged(MouseEvent due east) { if (prev != null) { Point next = due east.getPoint(); Color colour = colors[currentColor]; // We can safely pigment to the // prototype whatever fourth dimension we want to. imgG2.setColor(color); imgG2.drawLine(prev.10, prev.y, adjacent.x, side by side.y); // We just need to repaint the // panel to brand certain the // changes are visible // immediately. panel.repaint(); prev = next; } } @Override public void mouseReleased(MouseEvent due east) { if (SwingUtilities.isLeftMouseButton(e)) { prev = null; } if (SwingUtilities.isRightMouseButton(e)) { rButtonDown = false; } } }; PaintAnyTime() { // RenderingHints let you specify // options such as antialiasing. imgG2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); imgG2.setStroke(new BasicStroke(three)); // panel.setBackground(Color.white); panel.addMouseListener(drawer); panel.addMouseMotionListener(drawer); Cursor cursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); panel.setCursor(cursor); frame.setContentPane(panel); frame.pack(); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Information technology's also possible to set up a JLabel
with an ImageIcon
, although personally I don't like this method. I don't think JLabel
and ImageIcon
are required by their specifications to see changes nosotros make to the image afterward we've passed it to the constructors.
This way too doesn't let us do stuff like painting the swatch. (For a slightly more complicated paint program, on the level of e.g. MSPaint, we'd want to accept a way to select an area and describe a bounding box effectually it. That's another place we'd want to be able to pigment straight on the panel, in add-on to cartoon to the image.)
import javax.swing.*; import coffee.awt.*; import java.awt.image.*; import java.awt.event.*; /** * Holding left-click draws, and * right-clicking cycles the colour. */ class PaintAnyTime { public static void principal(Cord[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new PaintAnyTime(); } }); } Color[] colors = {Color.red, Color.blueish, Color.black}; int currentColor = 0; BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); Graphics2D imgG2 = img.createGraphics(); JFrame frame = new JFrame("Paint Any Time"); JLabel label = new JLabel(new ImageIcon(img)); MouseAdapter drawer = new MouseAdapter() { boolean rButtonDown; Point prev; @Override public void mousePressed(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(due east)) { prev = due east.getPoint(); } if (SwingUtilities.isRightMouseButton(e) && !rButtonDown) { // (This merely behaves a little better // than using the mouseClicked event.) rButtonDown = truthful; currentColor = (currentColor + 1) % colors.length; characterization.repaint(); } } @Override public void mouseDragged(MouseEvent e) { if (prev != zilch) { Betoken next = eastward.getPoint(); Color colour = colors[currentColor]; // Nosotros tin can safely pigment to the // image any time nosotros want to. imgG2.setColor(color); imgG2.drawLine(prev.x, prev.y, next.ten, next.y); // Nosotros just need to repaint the // label to make sure the // changes are visible // immediately. label.repaint(); prev = next; } } @Override public void mouseReleased(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(due east)) { prev = goose egg; } if (SwingUtilities.isRightMouseButton(east)) { rButtonDown = false; } } }; PaintAnyTime() { // RenderingHints let you specify // options such every bit antialiasing. imgG2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); imgG2.setStroke(new BasicStroke(three)); // characterization.setPreferredSize(new Dimension(img.getWidth(), img.getHeight())); label.setBackground(Color.white); label.setOpaque(true); label.addMouseListener(drawer); characterization.addMouseMotionListener(drawer); Cursor cursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); label.setCursor(cursor); frame.add(label, BorderLayout.Heart); frame.pack(); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(truthful); } }
Source: https://stackoverflow.com/questions/21322353/java-jpanel-getgraphics
Posted by: charettebegather1962.blogspot.com
0 Response to "How To Make Draw An Object Aprear And Desapear With Left Click In Java Jpanel"
Post a Comment