paintcomponent

Painting pixels images in Java

耗尽温柔 提交于 2019-12-03 07:25:55
Which method is the best way to create a pixel image with java. Say, I want to create a pixel image with the dimensions 200x200 which are 40.000 pixels in total. How can I create a pixel from a random color and render it at a given position on a JFrame. I tried to create a own component which just creates pixel but it seems that this is not very performant if I create such a pixel a 250.000 times with a for-loop and add each instance to a JPanels layout. class Pixel extends JComponent { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(getRandomColor());

Panel.repaint(); messes up layout

浪子不回头ぞ 提交于 2019-12-02 22:59:58
问题 JPanel Initiation p = new JPanel() { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { if(errors == 1) g.drawOval(215, 50, 75, 75); else if(errors == 2) g.drawOval(200,200,200,200); } }; Method that calls repaint static void drawHead() { System.out.println("Head"); errors = 1; p.removeAll(); p.revalidate(); p.repaint(); } Before repaint my frame looks like this, http://i.imgur.com/XQlQeul.png And afterwards it looks like this, http://i.imgur.com/RnVuUzt

Drawing on a buffered image

廉价感情. 提交于 2019-12-02 22:42:35
问题 I am trying to draw on a buffered image. I am able to get the picture on the frame but it doesnt seem to draw on the image. If I use BufferedImage bufferedImage = new BufferedImage(1280, 800,BufferedImage.TYPE_INT_RGB); then it seems to draw the string but I would like to ideally draw on the image as I need to plot some coordinates on the image for a project. Any guidance would be highly appreciated. Excuse the bad indentation import java.awt.Dimension; import java.awt.Graphics; import java

draw two line by KeyTyped event in java

情到浓时终转凉″ 提交于 2019-12-02 22:34:02
问题 hi I want to draw horizontal and vertical axis in a panel by pressing h and v on keyboard . I don't know how ... this is my code . how can I control the Keytyped event import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import javax.swing.JPanel; import java.awt.geom.Line2D; @SuppressWarnings("serial") public class DrawAxis extends JPanel implements KeyListener { private

Mirroring an Object in JFrame with paintComponent

笑着哭i 提交于 2019-12-02 21:26:26
问题 I created a class which is a "mirror" object. The class constructor has mirrors coordinates and direction. In this class also there is a paintComponent method. I am trying to create a mirror object with this class in my frame and automatically paint a mirror with coordinates and direction. There is the "mirror" class. Can I do this like that? import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JComponent; @SuppressWarnings("serial") class Mirror

Java - rounded corner panel with compositing in paintComponent

无人久伴 提交于 2019-12-02 20:50:17
From the original question (below), I am now offering a bounty for the following: An AlphaComposite based solution for rounded corners. Please demonstrate with a JPanel . Corners must be completely transparent. Must be able to support JPG painting, but still have rounded corners Must not use setClip (or any clipping) Must have decent performance Hopefully someone picks this up quick, it seems easy. I will also award the bounty if there is a well-explained reason why this can never be done, that others agree with. Here is a sample image of what I have in mind (but using AlphaComposite )

Java - draw circle after clicking button

三世轮回 提交于 2019-12-02 17:09:21
问题 I am trying to draw a circle with the press of a button in java. I put the System.out.println() inside the action method to make sure my code was working. The println shows up but no circle drawing anywhere. Any Suggestions? Thank you import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class CircleViewer { public static void main(String[] args) {

KeyListener not working with PaintComponent

允我心安 提交于 2019-12-02 15:58:57
问题 My program is supposed to wait for either the left or right arrow key to be pressed, then change a value so the next time PaintComponent is updated, the screen looks different. However, the screen doesn't change when I run the program. Here are the variable declarations: static KeyListener listen; public static int slide = 0; Here's the KeyListener declaration in the main: listen = new KeyListener() { public void keyPressed(KeyEvent arg0) { if(arg0.getKeyCode() == KeyEvent.VK_LEFT && slide>0)

How can I draw an image part by part?

*爱你&永不变心* 提交于 2019-12-02 15:08:52
问题 class DrawIma extends JPanel{ protected void paintComponent(Graphics g) { super.paintComponent(g); for (int i=0;i<20;i++){ for (int j=0;j<20;j++) { g.drawImage(BuArr[i*20+j], 20*i, 20*j, 20, 20, null); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } } In this part, BuArr are the 400 blocks divided from a BufferedImage , now i want them to be draw one by one, but the method can not draw the blocks separately, how can i do this? 回答1: Swing is single

How to draw 2d arrows with Java Swing?

岁酱吖の 提交于 2019-12-02 13:10:28
I'm trying to draw some rotatable arrows with Swing 2d, there are some sample code online, so I copied and combined them into one app, but there is something wrong with each of the 3 methods : 1st one doesn't rotate from it's center, the other 2 don't look correctly in the arrow head, can someone show me how to fix them ? import java.awt.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; public class Arrow_Test extends JPanel implements ChangeListener { Path2D.Double arrow = createArrow(); double theta = 0; public void stateChanged(ChangeEvent e) { int value = (