/** * test image generator */ private static RenderedImage generateTestImage(int width, int height, Random r, double angle) { BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, width, height); g.setColor(Color.BLUE); int x = width / 2; int y = height / 2; int radius = Math.min(x, y); g.drawLine(x, y, x + (int) (radius * Math.cos(angle)), y + (int) (radius * Math.sin(angle))); g.drawString("giftest", r.nextInt(width), r.nextInt(height)); return img; }
public void drawTopology(Graphics g) { int x=20, y=50; String label; Dimension box=getSize(); Color old=g.getColor(); if(coordinator) { g.setColor(Color.cyan); g.fillRect(11, 31, box.width - 21, box.height - 61); g.setColor(old); } g.drawRect(10, 30, box.width - 20, box.height - 60); g.setFont(myFont); for(int i=0; i < members.size(); i++) { label=members.get(i).toString(); drawNode(g, x, y, label, NormalStyle); y+=50; } }
static Icon getColorIcon (java.awt.Color color) { BufferedImage image = new BufferedImage(32, 16, BufferedImage.TYPE_INT_RGB); java.awt.Graphics g = image.getGraphics(); g.setColor(color); g.fillRect(1, 1, 30, 14); g.setColor(java.awt.Color.black); g.drawRect(0, 0, 31, 15); return new ImageIcon(image); }
/** * Overrides the painting of the bar's track (the darker part underneath that extends * the full page length). */ @Override protected void paintTrack(Graphics graphics, JComponent jComponent, Rectangle rectangle) { graphics.setColor(trackColor); graphics.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); }
private void shearY(Graphics g, int w1, int h1, Color color) { int period = random.nextInt(40) + 10; // 50; boolean borderGap = true; int frames = 20; int phase = 7; for (int i = 0; i < w1; i++) { double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames); g.copyArea(i, 0, 1, h1, 0, (int) d); if (borderGap) { g.setColor(color); g.drawLine(i, (int) d, i, 0); g.drawLine(i, (int) d + h1, i, h1); } } }
long start = System.nanoTime(); super.paintComponent(g); int w = this.getWidth(); int h = this.getHeight(); g.drawImage(background, 0, 0, this); double theta = 2 * Math.PI * index++ / 64; g.setColor(Color.blue); rect.setRect( (int) (Math.sin(theta) * w / 3 + w / 2 - RADIUS), (int) (Math.cos(theta) * h / 3 + h / 2 - RADIUS), 2 * RADIUS, 2 * RADIUS); g.fillOval(rect.x, rect.y, rect.width, rect.height); g.setColor(Color.white); if (frameCount == FRAMES) { averageTime = totalTime / FRAMES; g.drawString(s, 5, 16); background = gc.createCompatibleImage(w, h, Transparency.OPAQUE); Graphics2D g = background.createGraphics(); g.clearRect(0, 0, w, h); g.setColor(Color.green.darker()); for (int i = 0; i < 128; i++) { g.drawLine(w / 2, h / 2, r.nextInt(w), r.nextInt(h)); g.dispose(); System.out.println("Resized to " + w + " x " + h);
g2.setColor(Color.WHITE); g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding); g2.setColor(Color.BLACK); int y1 = y0; if (scores.size() > 0) { g2.setColor(gridColor); g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1); g2.setColor(Color.BLACK); String yLabel = ((int) ((getMinScore() + (getMaxScore() - getMinScore()) * ((i * 1.0) / numberYDivisions)) * 100)) / 100.0 + ""; FontMetrics metrics = g2.getFontMetrics(); int labelWidth = metrics.stringWidth(yLabel); g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3); g2.drawLine(x0, y0, x1, y1); int y1 = y0 - pointWidth; if ((i % ((int) ((scores.size() / 20.0)) + 1)) == 0) { g2.setColor(gridColor); g2.drawLine(x0, getHeight() - padding - labelPadding - 1 - pointWidth, x1, padding); g2.setColor(Color.BLACK); String xLabel = i + ""; FontMetrics metrics = g2.getFontMetrics(); int labelWidth = metrics.stringWidth(xLabel); g2.drawString(xLabel, x0 - labelWidth / 2, y0 + metrics.getHeight() + 3); g2.drawLine(x0, y0, x1, y1); g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
private JFrame frame = new JFrame("sssssssss"); private JButton tip1Null = new JButton(" test button "); final Border compound2; compound2 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl2)); tip1Null.setFont(new Font("Serif", Font.BOLD, 14)); tip1Null.setForeground(Color.darkGray); tip1Null.setPreferredSize(new Dimension(50, 30)); tip1Null.addActionListener(new java.awt.event.ActionListener() { @Override frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tip1Null, BorderLayout.CENTER); frame.setLocation(150, 150); frame.setPreferredSize(new Dimension(310, 75)); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true); ((Graphics2D) g).setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(color); g.drawRoundRect(x, y, width, height, MARGIN, MARGIN); paintText(g, b, b.getBounds(), b.getText()); g.setColor(Color.red.brighter()); g.fillRect(0, 0, b.getSize().width, b.getSize().height);
frame.pack(); frame.setVisible(true); this.setBackground(Color.lightGray); this.setPreferredSize(new Dimension( image.getWidth(null), image.getHeight(null))); this.addMouseListener(new MouseAdapter() { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.translate(this.getWidth() / 2, this.getHeight() / 2); g2d.rotate(theta); g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2); g2d.drawImage(image, 0, 0, null); public void actionPerformed(ActionEvent e) { theta += dt; repaint(); return new Dimension(SIZE, SIZE); g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1)); g2d.setStroke(new BasicStroke(size / 8)); g2d.drawLine(0, size / 2, size, size / 2); g2d.drawLine(size / 2, 0, size / 2, size); g2d.dispose(); return bi;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, BORDER_GAP, BORDER_GAP); g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, getWidth() - BORDER_GAP, getHeight() - BORDER_GAP); int y0 = getHeight() - (((i + 1) * (getHeight() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP); int y1 = y0; g2.drawLine(x0, y0, x1, y1); int y0 = getHeight() - BORDER_GAP; int y1 = y0 - GRAPH_POINT_WIDTH; g2.drawLine(x0, y0, x1, y1); g2.setColor(GRAPH_COLOR); g2.setStroke(GRAPH_STROKE); for (int i = 0; i < graphPoints.size() - 1; i++) { int x2 = graphPoints.get(i + 1).x; int y2 = graphPoints.get(i + 1).y; g2.drawLine(x1, y1, x2, y2); g2.setColor(GRAPH_POINT_COLOR); for (int i = 0; i < graphPoints.size(); i++) { int x = graphPoints.get(i).x - GRAPH_POINT_WIDTH / 2; int ovalW = GRAPH_POINT_WIDTH; int ovalH = GRAPH_POINT_WIDTH; g2.fillOval(x, y, ovalW, ovalH);
addMouseListener(myMouseAdapter); addMouseMotionListener(myMouseAdapter); return new Dimension(PREF_W, PREF_H); RenderingHints.VALUE_ANTIALIAS_ON); g2.setStroke(STROKE); g.setColor(POINTS_COLOR); for (List<Point> pointList : pointsList) { if (pointList.size() > 1) { int x2 = p2.x; int y2 = p2.y; g.drawLine(x1, y1, x2, y2); p1 = p2; g.setColor(CURRENT_POINTS_COLOR); if (currentPointList != null && currentPointList.size() > 1) { Point p1 = currentPointList.get(0); int x2 = p2.x; int y2 = p2.y; g.drawLine(x1, y1, x2, y2); p1 = p2; currentPointList = new ArrayList<Point>(); currentPointList.add(mEvt.getPoint()); repaint();
Point point = e.getPoint(); int width = getWidth(); int height = getHeight(); repaint(); addMouseMotionListener(mouseHandler); return new Dimension(200, 200); protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); int width = getWidth(); int height = getHeight(); g2d.setColor(Color.BLUE); g2d.fill(cell); g2d.setColor(Color.GRAY); for (Rectangle cell : cells) { g2d.draw(cell); g2d.dispose();
repaint(); repaint(); super.paintComponent(g); for (Line line : lines) { g.setColor(line.color); g.drawLine(line.x1, line.y1, line.x2, line.y2); JFrame testFrame = new JFrame(); testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); final LinesComponent comp = new LinesComponent(); comp.setPreferredSize(new Dimension(320, 200)); testFrame.getContentPane().add(comp, BorderLayout.CENTER); JPanel buttonsPanel = new JPanel(); JButton newLineButton = new JButton("New Line"); JButton clearButton = new JButton("Clear"); buttonsPanel.add(newLineButton); buttonsPanel.add(clearButton); testFrame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH); newLineButton.addActionListener(new ActionListener() { testFrame.pack(); testFrame.setVisible(true);
return new Dimension(image.getWidth(), image.getHeight()); w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.drawImage(old, 0, 0, null); g2d.setPaint(Color.red); g2d.setFont(new Font("Serif", Font.BOLD, 20)); String s = "Hello, world!"; FontMetrics fm = g2d.getFontMetrics(); int x = img.getWidth() - fm.stringWidth(s) - 5; int y = fm.getHeight(); g2d.drawString(s, x, y); g2d.dispose(); return img; protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, null); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new TextOverlay()); f.pack(); f.setVisible(true);
final JFrame frame = new JFrame("Nested Layout Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setBorder( new TitledBorder("BorderLayout(5,5)") ); plafComponents.setBorder( new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)") ); plafComponents.add(plafChooser); plafComponents.add(pack); gui.add(plafComponents, BorderLayout.NORTH); 20f,20f,Color.red, 180f,180f,Color.yellow); g.setPaint(gp); g.fillRect(0,0,200,200); ImageIcon ii = new ImageIcon(bi); JLabel imageLabel = new JLabel(ii); gui.add( splitPane, BorderLayout.CENTER ); frame.setContentPane(gui); frame.pack(); frame.setLocationRelativeTo(null);
this.setPreferredSize(new Dimension(640, 480)); this.addMouseListener(mouseHandler); this.addMouseMotionListener(mouseHandler); super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.blue); g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL)); g.drawLine(p1.x, p1.y, p2.x, p2.y); p1 = e.getPoint(); p2 = p1; repaint(); this.add(new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0)); this.add(new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA)); this.add(new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0)); this.add(new MoveButton("\u2193", KeyEvent.VK_DOWN, 0, DELTA)); ControlPanel.this.getInputMap(WHEN_IN_FOCUSED_WINDOW) .put(k, k.toString()); ControlPanel.this.getActionMap() .put(k.toString(), new AbstractAction() {
+ "The effect we want is a multi-line label."; JFrame f = new JFrame("Label Render Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Color.orange); imageGraphics.setPaint(gp); imageGraphics.fillRect(0, 0, 400, 300); textLabel.setSize(textLabel.getPreferredSize()); BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); g.setColor(new Color(255, 255, 255, 128)); g.fillRoundRect( 0, 0, 15, 10); g.setColor(Color.black); textLabel.paint(g); Graphics g2 = image.getGraphics(); g2.drawImage(bi, 20, 20, f); f.setVisible(true);
/** * show the images as stream of JFrame on desktop */ public void show() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); JLabel label = null; while (true) { for (int i = 0; i < this.frames.size(); i++) { Frame frame = this.frames.get(i); if (label == null) { label = new JLabel(new ImageIcon(frame.image)); f.getContentPane().add(label); f.pack(); } else { label.getGraphics().drawImage(frame.image,0,0, label); } try {Thread.sleep(frame.delayMillis);} catch (InterruptedException e) {} } } } }
import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(600, 400); JPanel panel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(0, 0, 100, 100); } }; frame.add(panel); // Graphics g = panel.getGraphics(); // g.setColor(Color.BLUE); // g.fillRect(0, 0, 100, 100); frame.validate(); // because you added panel after setVisible was called frame.repaint(); // because you added panel after setVisible was called } }