paintcomponent

paintComponent is not working (Java)

瘦欲@ 提交于 2019-12-10 20:13:42
问题 I have been trying to override and use the paint component method instead of the paint method because I've seen it been suggested in multiple questions around here. I've looked through many questions but I still can't seem to get this to work. I'm posting my original piece of code used to render a screen. I am thinking that extending JFrame is not the right way to go and instead I need to extend JPanel, and use paint component from there. I have another object where I actually extend the

Draw custom stuff on top of opaque components in a JPanel

旧街凉风 提交于 2019-12-10 18:37:28
问题 I have an JPanel populated with several opaque custom components. Now I would like to draw something on top of these components by overriding the paintComponent() method. My problem is that the painted stuff is placed behind the embedded components and, as they are opaque, is covered by them. Is there any way to let the painting appear on top of the components? Here's a short example of what I'm trying to do: public class DrawOnTop { public static void main(String[] args) { SwingUtilities

Why does this this Swing bug occur when using repaint() and not with getParent().repaint()?

百般思念 提交于 2019-12-10 18:05:23
问题 This question is based on a problem I had a while back with a simple Swing dice program. The original question I posted is here and has an accepted answer, but I'd like to know exactly what is happening, why the problem occurs, and why the solution works. I managed to whittle down the original code to get to the core of the problem and it now looks very different: I have two ColorPanel s that each draw a coloured square when you click on a panel the box should change colour in this order:

Drawing with paintComponent in Applet Upon Event

断了今生、忘了曾经 提交于 2019-12-10 11:00:42
问题 I'm trying to create an applet that will produce as many ovals as the number specified within a textbox. The textbox appears, but upon hitting enter, my paintComponent does not draw. Thank you in advance. import java.awt.*; import java.awt.event.*; import javax.swing.*; import net.miginfocom.layout.*; import net.miginfocom.swing.MigLayout; import java.awt.geom.*; public class OvalDrawer extends JApplet { private JLabel numberL; private JTextField numberTF; private NumHandler numHandler;

How to make the background gradient of a JPanel

点点圈 提交于 2019-12-10 06:10:18
问题 I want to know how to make background gradient which is in another JPanel. Many articles found in internet,but all of them had demostrated how to overide the paintComponent() of the JPanel not how to do for a jPanel which is inside it. I use Netbeans IDE. I created a new JPanel class and could overide its paintComponent(). I have another jpanel on it (dragged & dropped on to the parent JPanel). I want to make its background gradient. Here is how I tried for parent. It worked. How can I

Simulating rain

*爱你&永不变心* 提交于 2019-12-10 01:52:59
问题 I am making a game in java and I want to create a simulation of a cloud that is pouring rain. The cloud is supposed to move to the right while raining. Moving the cloud is no problem. It's the rain that I am struggling with. What I was thinking of doing was with a timer to draw a rectangle, thats supposed to look like falling rain at a random x value inside of the cloud. And then add 1 to the y value of the drop each 100 millisecond. But I don't want to create 100 different rectangles, x

Why does swing draw simple component twice?

被刻印的时光 ゝ 提交于 2019-12-09 16:43:07
问题 Here is simple example of drawing an oval. public class SwingPainter extends JFrame{ public SwingPainter() { super("Swing Painter"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); getContentPane().add(new MySwingComponent()); setSize(200, 200); setVisible(true); } public static void main(String[] args) { new SwingPainter(); } class MySwingComponent extends JComponent { public void paintComponent(Graphics g) { System.out.println("paintComponent"); super.paintComponent(g); g.setColor

Java Graphics2D is Drawing One Pixel Off (Rounding error?)

偶尔善良 提交于 2019-12-08 18:07:34
I have been trying to make a program in Java using Graphics2D and PaintComponent. However, Java is not rounding some values correctly resulting in a few points being rendered a pixel different from where it supposed to be which gives an unclean image. You can see what I mean in the picture below. Here is my code. What can I change to fix this? Thank you! public void paintComponent( Graphics g ) { super.paintComponent( g ); g.setColor(Color.red); int orginX = getWidth()/2; int orginY = getHeight()/2; for(int i=0; i<=360; i+= 10) { double angle = Math.toRadians(i); double centerX = radius * Math

How would I go about creating a custom graphics class for paintComponent to use?

▼魔方 西西 提交于 2019-12-08 11:35:30
问题 I've been searching around to understand better what I am trying to do but with little luck. I'm assuming the answer is much simpler than I realize. I am making a board game for a class project and am using a JPanel to display the graphics (obviously). In the paintComponent, rather than just create everything in one location I'd like to externalize my graphics into a class of there own which I can simply instantiate within paintComponent. Trying to keep everything logically separated and

How can I set the stroke color at runtime in JAVA?

☆樱花仙子☆ 提交于 2019-12-08 04:53:28
问题 I'm trying to create an interface where the user can change the color of a line to mark what is background or what is foreground of an image. So far I have coded this: private class ImageLine extends JComponent { java.awt.Point p1,p2; BufferedImage show; ArrayList <Shape> shapes = new ArrayList<Shape>(); int flag = 0; Color color = Color.ORANGE; public ImageLine(BufferedImage img) { show = img; setPreferredSize(new Dimension(img.getWidth(), img.getHeight())); this.addMouseListener ( new